The Thinkphp template does not parse the original output solution. The thinkphp Template

Source: Internet
Author: User

The Thinkphp template does not parse the original output solution. The thinkphp Template

The example in this article describes how the Thinkphp template does not parse the original output directly. 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. 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:

Copy codeThe 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.bkjia.com/article/54217.htm (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:
Copy codeThe 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
Copy codeThe Code is 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 __'
),
Replace "path constant character" _ ROOT _ with -- ROOT --:

Copy codeThe 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.


Thinkphp saves a Page code written in HTML + PHP into the database and then reads the result. HTML code parses the PHP Code and is directly output.

You can use widgets to render the output.
Create a new file named TestWidget. class. php under Lib/Widget:
Code
<? Php
Class TestWidget extends Widget {

Public function render ($ data ){
Return $ this-> renderFile ("test_page", $ data );
}
}
Create a new file test_page.html under Lib/Widget/Test/to put the code you read from the data library into test_page.html.
In other templates, call {: W ('test', array ()} like this ())}
This is just a basic example. In a specific project, you can dynamically Save the code you read from the database into Lib/Widget/Test/test_page.html. Call return $ this-> renderFile ("test_page", $ data );

Thinkphp template output

It should be <eq name = "data. answer" value = "">... <else/>... </eq>

Related Article

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.