Decrypt template inheritance in ThinkPHP version 3.1. 2

  • 2021-07-02 23:37:46
  • OfStack

Template inheritance is a more flexible template layout added in ThinkPHP version 3.1. 2. Template inheritance is different from template layout, and even should be at the upper level of template layout. In fact, template inheritance is not difficult to understand. Just like class inheritance, a template can also define a basic template (or layout), in which relevant blocks (block) are defined, and then the blocks defined in the basic template can be overloaded by inheriting (extend) from the sub-templates of the basic template.

Therefore, the advantage of template inheritance is actually to design blocks in the base template (block) and replace these blocks in the sub-template.
Each block is composed of < block > < /block > Tags and does not support nesting of block tags.
The following 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, which should be 1-only in the current template. The block tag can contain any template content, including other tags and variables, such as:


<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>

One template can define any number of blocks whose names do not duplicate. For example, one base. html basic template is defined below:


<html>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 <block name="title"><title> Title </title></block>
 </head>
 <body>
 <block name="menu"> Menu </block>
 <block name="left"> Left side column </block>
 <block name="main"> Main content </block>
 <block name="right"> Right side column </block>
 <block name="footer"> Bottom </block>
 </body>
 </html>

Then we use inheritance in the sub-template (which is actually the entry template of the current operation):


<extend name="base" />
 <block name="title"><title>{$title}</title></block>
 <block name="menu">
 <a href="/" > Home page </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 reserved 
 </block>

As you can see, the extend tag is used in the sub-template to define the template to be inherited. The usage of extend tag is similar to that of include tag 1. 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 sub-template, only blocks can be defined, but no other template content can be defined, otherwise it will be ignored directly, and only blocks already defined in the basic template can be defined.
For example, if you use the following definition:


<block name="title"><title>{$title}</title></block>
 <a href="/" > Home page </a>
 <a href="/info/" > Information </a>
 <a href="/bbs/" > Forum </a>

The navigation section will be invalid and will not be displayed in the template.

In the sub-template, you can overload the block in the basic template. If it is not redefined, it means that the block definition in the basic template will be used. If one empty block is defined, it means that the content of the block in the basic template will be deleted.
In the above example, we deleted the contents of left block, and overloaded all other blocks.
The order of block definition in sub-template is random, and the key to the usage of template inheritance lies in how to lay out and design the basic template. If combined with the original layout function, it will be more flexible.


Related articles: