9. Smarty3: Template inheritance

Source: Internet
Author: User
Tags php script smarty template

There are many sites using the same style of each page, but if each page is independent to develop template, the workload will be larger, even if the use of "copy" to modify the local way is not very ideal, if you want to modify the style of the page, each page to be modified. Template inheritance can solve this problem, as long as the parent template changes, the child template will change 1. Template inheritance using the {extends} function

Smarty template inheritance is very similar to object-oriented, which allows you to define one or more base templates for a child template inheritance. However, all files must be checked at runtime to modify the settings, and more inheritance is more expensive.

Use the {extends} label in Smarty to inherit the parent template from the template with the neutron template:
Note: The {extends} label used when inheriting the stencil must be used in the first row of the child template

#父类模版 (PARENT.TPL)-the base template

In addition to using {extends} in a child template file to inherit the parent template, you can also use the extends: template resource type to define the entire template inheritance tree in the PHP script when you invoke the display () function.

Using the template inheritance from the PHP script, an example of the output GRANDCHILD.TPL is as follows:

<?php
$smarty->display (' Extends:parent.tpl|child.tpl|grandchild.tpl ')
2. Overwrite part of the content area of the parent template in the stencil

Define a named template to inherit the source region with the {block} function, and then use the same {block} label in the child template to declare that a child template source will replace the corresponding region in the parent template.

You can declare one or more inherited source areas in the parent template, distinguished by the Name property of the {block}, while {block} can also be nested.

#父模版 (PARENT.TPL)-as the base template at the top of the template, two sets of source regions

Note: {block}, although declared in the parent template, does not affect the output of the parent template, only to be able to find and overwrite the zone source in the child template
If the child template inherits the parent template with the {extends} label, it can only contain the {block} label (content), and any other template content will ignore 3. Merge child templates and parent template {block} label contents

If you do not need to overwrite all of the area source content defined in the parent template, but need to make some adjustments to some of the content, any child, parent template {block} region can be combined. Samrty provides the following two methods. 3.1 Add or prepend, prepend option tag with append

Append and prepend function in the opposite direction, append is added to the parent template, and prepend is added to the front of the parent template, only the template is valid.

After adding to the parent template:

#父模版 (PARENT.TPL)-as the base template at the top of the parent template, a set of source regions

Add content to front of parent template:

#父模版 (PARENT.TPL)-as the base template at the top of the parent template, a set of source regions
3.2 Use {smarty.block.parent} or {smarty.block.parent} or {smarty.block.child} to retain variables as placeholders

You can use {$smarty. block.parent} to insert the {block} contents of the parent template into any location in the contents of the child {block}. You can also use {$smarty. block.child} To insert the child template {block} content anywhere in the contents of the value parent {block}

{$smarty. Block.child}:

#父模版 (PARENT.TPL)-as the base template at the top of the parent template, a set of source regions

Use the {$smarty. Block.parent} Example:

 #父模版 (PARENT.TPL)-as the base template at the top of the parent template, a set of source regions 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.