Motherboard Polar _extends and block tags in smarty

Source: Internet
Author: User

Template inheritance is the concept of object-oriented programming, and 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, which is more flexible when the PHP program calls fetch () or display (), using the extends: template resource type.

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 you have a template with {include} in it that contains templates, and the included template contains a {block} zone that is called by the {include} template, you will need to place an empty {block} as an inheritance in the top-most parent template.

Example 17.6. Template Inheritance Examples

LAYOUT.TPL (parent template)

<HTML><Head>  <title>{Block Name=title} default page title {/block}</title>{block Name=head}{/block}</Head><Body>{block Name=body}{/block}</Body></HTML>

MYPROJECT.TPL (sub-template)

{extends file= ' layout.tpl '}{block name=head}   <  href= "/css/mypage.css"  rel= "stylesheet"  type= "Text/css "/>  <src="/js/mypage.js "></  script>{/block}

Mypage.tpl (grandson Template)

{extends file= ' myproject.tpl '}{block name=title} my page title {/block}{block Name=head}   <  href= "/css/mypage.css"  rel= "stylesheet"  type= "Text/css "/>  <src="/js/mypage.js "></  script>{/block}{block name=body} My HTML page content is here {/block}

Show the above template

$smarty->display (' Mypage.tpl ');

The page will output:

<HTML><Head>  <title>My page title</title>  <Linkhref= "/css/mypage.css"rel= "stylesheet"type= "Text/css"/>  <Scriptsrc= "/js/mypage.js"></Script></Head><Body>The content of my HTML page is here</Body></HTML>

Example 17.7. Use extends: To perform template inheritance

Instead of using {extends} inheritance in a template, you can inherit by defining an inheritance tree within PHP, using the extends: resource type.

The following code returns the same result as the example above:

<? PHP $smarty->display (' extends:layout.tpl|myproject.tpl|mypage.tpl '?>

Motherboard Polar _extends and block tags in smarty

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.