ThinkPHP3.1 quick start (15) template layout

Source: Internet
Author: User
The ThinkPHP template engine has built-in layout template function support, allowing you to easily implement Template layout and layout nesting. Three layout templates are supported: the ThinkPHP template engine has built-in layout template function support, allowing you to easily implement Template layout and layout nesting.
Three layout templates are supported:

The first method is global configuration. you only need to add the relevant layout template configuration in the project configuration file to implement the template layout function. this method is more suitable for the whole site to use the same layout, you must enable the LAYOUT_ON parameter (disabled by default) and set the layout entry file name LAYOUT_NAME (layout by default ).
  1. 'Layout _ on' => true,
  2. 'Layout _ name' => 'layout ',
After the Copy code enables LAYOUT_ON, our template rendering process changes. for example:
  1. Class UserAction extends Action {
  2. Public function add (){
  3. $ This-> display ('ADD ');
  4. }
  5. }
The copy code directly renders the Tpl/User/add.html template file before the LAYOUT_ON layout template is enabled. after the template is enabled, the Tpl/layout.html layout template is first rendered:
  1. {__ CONTENT __}
After the code is copied to read the layout template, it parses the User/add.html template file and replaces the parsed CONTENT with the {__content __} specified string in the layout template file.
Of course, you can change this specific replacement string through settings, for example:
  1. 'Tmpl _ LAYOUT_ITEM '=>' {__ REPLACE __}'
Copy the code, but a layout template can only have a specific replacement string.
In this layout mode, once the User/add.html template file layout.html layout template file is modified, the template will be re-compiled.
To specify a layout template for another location, you can use:
  1. 'Layout _ name' => 'layout/layoutname ',
Copy the code to use Tpl/Layout/layoutname.html as the Layout template.

If some pages do not require the layout template function, you can add the {__nolayout __} string at the beginning of the template file.
If the preceding User/add.html template file contains {__ NOLAYOUT __}, the layout template will not be parsed even if the layout template is enabled.

Method 2: Template label. this layout template does not need to set any parameters in the configuration file or enable LAYOUT_ON. you can directly specify the layout template in the template file, the relevant layout template is also adjusted in the template.
The preceding output template is used as an example. the entry of this method is still in the User/add.html template, but we can modify the content of the add template file and add the following layout tag in the header ( Remember to disable the previous LAYOUT_ON settings first, otherwise layout loops may occur.):
The copy code example uses the layout.html layout template file as the template file, and the layout template file is written in the same way as the preceding method. When rendering the User/add.html template file, if the layout tag is read, the parsing CONTENT of the current template is replaced with the specific string of {__content __} in the layout template.
Only one layout template can be used in a template file. If no layout tag is used in the template file, the current template does not use any layout.
To use other layout templates, you can change the name attribute of layout, for example:
The copy code can also specify the specific string to be replaced in the layout tag:
Copy the code. because all the files introduced by the include tag support the layout tag, we can use the combination of the layout tag and the include tag to implement nesting of the layout template. For example
  1. Id = "main" class = "main">
  2. {__ CONTENT __}
  3.  
Copy the code to add the layout tag in the imported header and footer template files. for example, add the following tag at the beginning of the header template file:
Copy the code to reference the menu layout template in the header template.
You can also combine the two la S to implement more complex template la S and nested functions.

Method 3: Use layout to control the layout of the template. Use the built-in layout method to control the layout of the template output in the program more flexibly. this method is especially suitable for scenarios where layout needs to be laid out or closed, in this way, you do not need to enable LAYOUT_ON in the configuration file. For example:
  1. Class UserAction extends Action {
  2. Public function add (){
  3. Layout (true );
  4. $ This-> display ('ADD ');
  5. }
  6. }
Copy the code to enable the layout template for the current template output and use the default layout template.
If different layout templates are required for the current output, you can dynamically specify the layout template name, for example:
  1. Class UserAction extends Action {
  2. Public function add (){
  3. Layout ('layout/newlayout ');
  4. $ This-> display ('ADD ');
  5. }
  6. }
Copy the code or use the layout method to dynamically disable the layout function of the current template (this method can be used in combination with the first layout method. for example, if the layout is enabled for the global configuration, you can disable it on a specific page ):
  1. Class UserAction extends Action {
  2. Public function add (){
  3. Layout (false); // temporarily disable the layout function of the current template
  4. $ This-> display ('ADD ');
  5. }
  6. }
Copy code
To sum up the three template layout modes, the first and third are to configure and implement the template layout in the program, and the second is to simply use the template label in the template layout. The specific method of selection depends on the actual situation of the project.

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.