THINKPHP5 Template Layout

Source: Internet
Author: User
Tags html tags
The first way: Global configuration mode

You need to configure the open layout_on parameter (not open by default) and set the layout entry filename layout_name (default is layout)
This is in the application/config.php file.

' Template '  =>  [/
        /Open layout_on
        ' layout_on '     =>  True,
        //directory location
        ' Layout_name '   =>  ' layout/layoutname ',
        //Layout substitution variable
        ' layout_item '   =>  ' {__content__} '
]

Note:
In the configuration file ' Url_html_suffix ' => ' html ', this must be HTML, otherwise the template output will not be formatted correctly. I was accidentally configured as a JSON format result template output is all JSON format, even HTML tags are exported.
Then the controller, in fact, the controller does not need to have any configuration. If no one needs to output the content of even the empty controller is also possible.

<?php
namespace App\index\controller;
Use Think\controller;
Class Index extends Controller
{
    /**
     * @param string $name
    /Public Function index ()
    C26/>return $this->fetch ();
    }
? >

Layout template, the meaning is very clear. Yes, that's right. The middle part of the end of the head is separated by the rule of the template to import in.

{include file= ' Layout/header '/}
{__content__}
{include  file= ' Layout/footer '/}

The above is the code on the layoutname.html file because I configured the ' layout_name ' => ' layout/layoutname ' in the config file so his layout template file location is application/index/ View/layout/layoutname.html also has header and footer two files are in this folder.

Here's what this header file is about.

<!doctype html>

This is footer tail content.

This is the bottom information
</body>

The middle part is the controller's operation template, which is the current controller's index operation application/index/view/index/index.html

{//__nolayout__}
<div>
This is Layout template layout
middle content
</div>

{//__nolayout__} This is if you do not want to use the layout template in this template, {__nolayout__} can mask off the head and tail of the layout template. Double slash "//" I commented out the variable, and you need to get rid of the "//".

Next we look at the output:

Now open the {__nolayout__} variable to see what the result is.
Here I need to remind you to clear the cached file for runtime\temp this directory before you refresh the browser after you open {__nolayout__}.


Otherwise the refresh is not effective oh.

All right. Now you can really refresh the browser to see the results.

We can see the head and tail of the information are gone, indicating that the test has been successful. The Second way: template label mode

This method is much simpler, you do not need to set any parameters in the configuration file, do not need to open layout_on, directly in the template file to specify the layout template, the relevant layout template adjustment also in the template.

Take the previous output template as an example, this way the entrance is still in the index/index.html template, but we can modify the contents of the Add template file, add the following layout tag in the header (remember to first close the previous layout_on settings, otherwise the layout loop may occur):

{Layout name= "Layout/layoutname"/}

It means using application/index/view/layout/layoutname.html as a layout template

Now let's take a look at the code implementation of the template

{Layout name= "Layout/layoutname"/}
{//__nolayout__}
<div>
This is Layout template layout
middle content
</div>

It is emphasized here that no configuration is required in the middle of the configuration file except that header and footer two templates remain unchanged.

Next refresh to see how the results, the same refresh needs to empty the cache.

You can also specify a specific string to replace in the layout tag:

{Layout name= "layout/layoutname" replace= "[replace]"/}

Note: substitution characters cannot be used here with curly braces, or in brackets or anything else, even without parentheses. Otherwise it will not replace the content.

You can also set some variables in the template layout, such as:
Set title variable

{include file= "Layout/header"  title= "This is the layout pass title"/}
__replace__
{include  file= "layout/ Footer "/}

Then receive title in header file

<!doctype html>

The result is:

The title was changed.

There is a third way, I will not be introduced here. This method is to call the TP function set inside, the usage is also very simple and interested can go to see for yourself.

THINKPHP5 Template 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.