Template inheritance in thinkPHP is like class inheritance. A template can define a base template (or layout), define related blocks (blocks), and then inherit (extend) the sub-templates of the base 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 <block></block>
tags. The following is a typical block design in the basic template (used for designing website titles):
<block name="title"><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 Yes, the block tag can contain any template content, including other tags 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>
Any template can be defined Multiple names identify non-repeating blocks. 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>
Then we use it in the subtemplate (actually the entry template for the current operation) Inheritance:
<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"> 最新资讯: <volist name="news" id="new"> <a href="/new/{$new.id}">{$new.title}</a><br/> </volist> </block> <block name="footer"> @ThinkPHP2012 版权所有 </block>
As you can see, the extend tag is used in the subtemplate 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" />
or Use absolute file path to load
<extend name="./Template/Public/base.html" />
In the current subtemplate, 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 the following definition is used:
<block name="title"><title>{$title}</title></block> <a href="/" >首页</a> <a href="/info/" >资讯</a> <a href="/bbs/" >论坛</a>
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 basic template. If it is not redefined, it means that the block definition in the basic template will be used. If an empty block is defined, It means deleting the content of the block in the basic 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.
Related recommendations:
Geek Academy in-depth ThinkPHP framework video tutorial
The above is the detailed content of What is template inheritance in Thinkphp? Examples of template inheritance. For more information, please follow other related articles on the PHP Chinese website!