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 ).
- 'Layout _ on' => true,
- 'Layout _ name' => 'layout ',
After the Copy code enables LAYOUT_ON, our template rendering process changes. for example:
- Class UserAction extends Action {
- Public function add (){
- $ This-> display ('ADD ');
- }
- }
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:
-
- {__ 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:
- '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:
- '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
-
- Id = "main" class = "main">
- {__ CONTENT __}
-
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:
- Class UserAction extends Action {
- Public function add (){
- Layout (true );
- $ This-> display ('ADD ');
- }
- }
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:
- Class UserAction extends Action {
- Public function add (){
- Layout ('layout/newlayout ');
- $ This-> display ('ADD ');
- }
- }
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 ):
- Class UserAction extends Action {
- Public function add (){
- Layout (false); // temporarily disable the layout function of the current template
- $ This-> display ('ADD ');
- }
- }
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.