This article mainly introduces the solution to the Thinkphp template that does not parse the original output directly. It is a problem that many developers encounter and is very practical, for more information about the Thinkphp template, see the example in this article. Share it with you for your reference. The details are as follows:
I. Problems:
I recently learned thinkphp templates, but I found that the template page is as it is. After some hard work, I finally found a solution.
Ii. solution:
Many people encounter the same problem, including/Public,/index. php, which is replaced with the actual path when displayed in the template. This problem was found when writing the Timi file management system.
After the source code is read from the file and output to the page, it is found that all the TP path characters are replaced with the real path.
For example:
The Code is as follows:
$ This-> assign ('filecontent', $ fileContent); // $ fileContent contains the/Public "path constant" character.
The real path/public/is displayed after the page is output /.
Several attempts were made during this period, such as base64 encryption during assign and decode when the template is output. Finally, I couldn't help looking at the source code of Tp. I found that the "Tp path constant" was replaced with the actual path by calling the tag Method in the last step of the display method. It is normal before the render method.
I was prepared to change the source code to implement the following demand solution for the kids shoes:
Boss, is this a temporary solution or a final solution?
However, I think it is good to add a judgment in the assign () method,
If it is $ this-> assign ('','', false), the content is not replaced and output as is.
As a result, after reading the source code, we found that it was not so easy and the changes were too large.
The last response from another shoes showed the final solution:
"You can refer to the content here: http://www.jb51.net/article/54217.htm (template replacement)
With the template replacement rules, all the/Public strings on the page will be replaced. If you need to output the/Public string to the template, we can add a replacement rule, for example:
The Code is as follows:
'Tmpl _ PARSE_STRING '=> array (
'-- PUBLIC --' => '/Public', // use the new rule to output/Public String
)
After adding a replacement rule, if we want to output the/Public String, we only need to add -- PUBLIC -- to the template. The output method of other replacement strings is similar.
After adding a replacement rule, if we want to output the/Public String, we only need to add -- PUBLIC -- to the template. The output method of other replacement strings is similar.
As a result, the solution was released:
Configure in Tp configuration file config. php
The Code is as follows:
'Tmpl _ PARSE_STRING '=> array (// path Configuration
// Restore the Timi file path
'-- PUBLIC --' => '/public ',
'-- APP --' => '/index. php ',
'-- URL --' => '/index. php/article ',
'-- ACTION --' => '/index. php/Article/detail ',
'-- SELF --' => '/js-tutorial-10534.html ',
'-- INFO --' => '_ INFO __',
'-- EXT --' => '_ EXT __'
),
When reading the source code, replace "path constant character" with -- ROOT --:
The Code is as follows:
$ FileContent = file_get_contents ($ filePath );
$ FileContent = htmlspecialchars (preg_replace ('/__(.*?) _/Is ',' -- $1 -- ', $ fileContent ));
Then, the configuration of TMPL_PARSE_STRING is replaced when the template is parsed, as shown in:
As a result, this problem is solved "Temporarily" and "perfectly.
I hope this article will help you with ThinkPHP framework programming.