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