I have seen the source of Phpcms, discuz, so there may be a lack of innovation, but the principle is mostly connected, but the details may be slightly different processing.
To get to the point, let's start with the concrete implementation process.
1. Where do I put the template files first? Where do you put the converted PHP files? How else to name it? Directly on the source:
Copy the code code as follows:
function template ($tpl = ' index ', $dir = ' Hello ')
{
if (!file_exists ($pd = Tpl_path $dir. '/')) @mkdir ($pd, 0777) or Die ("$PD Directory Creation failed");//As cache/tpl/hello/
if (!file_exists ($td = TPL. $dir. '/') @mkdir ($TD, 0777) or Die ("$td Directory Creation failed");//As data/tpl/hello/
$t 2p = $pd. $tpl. PHP ';//template files are converted to form PHP files, such as cache/tpl/hello/index.php
$t 2h = $td. $tpl. HTML ';//html template files, such as data/tpl/hello/index.html
2. When do I need a regular conversion? Can be a regular after the PHP file does not exist, or the regular before the HTML file changes. The Filemtime (string $path) function is used here to return the file's last modified time.
Copy the code code as follows:
if (!file_exists ($t 2p) | | @filemtime ($t 2p) < @filemtime ($t 2h))//template file changed, regular PHP files are updated accordingly
{
Template_go ($t 2p, $t 2h);//Template conversion start
}
Return $t 2p;//Returns a regular PHP file, which can be called: include Template (' header ', ' Hello ');
}
3. Start the template transformation, first read from the HTML file, then the regular replacement, and finally written to the PHP file.
Copy the code code as follows:
function Template_go ($t 2p, $t 2h)
{
$str = @file_get_contents ($t 2h);//read out
if ($str = = = False) exit ("The template file is missing, please check!") ");
$str = Template_do ($STR);//Regular replacement
@chmod ($t 2p,0777);
return $str = file_put_contents ($t 2p, $str);//write
}
4. Regular rules, a few more abbreviated regular substitution syntax.
Copy the code code as follows:
function Template_do ($STR)
{
$str = Preg_replace ('/([\n\r+]) \t+/s ', ' \\1 ', $str);//Remove Tab tab. Modifier/s is not ignoring line breaks
$str = Preg_replace ('/\{\$ (. *) \}/us ', ' <?php echo $\\1;?> ', $str);/*{$xx} change to <?php echo $xx;? > Note that you must add the modifier/U to match only once. can also be lazy match */
$str = Preg_replace ('/\{php (. +) \}/', ' <?php \\1?> ', $str);/*{php xxxx} change to <?php xxxx?> Note that you cannot add the modifier/s to consider multiple times The question of a regular and a newline */
$str = Preg_replace ('/\{template (. *) \}/us ', ' <?php include template\\1;?> ', $str);
/*{template (XX,YY)} replaced by <?php include template (xx,yy);?> */
$str = Preg_replace ('/\{include (. *) \}/us ', ' <?php include ' \\1 ';?> ', $str);/*{include xx.php} change to <?php include X x.php?> */
$STR = "<?php defined (' in_ph ') or Die (' Access Denied ');? > ". $str;
$str = preg_replace ('/\s+/', ' ', $str);//view the page source code to see
return $str;
}
Of course, this function is still relatively rudimentary and looks forward to perfecting it.
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