Laravel5 implements the template theme function. laravel template_php tutorial

Source: Internet
Author: User
Laravel5 implements the template theme function, laravel template. Laravel5 implements the template theme function. as we all know in laravel templates, laravel rendering templates are implemented through View: make (). you need to explicitly specify the template file path: copy the code as follows: laravel 5 implements the template theme function, laravel Template

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/963120.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/963120.htmlTechArticlelaravel 5 to implement the template theme function, laravel template as we all know, laravel rendering template is achieved through View: make (), you need to explicitly specify the template file path: the code is as follows...

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.