Laravel 5 Implementing the template Theme feature (cont.), Laravel template
In a previous article, I introduced the way to dynamically change template file paths to implement theme functionality by defining response macros: Laravel implement template Theme functionality, but later I found that this approach has a disadvantage, using @extends in the template must explicitly specify the template path, This can cause confusion, I decide to change the mind, the subject and the subject should be completely isolated, nonexistent or nonexistent, and do not automatically go to another topic to find the replacement template.
The way I originally defined response macros was possible, but I decided to use a more canonical approach.
Laravel's view class has a method View::addnamespace, this method in the manual "Development expansion Package" section mentioned, have to say laravel manual layout logic Confusion, this method should be placed in the "View" chapter is, the topic is not said, Let's talk about this method first.
The Laravel rendering view has a notation:
Copy the Code code as follows:
View::make (' namespace::p ath ');
For example View::make (' Default::index.index ');
How to define namespace, is through this method:
Copy the Code code as follows:
View::addnamespace (' Default ', App_path (). ' /views/default ');
Smart friends may already feel that this feature can help us to achieve the theme of the template, such as:
Copy the Code code as follows:
Sign up for Blue themes
View::addnamespace (' Blue ', App_path (). ' /views/blue ');
Register a red theme
View::addnamespace (' Red ', App_path (). ' /views/red ');
Registering a green theme
View::addnamespace (' Green ', App_path (). /views/green ');
Then call:
Copy the Code code as follows:
Render the Index.index template under the green theme
View::make (' Green::index.index ');
However, we need to register the path mappings of these topics in advance through the View::addnamespace method, and we need to explicitly specify namespace when rendering.
I do not feel very convenient, can't view set a default namespace? So we just have to set it up once, for example:
Copy the Code code as follows:
We can write this down in __construct.
View::setdefaultnamespace (' Blue ', App_path (). ' /views/blue ');
After:
Copy the Code code as follows:
actually equivalent to View::make (' Blue::index.index ');
View::make (' Index.index ');
Further, we can set the theme in the background, write the theme name into the database, the foreground read and set the theme:
Copy the Code code as follows:
Assuming that the configuration is read from the database, option is the model class
$theme = Option::getbykey (' theme ');
View::setdefaultnamespace ($theme, App_path (). ' /views/'. $theme);
This will enable the background switch theme.
Unfortunately, view does not have a setdefaultnamespace method, so I decided to create a project specifically for the core Class library extension for Laravel, a feature that has been implemented to view my project: Project address, in Src/keepeye/laravel /view/See how to use it.
Well, about the Laravel template theme feature implementation, we have explored here, I hope you can enjoy.
http://www.bkjia.com/PHPjc/963119.html www.bkjia.com true http://www.bkjia.com/PHPjc/963119.html techarticle Laravel 5 Implementing the template Theme feature (cont.), Laravel template in a previous article I introduced a way to dynamically change the template file path to achieve the theme by defining response macros ...