Layout improvement of TP template Layout

Source: Internet
Author: User
Provides various official and user-released code examples. For code reference, you are welcome to exchange and learn. When using Layout, TP only provides a single placeholder _ CONTENT _ Layout. If you want to temporarily add some content to a special location, you have to define another layout file. I added placeholder Resolution on the original basis. You can easily insert code at the desired location
When Layout is used, TP only provides the replacement of a single placeholder _ CONTENT _ Layout. If you want to temporarily add some content to a special location, you have to define another layout file. I added placeholder Resolution on the original basis. You can easily insert code at the desired position. The original TP layout method is still available. The Code is as follows: // Modify and replace the parseLayout layout Parsing Method in the ThinkPHP/Library/Think/Template class.
// Use the regular expression to match the placeholder region and replace the placeholder specified by the placeholder feature name in the layout.
// Placeholder format: {__ placeholder name __}
Protected function parseLayout ($ content ){
// Read the layout label in the template
$ Find = preg_match ('/'. $ this-> config ['taglib _ in in']. 'layout \ s (. + ?) \ S *? \/'. $ This-> config ['taglib _ end'].'/is ', $ content, $ matches );
If ($ find ){
// Replace the Layout tag
$ Content = str_replace ($ matches [0], '', $ content );
// Parse the Layout tag
$ Array = $ this-> parseXmlAttrs ($ matches [1]);
If (! C ('layout _ on') | C ('layout _ name ')! = $ Array ['name']) {
// Read the layout Template
$ LayoutFile = THEME_PATH. $ array ['name']. $ this-> config ['template _ suffix '];
$ Pattern = '/'. $ this-> config ['taglib _ begin']. 'placeholder \ s ([^ '. $ this-> config ['taglib _ end']. '] *?) '. $ This-> config ['taglib _ end'].' \ s (. + ?) \ S *? '. $ This-> config ['taglib _ begin'].' \/placeholder '. $ this-> config ['taglib _ end'].'/is ';
$ Result = preg_match_all ($ pattern, $ content, $ matches );
If ($ result ){
$ Layout = file_get_contents ($ layoutFile );
Foreach ($ matches [0] as $ index => $ placeholder ){
$ Array = $ this-> parseXmlAttrs ($ matches [1] [$ index]);
$ Replace = '{__'. $ array ["name"]. "__}";
$ Layout = str_replace ($ replace, $ matches [2] [$ index], $ layout );
}
$ Content = $ layout;
} Else {
$ Replace = isset ($ array ['replace '])? $ Array ['replace ']: $ this-> config ['layout _ item'];
// Replace the subject content of the layout
$ Content = str_replace ($ replace, $ content, file_get_contents ($ layoutFile ));
}
// Replace the empty placeholder of the template
$ Content = preg_replace ('/{__ \ w +? __}/', '', $ Content );
}
} Else {
$ Content = str_replace ('{__ NOLAYOUT __}', '', $ content );
}

Return $ content;
}
Instance used:
Layout file:
{__HEADER__}
{__CONTENT__}
{__FOOTER__}
Application:


In this way, you can define any placeholder in your layout file so that you can insert code at the desired position.

AD: truly free, domain name + VM + enterprise mailbox = 0 RMB

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.