Template inheritance is a more flexible template layout that is added to the ThinkPHP3.1.2 version, where template inheritance differs from template layout and, even, should be on top of the template layout. Template inheritance is not difficult to understand, just like the inheritance of a class, a template can also define a base template (or layout), where the relevant block (block) is defined, and then inherit (extend) the template in the base templates can be overloaded with the blocks defined in the underlying template.
Thus, the advantage of template inheritance is that the blocks and sub-templates in the design base template replace these chunks.
Each chunk is made up of labels and does not support nesting of block tags.
Here is a typical chunk design in the base template (for designing the site title):
Site Title
The block tag must specify the Name property to identify the current chunk, which should be unique in the current template, and the block tag can contain any template content, including other tags and variables, such as:
{$web _title}
You can even load external files in chunks:
A template can define any number of names to identify distinct chunks, such as a base.html base template defined below:
Title
Sidebar on the
left
side of
the menu
main content
right column
Bottom
We then use inheritance in the child template (which is actually the entry template for the current operation):
{$title}
Home Information Forum
{$vo. Title}
{$vo. Content}
Latest News:
{$new. Title}
@ThinkPHP2012 All rights reserved.
As you can see, the extend tag is used in the child template to define the template that needs to be inherited, and the use of the extend tag is the same as the include tag, and you can load other templates:
Or use an absolute file path to load
In the current sub-template, only chunks can be defined and other template content cannot be defined, otherwise they will be ignored directly, and only chunks already defined in the underlying template can be defined.
For example, if you use the following definition:
{$title}
Home Information Forum
The navigation section will be invalid and will not appear in the template.
In a child template, you can overload the definition of chunks in the underlying template and, if not redefined, follow the chunk definition in the underlying template, or delete the chunk content from the underlying template if an empty chunk is defined.
In the example above, we have deleted the contents of the left chunk, and the other chunks have been overloaded.
The order of chunk definitions in a child template is arbitrary, and the key to the use of template inheritance is how the base template is laid out and designed, and is more flexible if combined with the original layout functionality.