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