Decrypting the Template Inheritance of ThinkPHP 3.1.2 Version_PHP Tutorial

WBOY
Release: 2016-07-13 10:24:26
Original
999 people have browsed it

Template inheritance is a more flexible template layout method added in ThinkPHP 3.1.2 version. Template inheritance is different from template layout. In fact, it should be on the upper layer of template layout. Template inheritance is actually not difficult to understand. Just like class inheritance, a template can also define a basic template (or layout), and define relevant blocks in it, and then extend the sub-templates of the basic template. You can overload the blocks defined in the basic template.

Therefore, the advantage of template inheritance is actually to design blocks in the base template and replace these blocks in sub-templates.
Each block consists of tags, and nesting of block tags is not supported.
The following is a typical block design in the basic template (used to design website titles):

<block name="title"><title>网站标题</title></block>

Copy after login

The block tag must specify a 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 tags and variables, for example:

<block name="title"><title>{$web_title}</title></block>

Copy after login

You can even load external files in blocks:

<block name="include"><include file="Public:header" /></block>

Copy after login

A template can define any number of blocks with unique names and identifiers. For example, a base.html base template is defined below:

<html>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 <block name="title"><title>标题</title></block>
 </head>
 <body>
 <block name="menu">菜单</block>
 <block name="left">左边分栏</block>
 <block name="main">主内容</block>
 <block name="right">右边分栏</block>
 <block name="footer">底部</block>
 </body>
 </html>

Copy after login

Then we use inheritance in the child template (actually the entry template of the current operation):

<extend name="base" />
 <block name="title"><title>{$title}</title></block>
 <block name="menu">
 <a href="/" >首页</a>
 <a href="/info/" >资讯</a>
 <a href="/bbs/" >论坛</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">

Copy after login

Latest news:

 <volist name="news" id="new">
 <a href="/new/{$new.id}">{$new.title}</a><br/>
 </volist>
 </block>
 <block name="footer">
@ThinkPHP2012 版权所有
 </block>

Copy after login

As you can see, the extend tag is used in the sub-template to define the template that needs to be inherited. The usage of the extend tag is the same as the include tag. You can also load other templates:

<extend name="Public:base" />

Copy after login

Or use absolute file path to load

<extend name="./Tpl/Public/base.html" />

Copy after login

In the current sub-template, you can only define blocks and not other template content, otherwise it will be ignored directly, and only the blocks that have been defined in the basic template can be defined.
For example, if you take the following definition:

<block name="title"><title>{$title}</title></block>
 <a href="/" >首页</a>
 <a href="/info/" >资讯</a>
 <a href="/bbs/" >论坛</a>

Copy after login

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

In the sub-template, you can overload the definition of the block in the base template. If it is not redefined, it means that the block definition in the base template will be used. If an empty block is defined, it will be deleted. The content of this block in the base template.
In the above example, we deleted the contents of the left block and reloaded the other blocks.
The order of block definitions in sub-templates is arbitrary. The key to using template inheritance lies in how the basic template is laid out and designed. If combined with the original layout function, it will be more flexible.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/825440.htmlTechArticleTemplate inheritance is a more flexible template layout method added in ThinkPHP3.1.2 version. Template inheritance is different from template layout. , even speaking, it should be on the upper level of the template layout. Templates inherit their...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template