Thinkphp Template page output problem solution

Source: Internet
Author: User
Tags constant

Many people encounter the same problem. The _ ROOT _, _ PUBLIC _, and _ APP __characters in the strings assigned in the variable are, when displayed in the template, the actual path is replaced. 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:


$ 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://doc.thinkphp.cn/manual/template_replace.htm l (Template replacement)

 

With the template replacement rules, all the _ PUBLIC _ strings on the page will be replaced. If The _ PUBLIC _ string needs to be output to the template, we can add replacement rules, for example:

'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.

7. After adding a replacement rule, 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.

As a result, the solution was released:

Configure in Tp configuration file config. php

'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 __'
),

Replace "path constant character" _ ROOT _ with -- ROOT --:


$ 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 a result, this problem is solved "temporarily" and "perfectly.

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.