PHP Smarty Template template inheritance {extends}

Source: Internet
Author: User
Tags smarty template

{extends}
In template inheritance, you can use the {extends} tag within a child template to extend the parent template.
1.{extends} must be placed on the first line of the template.
2. If the child template is to extend the parent template with {extends}, it can only have a region of {block}.

3. Any other template content will be ignored.

To extend the use of files outside of $template_dir, use the syntax of the template resource.

Note
When you extend a parent template name to a variable such as {extends file= $parent _file},

Make sure that the $parent_file variable is placed in the same $cache _id. Otherwise, Smarty cannot discern different $parent_file variables.

parent.php page

<?php//Create Smarty Object require_once './libs/smarty.class.php ';//define root directory define (' root ', str_replace ("\ \", "/", DirName (__ file__)). " /");//Instantiate Smarty Class $smarty=new smarty ();//Set delimiter $smarty->left_delimiter=" <{"; $smarty->right_delimiter="} > ";//set to false the bounding symbol can have spaces around $smarty->auto_literal = false;//Add a plugin to the directory//$smarty->setpluginsdir (ROOT.") /libs/myplugins/");//pay attention to adding a plug-in, to add the system default path to add the default system can not use the plug-in $smarty->setpluginsdir (" ROOT. " /libs/plugins/",//system default set path ROOT." /libs/myplugins/",//Custom));//variable Output $smarty->display (' Parent.tpl ');? >

child.php page

<?php//Create Smarty Object require_once './libs/smarty.class.php ';//define root directory define (' root ', str_replace ("\ \", "/", DirName (__ file__)). " /");//Instantiate Smarty Class $smarty=new smarty ();//Set delimiter $smarty->left_delimiter=" <{"; $smarty->right_delimiter="} > ";//set to false the bounding symbol can have spaces around $smarty->auto_literal = false;//variable output $smarty->display (' Child.tpl ');? >

PARENT.TPL page

<!    DOCTYPE html>

CHILD.TPL page

<{extends ' parent.tpl '}><{block name= "One"}> I was the first block<{/block}><{block name= "two"}> I am the second block<{/block}><{block name= "three" Append}> Jin Sha Harbor <{/block}><{block name= "four" prepend} > I love you, <{/block}><{block name= "Five"}> do you like to see <{$smarty .block.parent}>? <{/block}><{ Block Name= "Six"}> Shanghai <{/block}>

Browser output (child.php page output)

I'm the second block. I love you, China. Do you like watching movies? I'm going to Shanghai to go to college.

Template inheritance
Inheritance is a concept that comes from object-oriented programming.

Template inheritance allows you to define one or more parent templates that are provided to the child template for extension.

Extended inheritance means that a child template can overwrite a block area of some or all of the parent templates.

The inheritance structure can be multi-layered, so you can inherit from a file, and this file inherits from other files, and so on.

A child template cannot define anything beyond the {block} block of the parent template.

Any content outside of {block} will be automatically ignored.

The {block} contents in the child template and parent template can be merged by append and Prepend.

{block} option, and {$smarty. Block.parent} or {$smarty. Block.child} will hold these contents.

Template inheritance is compiled into a single compiled file at compile time.

The {include}, which has a similar contrast effect, contains template functionality, and the performance of the template inheritance is higher.

Child template inheritance uses the {extends} tag, which must be placed in the first row of the child template .

Another approach is to inherit the entire template from the tree, and when the PHP program calls fetch () or display (),

With extends: the template resource type, this approach has greater flexibility.

Note
When $compile_check is turned on, all the files in the inheritance tree will be checked for changes every time they are called.

Therefore, you'd better close the $compile_check in a production environment.

Note
If your child template is useful for {include} to contain the template, and the contained template contains a {block} zone for the {include} template call, you need to place an empty {block} in the top-most parent template as an inheritance.

This article is from the "Jin Sha Harbor" blog, please be sure to keep this source http://11410485.blog.51cto.com/11400485/1844354

PHP Smarty Template template inheritance {extends}

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.