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.