Template inheritance is a more flexible template layout method added by ThinkPHP3.1.2. The template inheritance is different from the template layout, and should be at the upper layer of the template layout. Template inheritance is not difficult to understand. Just like class inheritance, templates can also define a basic template (or layout) and define related blocks ), then, you can reload the blocks defined in the basic template in the subtemplate of the basic template.
Therefore, the advantage of template inheritance is to design blocks in the Basic Template and replace these blocks in the subtemplate.
Each block consists of <block> </block> tags, and does not support nesting of block tags.
Below is a typical block design in the Basic Template (used to design the website title ):
<Block name = "title"> <title> website title </title> </block>
The block tag must specify the name attribute to identify the name of the current block. This identifier should be unique in the current template. The block tag can contain any template content, including other labels and variables, for example:
<block name="title"><title>{$web_title}</title></block>
You can even load external files in the block:
<block name="include"><include file="Public:header" /></block>
In a template, you can define multiple block with unique names. For example, you can define a base.html basic template as follows:
<Html>
Then we use inheritance in the subtemplate (actually the portal template for the current operation:
<Extend name = "base"/> <block name = "title"> <title >{$ title} </title> </block> <block name = "menu"> <a href = "/"> homepage </a> <a href = "/info/"> Information </a> <a href = "/bbs/"> Forum </a> </block> <block name = "left"> </block> <block name = "content"> <volist name = "list" id = "vo"> <a href = "/new/{$ vo. id} "> {$ vo. title} </a> <br/> {$ vo. content} </volist> </block> <block name = "right">
Latest information:
<Volist name = "news" id = "new"> <a href = "/new/{$ new. id} "> {$ new. title }</a> <br/> </volist> </block> <block name = "footer"> @ ThinkPHP2012 copyright </block>
As you can see, the sub-template uses the extend tag to define the template to be inherited. The extend tag usage is the same as the include tag, you can also load other templates:
<extend name="Public:base" />
Or use the absolute file path to load
<extend name="./Tpl/Public/base.html" />
In the current subtemplate, only blocks can be defined, but other template content cannot be defined. Otherwise, blocks already defined in the basic template can be defined.
For example, if the following definition is used:
<Block name = "title"> <title >{$ title} </title> </block> <a href = "/"> homepage </a> <a href = "/info/"> Information </a> <a href = "/bbs/"> Forum </a>
The navigation part will be invalid and will not be displayed in the template.
In the subtemplate, You can reload the block definition in the basic template. if the definition is not redefined, the block definition in the basic template is used. If an empty block is defined, the block content in the basic template is deleted.
In the preceding example, the content of the left Block is deleted, and other blocks are reloaded.
The block definition sequence in the subtemplate is random. The key to the use of template inheritance lies in the layout and design planning of the basic template. if combined with the original layout function, it will be more flexible.