Home > Backend Development > PHP Tutorial > What is template inheritance in Thinkphp? Examples of template inheritance

What is template inheritance in Thinkphp? Examples of template inheritance

不言
Release: 2023-04-03 11:58:02
Original
1868 people have browsed it

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>
Copy after login

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>
Copy after login

You can even load external files in the block:

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

Any template can be defined Multiple names identify non-repeating blocks. For example, a base.htmlbase 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 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>
Copy after login

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" />
Copy after login

or Use absolute file path to load

<extend name="./Template/Public/base.html" />
Copy after login

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

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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template