Laravel5 implements the template theme function (continued). laravel template_php tutorial

Source: Internet
Author: User
Laravel5 implements the template theme function (continued) and laravel template. Laravel5 implements the template theme function (continued ), in the previous article, laravel templates I introduced how to dynamically change the template file path by defining the Response macro to achieve the theme laravel 5 and the template theme function (continued). laravel templates

In the previous article, I introduced how to dynamically change the template file path by defining the Response macro to implement the theme function: laravel implements the template theme function, however, I found that this method has a drawback. to use @ extends in a template, you must explicitly specify the template path, which may cause confusion. I decided to change my mind, A topic must be completely isolated from a topic. if the topic does not exist, it does not automatically search for an alternative template from another topic.

The previously defined response macro method can be implemented, but I decided to use a more standardized method.

Laravel's View class has a method View: addNamespace, which is mentioned in the "development extension package" section of the manual. I have to say that the layout logic of the Laravel manual is chaotic, this method should be described in the "View" section. if you leave it blank, let's talk about this method first.

Laravel rendering view can be written as follows:

The code is as follows:
View: make ('namespace: path ');
// For example, View: make ('default: index. index ');

This method is used to define the namespace:

The code is as follows:
View: addNamespace ('default', app_path (). '/views/default ');

Smart Friends may already feel that this feature can help us implement template theme, for example:

The code is as follows:
// Register the blue subject
View: addNamespace ('blue', app_path (). '/views/blue ');
// Register a red topic
View: addNamespace ('red', app_path (). '/views/Red ');
// Register a green topic
View: addNamespace ('green', app_path (). '/views/green ');

Call later:

The code is as follows:
// Render the index. index template under the green topic
View: make ('green: index. index ');

However, we need to first register the path mappings of these topics through the View: addNamespace method, and explicitly specify the namespace during rendering.

I don't think it is very convenient. can't View set a default namespace? In this way, we only need to set one time, for example:

The code is as follows:
// We can write this in _ construct.
View: setDefaultNamespace ('blue', app_path (). '/views/blue ');

After:

The code is as follows:
// It is actually equivalent to View: make ('Blue: index. index ');
View: make ('index. index ');

Furthermore, we can set a topic in the background, write the topic name into the database, and read and set the topic in the foreground:

The code is as follows:
// Assume that the configuration is read from the database, and Option is a model class.
$ Theme = Option: getByKey ('theme ');
View: setDefaultNamespace ($ theme, app_path (). '/views/'. $ theme );

In this way, the background switch topic is implemented.

Unfortunately, View does not have the setDefaultNamespace method. Therefore, I decided to create a project dedicated to the core class library extension for laravel. this function has been implemented. you can check my Project: Project address, go to src/Keepeye/Laravel/View/to View the usage method.

Now, we will discuss the implementation of the theme feature of laravel template. I hope you will like it.

Http://www.bkjia.com/PHPjc/963119.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/963119.htmlTechArticlelaravel 5 implement template theme function (continued), laravel template in the previous article I introduced through the definition of Response macro way to achieve dynamic change of the template file path to achieve theme...

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.