"IT168" in the previous tutorial on Dwoo (PHP development required step-by-step PHP template engine Dwoo), we discussed the basic use of Dwoo. In this article, we will further explain some of the advanced usage in Dwoo, such as the authoring of plug-in mechanisms and how to integrate with the common PHP framework.
The inheritance of Dwoo template
One of the powerful features of Dwoo is that it can support Template inheritance. This is very simple to implement, you just have to define a parent template, then the child template can inherit the parent template, and you can define a variety of new features in the child templates. Smart developers will be adept at using this feature to reduce repetitive labor.
The key to inheriting templates in Dwoo is the "extended" plug-in mechanism, which allows templates to inherit another template. To better understand this feature, let's give an example to illustrate the following. Suppose you already have a template for the base class, called Base.tpl, which contains two areas, one is the navigation area, the other is the content area, as follows:
<body> {block "NAV"} {/block} {block ' content} ' <div id= "Content" > This is content for the main page. </div> {/block} </body>
|
Now, let's say that some of your pages need to use the horizontal navigation bar, you just have to inherit the base class template page, and you can redefine the style of the navigation bar, as in the following code, we define this code as CHILD.TPL:
{extends "BASE.TPL"} {block "NAV"} <div id= "NAV" > <a href= "#" >Home</a> <a href= "#" >News</a> <a href= "#" >Weather</a> <a href= "#" >Hotels</a> <a href= "#" >Dining</a> </tr> </table> </div> {/block} |
Here the {extends "BASE.TPL"} is used to indicate that the template of the parent class is inherited, and in the {block "NAV"} area here, the contents of the navigation bar are overridden and the final output is as follows:
Now we look at another scenario where you need additional child navigation menus on some pages, such as the need to display the navigation bar vertically, at which point we can use the following code:
{block" NAV "} {$dwoo. Parent} <div id= "Sub-nav" <ul> {loop $items} <li><a href= "#" >{$item}</a></li> &NBSP {/loop} </ul> &NBSP;&NBSP;&NBSP;&NBSP;</DIV>&NBSP;&NBSP;&NBSP;&NBSP {/block} {block "Content"} <div id= "content" this is Content for the dining page. </div> {/block} |
In this case, {$dwoo. Parent} is used to invoke the parent class template. Note that this is inherited from the Child.tpl template, that is, the contents of the navigation bar in the parent template are invoked, and a vertical navigation bar is redefined here, noting that the contents of the content area are overridden, overwriting the contents of the corresponding content area in the parent template, and the results are as follows:
As you can see, you can greatly facilitate development by using template inheritance as appropriate.