The dynamic include file in the ThinkPHP template supports variable parsing.

Source: Internet
Author: User
Provides various official and user-released code examples and code reference. You are welcome to exchange and learn how to dynamically reference the File include to support variable parsing.
Today, the instance uses a method that requires dynamic include files, but thinkPHP does not support dynamic parsing.
.


Use Cases: After a user logs on, different display pages are called based on different levels. Therefore, files need to be dynamically introduced. thinkPHP does not support dynamic introduction, so you have to find a solution.


How should I use it?
:



The above is the file corresponding to the database.

The next step is to call different levels and different menu files after the user successfully logs on, such as menu1.tpl or menu2.tpl.
But thinkPHP does not support dynamic reference. What should I do?
My personal solution:
Figure:





Now, where does the level come from? Is injected in controller
See the figure below:




In order to give you a better understanding, I posted the data stored in the session for you to see: It is not clear what is going on


You need to modify \ ThinkPHP \ Library \ Think \ Template. class. php to correspond to the parseInclude () method: specific modification:






Edit: \ ThinkPHP \ Library \ Think \ Template. class. php Code as follows: Protected function parseInclude ($ content, $ extend = true ){
// Resolution inheritance
If ($ extend)
$ Content = $ this-> parseExtend ($ content );
// Parse the Layout
$ Content = $ this-> parseLayout ($ content );
$ Begin = $ this-> config ['taglib _ in in'];
$ End = $ this-> config ['taglib _ end'];
// Read the include tag in the template
$ Find = preg_match_all ('/'. $ begin. 'include \ s (. + ?) \ S *? \/'. $ End.'/is ', $ content, $ matches );
If ($ find ){
For ($ I = 0; $ I <$ find; $ I ++ ){
$ Include = $ matches [1] [$ I];
$ Array = $ this-> parseXmlAttrs ($ include );
$ File = $ array ['file'];
// $ View = Think: instance ('Think \ view ');
/*
* Explanation
* ThinkPHP cannot dynamically include files.
* Here we need to make some adjustments.
* Check whether append exists.
***/
If (isset ($ array ['append']) {
If (isset ($ array [isvar]) & $ array [isvar] = "true "){
$ Append = $ this-> get ($ array ['append']);
Unset ($ array ['isvar']);
} Else {
$ Append = $ array ['append'];
}
$ File = $ file. $ append;
Unset ($ array ['append']);
}
If (isset ($ array ['replace ']) {
If (isset ($ array [isVar]) & $ array [isVar] = "true "){
$ File = $ this-> get ($ array ['replace ']);
Unset ($ array ['isvar']);
} Else {
$ File = $ array ['replace '];
}
Unset ($ array ['replace ']);
}
Unset ($ array ['file']);
$ Content = str_replace ($ matches [0] [$ I], $ this-> parseIncludeItem ($ file, $ array, $ extend), $ content );
}
}
Return $ content;
}
A friend who needs to take it away !!!!!!!!!!!



Another approach: http://www.thinkphp1.cn/code/1261.html

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.