Php template function regular expression implementation code _ PHP Tutorial

Source: Internet
Author: User
Php template function regular implementation code. I have read the source code of phpcms and discuz, so there may be a lack of innovation, but the principles are mostly the same, but the details may be slightly different. Let's start with the specific implementation process. I have read the source code of phpcms and discuz, so there may be a lack of innovation. However, the principles are mostly the same, but the details may be slightly different.
Let's start with the specific implementation process.
1. First, you need to figure out where the template file is stored? Where can I store the converted php file? How can I name it? Source code:

The code is as follows:


Function template ($ tpl = 'index', $ dir = 'Hello ')
{
If (! File_exists ($ pd = TPL_PATH. $ dir. '/') @ mkdir ($ pd, 0777) or die ("$ pd directory creation failed"); // such as cache/tpl/hello/
If (! File_exists ($ td = TPL. $ dir. '/') @ mkdir ($ td, 0777) or die ("$ td directory creation failed"); // such as data/tpl/hello/

$ T2p = $ pd. $ tpl. '. php'; // The php file generated after regular conversion of the template file, such as cache/tpl/hello/index. php
$ T2h = $td.$tpl.'.html '; // html template file, such as data/tpl/hello/index.html


2. when do I need regular expression conversion? It can be that the php file after the regular expression does not exist, or the html file before the regular expression changes. The filemtime (string $ path) function is used here, and the last modification time of the file is returned.

The code is as follows:


If (! File_exists ($ t2p) | @ filemtime ($ t2p) <@ filemtime ($ t2h) // after the template file is changed, the regular PHP file is updated accordingly.
{
Template_go ($ t2p, $ t2h); // The template conversion starts.
}
Return $ t2p; // return the php file after the regular expression, which can be called like this: include template ('header', 'Hello ');
}


3. start template conversion, read from the html file, replace the regular expression, and write it to the PHP file.

The code is as follows:


Function template_go ($ t2p, $ t2h)
{
$ Str = @ file_get_contents ($ t2h); // Read
If ($ str = false) exit ("The template file is missing. Please check it! ");
$ Str = template_do ($ str); // regular expression replacement
@ Chmod ($ t2p, 0777 );
Return $ str = file_put_contents ($ t2p, $ str); // write
}


4. Regular expression rules: several simple regular expression replacement syntaxes.

The code is as follows:


Function template_do ($ str)
{
$ Str = preg_replace ('/([\ n \ r +]) \ t +/S',' \ 1', $ str); // remove the TAB. The modifier/s does not ignore line breaks.
$ Str = preg_replace ('/\{\$ (. *) \}/U ',' ', $ Str);/* Replace {$ xx} Note: the modifier/U must be added and can only be matched once. It can also be a lazy match */
$ Str = preg_replace ('/\ {php (. + )\}/',' ', $ Str);/* Replace {php xxxx} Note: the modifier/s cannot be added. you need to consider the issue of line feed when performing this regular expression multiple times */
$ Str = preg_replace ('/\ {template (. *) \}/U ',' ', $ Str );
/* Replace {template (xx, yy)} */
$ Str = preg_replace ('/\ {include (. *) \}/U ',' ', $ Str);/* {include xx. php} is replaced */
$ Str =" ". $ Str;
// $ Str = preg_replace ('/\ s +/', ', $ str); // check the source code of the webpage.
Return $ str;
}


Of course, this function is still relatively simple and is expected to be improved.
Ps: This is my first blog. I was thinking about writing a technical blog when I was free. I had to talk about it. when I learned some lessons, I also learned from the experts.
In addition, blogs are well preserved to facilitate ease of operation.

Bytes. Let's start with the specific implementation process...

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.