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

What is template inheritance in Thinkphp? Examples of template inheritance

不言
不言Original
2018-07-24 17:49:101704browse

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 11606cc87d8fd89bcb19e3fd11c7bb4b36b196a5d42bcd29e331cb722979f9a6 tags. The following is a typical block design in the basic template (used for designing website titles):

网站标题

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:

{$web_title}

You can even load external files in the block:

Any template can be defined Multiple names identify non-repeating blocks. For example, a base.htmlbase template is defined below:




标题

菜单 左边分栏 主内容 右边分栏 底部

Then we use it in the subtemplate (actually the entry template for the current operation) Inheritance:


{$title}

首页
资讯
论坛




{$vo.title}
{$vo.content}
最新资讯: {$new.title}
@ThinkPHP2012 版权所有

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:

or Use absolute file path to load

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:

{$title}
首页
资讯
论坛

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!

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