Decrypting ThinkPHP3.1.2 template inheritance _ php instance

Source: Internet
Author: User
This article mainly introduces ThinkPHP3.1.2 template inheritance. If you need it, you can refer to the template inheritance method which is a more flexible template layout method added by ThinkPHP3.1.2. The template inheritance method is different from the template layout, it should be at the upper layer of the template layout. Template inheritance is not difficult to understand. Just like class inheritance, templates can also define a basic template (or layout) and define related blocks ), then, you can reload the blocks defined in the basic template in the subtemplate of the basic template.

Therefore, the advantage of template inheritance is to design blocks in the Basic Template and replace these blocks in the subtemplate.
Each block is composed Tags, and block tag Nesting is not supported.
Below is a typical block design in the Basic Template (used to design the website title ):

 
  Website title
 

The block tag must specify the name attribute to identify the name of the current block. This identifier should be unique in the current template. The block tag can contain any template content, including other labels and variables, for example:

 
  {$web_title}
 

You can even load external files in the block:

 
  
 

In a template, you can define multiple block with unique names. For example, you can define a base.html basic template as follows:

  
  
 
  Title
    
 
  
Menu
  
 
  
Left column
  
 
  
Main Content
  
 
  
Right column
  
 
  
Bottom
   

Then we use inheritance in the subtemplate (actually the portal template for the current operation:

  
 
  {$ Title}
  
 
  
Homepage information forum
  
  
  
  
   
{$ Vo. title}
   
{$ Vo. content}

Latest information:

 
 
  
{$ New. title}
  
@ ThinkPHP2012 Copyright

As you can see, the sub-template uses the extend tag to define the template to be inherited. The extend tag usage is the same as the include tag, you can also load other templates:

 

Or use the absolute file path to load

 

In the current subtemplate, only blocks can be defined, but other template content cannot be defined. Otherwise, blocks already defined in the basic template can be defined.
For example, if the following definition is used:

 
  {$ Title}
 Homepage information forum

The navigation part will be invalid and will not be displayed in the template.

In the subtemplate, You can reload the block definition in the basic template. if the definition is not redefined, the block definition in the basic template is used. If an empty block is defined, the block content in the basic template is deleted.
In the preceding example, the content of the left Block is deleted, and other blocks are reloaded.
The block definition sequence in the subtemplate is random. The key to the use of template inheritance lies in the layout and design planning of the basic template. if combined with the original layout function, it will be more flexible.

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.