Laravel 5 Implementation Template Theme function _php instance

Source: Internet
Author: User

As we all know, the Laravel rendering template is implemented through View::make (), and you need to explicitly specify the template file path:

Copy Code code as follows:

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

In this way, we can implement the template theme function, we just need to put the template file in the directory corresponding to the title of the line, such as default theme of the word, we wrote:

Copy Code code as follows:

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

Customize Theme Custom:

Copy Code code as follows:

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

To read the topic name from the configuration file:

Copy Code code as follows:

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

This basically realizes the template theme function, but there is still a problem, that is, custom theme must implement all default theme of all templates, otherwise it will cause some page template files do not have an error, then further optimization:

Copy Code code 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);
}

is to detect the existence of a template file before rendering it, and then use the corresponding template in the default theme.

So many lines of code, we can continue to encapsulate, this time to use the Response object, we know that Response::view () is equivalent to View::make (), and Response also has a method Response::macro () Method can be used to define a macro, we can encapsulate logic into a macro:

Copy Code code 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);
});

Use:

Copy Code code as follows:

Function index ()
{
$bindings = Array (
' title ' => ' home '
);
Return Response::render (' Index.index ', $bindings);
}

Note that the variables passing through the template have to pass the second parameter of the Response::render.

Today's tutorial on the first here, the follow-up we come to in-depth analysis, I hope you can enjoy.

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.