Home  >  Article  >  Web Front-end  >  Solution to the problem that Thinkphp template does not parse and output directly as it is_javascript skills

Solution to the problem that Thinkphp template does not parse and output directly as it is_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:32:323405browse

The example in this article describes the solution to the problem that the Thinkphp template is not parsed and directly outputs it as it is. Share it with everyone for your reference. The details are as follows:

1. Question:

I was learning thinkphp templates recently, but I found that the template page came out as it was. After some hard searching, I finally found the solution.

2. Solution:

Many people have encountered the same problem. The __ROOT__, __PUBLIC__, and __APP__ contained in the string assigned to the variable are replaced with real paths when displayed in the template. I discovered this problem while writing the Timi file management system.

After reading the source code from the file and outputting it to the page, I found that as long as it is a TP path character, it has been replaced with a real path.
For example:

Copy code The code is as follows:
$this->assign('fileContent',$fileContent); //$fileContent contains __PUBLIC__ "path constant" characters.


After the page is output, it is displayed as the real path /public/.
During this period, I went through many attempts, such as base64 encryption when assigning, decoding when outputting the template, and found that it didn't work. Finally, I couldn't help but look at the source code of Tp, and found that in the last step of the display method, the "Tp path constant" was replaced with the real path by calling the tag method. Everything is normal before the render method.

I originally planned to change the source code to implement the following solution for children’s shoes:
Boss, is this a temporary solution or a final solution?
But I think it’s not bad to add a judgment in the assign() method,
If it is $this->assign('','',false), the content will not be replaced and will be output as is.

As a result, after reading this source code, I realized that it was not that easy and the changes were too big.

The last reply from another child pointed out the final solution:

"You can refer to the content here: http://www.jb51.net/article/54217.htm(template replacement)

With the template replacement rule, all __PUBLIC__ strings on the page will be replaced. If we really need to output the __PUBLIC__ string to the template, we can add replacement rules, for example:

Copy code The code is 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-- in the template. The output method of other replacement strings is similar.

After adding the replacement rules in this way, if we want to output the __PUBLIC__ string, we only need to add --PUBLIC-- in the template. The output method of other replacement strings is similar.

So, the plan was released:

Configure in Tp’s configuration file config.php

Copy code The code is as follows:
'TMPL_PARSE_STRING' => array (//path configuration
                         
//Timi file path restoration
'--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__ with --ROOT--:

Copy code 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 just replaced when the template is parsed, as shown in the figure below:

Ever since, this problem has been "temporarily" and "perfectly" solved.

I hope this article will be helpful to everyone’s ThinkPHP framework programming.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn