PHP random 10-thinkphp 3.1.3 Template inheritance Layout

Source: Internet
Author: User

8.25 Template Inheritance

Template inheritance is a more flexible template layout that is added in version 3.1.2, template inheritance is different 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 consists of a <block></block> tag and does not support nesting of block tags.
Here is a typical chunk design in the base template (for designing the site title):

<name= "title"><title> site title </ title ></ Block >

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:

<name= "title"><title>{$web _title}  </title></block>

You can even load external files in chunks:

<name= "include"><file= "Public:header"  /></block>

A template can define any number of names to identify distinct chunks, such as a base.html base template defined below:

    <HTML>    <Head>    <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8">    <Blockname= "title"><title>Title</title></Block>    </Head>    <Body>    <Blockname= "Menu">Menu</Block>    <Blockname= "Left">Left column</Block>    <Blockname= "Main">Main content</Block>    <Blockname= "Right">Right column</Block>    <Blockname= "Footer">Bottom</Block>    </Body>    </HTML>

We then use inheritance in the child template (which is actually the entry template for the current operation):

    <Extendname= "Base" />    <Blockname= "title"><title>{$title}</title></Block>    <Blockname= "Menu">    <ahref="/" >Home</a>    <ahref= "/info/" >Information</a>    <ahref= "/bbs/" >Forum</a>    </Block>    <Blockname= "Left"></Block>    <Blockname= "Content">    <volistname= "List"ID= "Vo">    <ahref= "/new/{$vo. ID}">{$vo. Title}</a><BR/>{$vo. Content}</volist>    </Block>    <Blockname= "Right">Latest News:<volistname= "News"ID= "new">    <ahref= "/new/{$new. ID}">{$new. Title}</a><BR/>    </volist>    </Block>    <Blockname= "Footer">@ThinkPHP2012 All rights reserved .</Block>

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:

<name= "public:base"/>

Or use an absolute file path to load

<name= "./tpl/public/base.html"/>

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:

    <Blockname= "title"><title>{$title}</title></Block>    <ahref="/" >Home</a>    <ahref= "/info/" >Information</a>    <ahref= "/bbs/" >Forum</a>

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.

PHP random 10-thinkphp 3.1.3 Template inheritance Layout

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.