Laravel5 template theme function (continued) _ PHP Tutorial

Source: Internet
Author: User
Laravel5 implements the template theme function (continued ). Laravel5 template theme function (continued) in the previous article, we briefly discussed the laravel template theme function. in this article, we will continue to explore the implementation of the laravel template theme function, implement the template theme function in laravel 5 (continued)

In the previous article, we briefly discussed the theme feature of laravel templates. in this article, we will continue to discuss the implementation of the theme feature of laravel templates. this implementation is very important and you are interested to take a look.

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 );

Http://www.bkjia.com/PHPjc/962930.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/962930.htmlTechArticlelaravel 5 Implementation Template theme function (continued) in the previous article, we briefly discuss the laravel template theme function, this article we continue to explore laravel template theme function implementation, this implementation...

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.