This article describes the solution to the Thinkphp template without parsing it directly output as is. Share it for your reference. The details are as follows:
1. Question:
I have been learning thinkphp template recently, but I found that the template page was as it was. After a hard search, I finally found the solution.
2. Solution:
Many people have encountered the same problem. The characters of __ROOT__, __PUBLIC__, and __APP__ contained in the string assigned in the variable are replaced with the real path when displayed in the template. That is, this problem was discovered when writing the Timi file management system.
Read the source code from the file and output it to the page and find that as long as the path character is TP, it is replaced with the real path.
for example:
The code copy is as follows: $this->assign('fileContent',$fileContent);//$fileContent contains the __PUBLIC__ "path constant" character.
The page is output as the real path /public/.
During this period, I have tried many things, such as base64 encryption when assigning, and decode it when outputting the template, but found that it did not work. Finally, I couldn't help but look at the source code of Tp and found that at the last step of the display method, I replaced the "Tp path constant" with the real path by calling the tag method. It's normal before the render method.
I was originally planning to change the source code to realize the following children's shoes requirements:
Boss, is this a temporary solution or a final solution?
But I think it's good to add a judgment to the assign() method.
If it is $this->assign('','',false), the content will not be replaced and the output will be as-is.
As a result, after reading this source code, I found that it was not that easy and the changes were too big.
The last reply from another children's shoes pointed out the final solution:
"You can refer to the content here: //www.VeVB.COM/article/54217.htm (template replacement)
With the template replacement rule, all __PUBLIC__ strings on the page will be replaced. If you really need to output __PUBLIC__ strings to the template, we can add the replacement rule, for example:
Copy the code as follows: 'TMPL_PARSE_STRING' =>array(
'--PUBLIC--' => '__PUBLIC__', // Use new rules to output/Public string
)
After adding the replacement rule in this way, if we want to output the __PUBLIC__ string, we only need to add --PUBLIC-- to the template, and the output method of other replacement strings is similar.
After adding the replacement rule in this way, if we want to output the __PUBLIC__ string, we only need to add --PUBLIC-- to the template, and the output method of other replacement strings is similar.
So, the plan was released:
Configure in Tp's configuration file config.php
Copy the code as follows: 'TMPL_PARSE_STRING' => array (//Path configuration
//Restore the Timi file path
'--PUBLIC--' => '__PUBLIC__',
'--APP--' => '__APP__',
'--URL--' => '__URL__',
'--ACTION--' => '__ACTION__',
'--SELF--' => '__SELF__',
'--INFO--' => '__INFO__',
'--EXT--' => '__EXT__'
),
When reading the source code, replace the "path constant character" __ROOT__ as --ROOT--:
The code copy is as follows: $fileContent=file_get_contents($filePath);
$fileContent=htmlspecialchars(preg_replace('/__(.*?)__/is','--$1--',$fileContent));
Then the TMPL_PARSE_STRING configuration is replaced when the template is parsed, as shown in the figure below:
Therefore, this problem was solved "temporarily" and "perfectly".
I hope this article will be helpful to everyone's ThinkPHP framework programming.