Laravel5 implements Template theme function _ PHP Tutorial

Source: Internet
Author: User
Laravel5 implements the template theme function. Laravel5 implements the template theme function. many cms have the template theme function. we can switch the theme through a configuration. how can this function be implemented under laravel? Today, let's look at laravel 5 to implement the template theme function.

Many cms have the template theme function. we can switch themes through a configuration. how can this function be implemented under laravel? We will discuss this issue today.

As we all know, laravel rendering templates are implemented through View: make (). you need to explicitly specify the template file path:

The code is as follows:


Function index ()
{
Return View: make ('index. index ');
}

In this case, we can implement the template topic function by ourselves. we only need to put the template file in the directory corresponding to the topic name. for example, if the default topic is default, let's write:

The code is as follows:


Function index ()
{
Return View: make ('default. index. index ');
}

Custom topic M:

The code is as follows:


Function index ()
{
Return View: make ('m M. index. index ');
}

Read the topic name from the configuration file:

The code is as follows:


Function index ()
{
Return View: make (Config: get ('app. theme ', 'default').'. index. index ');
}

In this way, the theme function of the template is basically implemented, but there is still a problem, that is, the custom topic must implement all templates of all default themes, otherwise some page template files will not be reported, further optimization:

The code is as follows:


Function index ()
{
$ Theme = Config: get ('app. theme ', 'default ');
$ Tpl = $ theme. '. index. index ';
If (! View: exists ($ tpl )){
$ Tpl = 'default. index. index ';
}
Return View: make ($ tpl );
}

Before rendering a template, check whether the template file exists. if the template file does not exist, use the template corresponding to the default topic.

So many lines of code can be further encapsulated. at this time, the Response object is used. we know that Response: view () is equivalent to View: make (), response also has a method Response: macro () which can be used to define a macro. we can encapsulate the logic in the macro:

The code is as follows:


Response: macro ('render', function ($ path, $ data = array ()){
$ Theme = Config: get ('app. theme ', 'default ');
$ Tpl = $ theme. '.'. $ path;
If (! View: exists ($ tpl )){
$ Tpl = 'default. '. $ path;
}
Return Response: view ($ tpl, $ data );
});

Usage:

The code is as follows:


Function index ()
{
$ Bindings = array (
'Title' => 'homepage'
);
Return Response: render ('index. Index', $ bindings );
}

Note that the variables passed in to the template must pass the second parameter Response: render.

Let's take a look at today's tutorials here. we will give you an in-depth analysis later and hope you will like them.

Http://www.bkjia.com/PHPjc/962929.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/962929.htmlTechArticlelaravel 5 realize template theme function many cms has template theme function, we can switch the theme through a configuration, how to implement this function under laravel? Let's explore today...

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.