Phpcms source code analysis ------- template engine PHPCMS full-site code analysis (6) template engine release: shuishui 19May & lt ;? Php/** the template function is defined in global. func. php. Previous phpcms homepage in phpcms source code parsing ------- template engine
PHPCMS full-site code analysis (6) template engine
Release: shuishui
19 May
/**
The function template function is defined in global. func. php. We can see it in index. php on the home page of phpcms. Usage: The include template () method is very familiar. it is actually the same as the template engine of dz. However, the DZ template engine is much simpler than PHPCMS, because the template label technology is not used. You can study the DZ template engine whenever you have time. Not here. Let's take a look at the main functions of the template above. The function is to return the path of the compiled template file. That is, replace the template X.html (template file) with regular expressions with x. php (compiled php file). then use the include function. Understand it! The php template engine is a bird. Then the rest is the regular expression. Wait.
*/
Function template ($ module = 'phpcms', $ template = 'index ')
{
Global $ CONFIG;
$ Compiledtplfile = $ CONFIG ['templatescachedir']. $ module. '_'. $ template. '. tpl. php ';
/**
Because phpcms stores template files in modules. Therefore, the template function has two parameters: the first is the module directory name, and the second is the template file name in this module.
$ CONFIG ['templatescachedir'] is the directory where php files are stored after compilation. In the config. inc. php site configuration file, define your own. In this way, the PHP file path after template compilation is obtained.
*/
If ($ CONFIG ['templaterefresh ']) // $ CONFIG ['templaterefresh'] is configured in config. inc. php. The default value is 1. Is the update Template switch. If you set it to 0, the template is updated. The program will not be updated.
{
$ Tplfile = PHPCMS_ROOT. '/templates/'. $ CONFIG ['defaulttemplate']. '/'. $ module. '/'.w.template.'.html ';
/**
Similar to the above sentence. $ CONFIG ['defaulttemplate'] is the default template directory. This is to obtain the path of the module you want and the template file in (@ get the template file before compilation)
*/
If (! File_exists ($ compiledtplfile) | @ filemtime ($ tplfile)> @ filemtime ($ compiledtplfile ))
{
/**
I compiled the file into a php file. The template is changed. Php files must also change. If you modify the template. What's the meaning of the station.
First, determine whether the template compilation file exists. If it does not exist, you do not need to judge the following condition. Because none of the compiled files exist. The program cannot run. (In fact, we mainly run the compiled PHP file, and the template file is a P for running html)
Or the following @ filemtime ($ tplfile)> @ filemtime ($ compiledtplfile) can easily understand that the filetime () function determines the last modification time of the file and returns the Unix timestamp. If the modification time of the template file is greater than that of the compiled file. This proves that the template file has been modified after the compilation file is generated. So are we still updating the compiled files? that is definitely pulling. So proceed.
*/
Require_once PHPCMS_ROOT. '/include/template. func. php'; // load the compilation function
Template_refresh ($ tplfile, $ compiledtplfile); // This is the template compilation startup function, which drives a series of template compilation functions to generate template compilation files.
}
}
Return $ compiledtplfile; // return the php file path after template compilation.
}
Defined ('in _ PHPCMS ') or exit ('Access Denied ');
Function template_compile ($ module, $ template) // compile the template to start the function. However, the parameters of the two functions are different according to the context. This function is used to compile templates in batches. The first is the module directory name, and the second is the template file name. See the following
{
Global $ CONFIG;
$ Content = file_get_contents (PHPCMS_ROOT. '/templates/'. $ CONFIG ['defaulttemplate']. '/'. $ module. '/'.w.template.'.html ');
$ Content = template_parse ($ content );
$ Compiledtplfile = $ CONFIG ['templatescachedir']. $ module. '_'. $ template. '. tpl. php ';
$ Strlen = file_put_contents ($ compiledtplfile, $ content );
@ Chmod ($ compiledtplfile, 0777 );
Return $ strlen;
}
Function template_refresh ($ tplfile, $ compiledtplfile) // compile the template to start the function. The first parameter is the template file name, and the second is the compiled php file name.
{
$ Str = file_get_contents ($ tplfile); // use the best function of php5: file_get_contents () to obtain the file content.
$ Str = template_parse ($ str);/* then use the template_parse () function to replace the file content. For example, replace {if xx> xx} regular expressions Xx) {?> For details, refer to the following */
$ Strlen = file_put_contents ($ compiledtplfile, $ str); // after compilation. Write the content to our so-called PHP compiling file.
@ Chmod ($ compiledtplfile, 0777); // do not forget to set the permission.
Return $ strlen; // return the number of characters in the content written to the compiled file. The following describes the template_parse () function.
}
Function template_module ($ module) // This is useful. Batch compile template files under a module directory
{
Global $ CONFIG;
$ Files = glob (PHPCMS_ROOT. '/templates/'. $ CONFIG ['defaulttemplate']. '/'. $ module. '/*. html ');
/*
The glob function obtains the list of all *. html files with the html extension in this path. For more information, see the manual.
**/
If (is_array ($ files ))
{
Foreach ($ files as $ tpl)
{// Start batch
$ Template = str_replace('.html ', '', basename ($ tpl ));
// Obtain the template file name. PHP file name after Compilation
Template_compile ($ module, $ template); // This function is described above. View above
}
}
Return TRUE;
}
Function template_cache () // This is a larger batch of generation than the above one. Because all modules in $ MODULE exist in the cache file template. As mentioned above. Watch it for yourself
{
Global $ MODULE;
Foreach ($ MODULE as $ module => $ m)
{
Template_module ($ module );
}
Return TRUE;
}
/**
Wow, don't scare it. In fact, they are all simple regular expressions. You only need to know what they are doing. Some custom labels are used in the template. Statement. These are not the standard PHP syntax. So it is impossible to run.
So what should we do. Use regular expressions to customize the syntax. To the standard PHP syntax. Then write it to our php file. Therefore, the following regular expressions are used to compile our own defined syntax.
The following describes the regular expressions. According to my own level. The following is a rough explanation. If you do not know the regular expression, please study it on Baidu. Something wrong. You can discuss it. @ Friends and sisters who do not understand the preg_replace () function. Read the manual by yourself.
*/
Function template_parse ($ str)
{
$ Str = preg_replace ("/([\ n \ r] +) \ t +/s", "\ 1", $ str );
// Use \ n \ r to filter out the tab. \ 1 is the text that matches the \ n line feed \ r Page feed in the first bracket, it is best to read the regular expression/s as the modifier. Baidu by yourself)
$ Str = preg_replace ("/\ <\! \-\ {(. + ?) \}\-\>/S "," {\\ 1} ", $ str );
// Replace with {xx} {Xx} must perform the second regular expression replacement below. if it is not possible to run it in PHP. . +? And. + are both lazy and greedy. You can see the name. I don't know about Baidu.
$ Str = preg_replace ("/\ {template \ s + (. +) \}/", "\ n \ N ", $ str );
/* Compile the {template 'XX' and 'JJ '} in the template into the PHP Standard syntax: You may understand at first glance: include template. By the way. This can also be run in PHP. Because the template () function is not defined. */
$ Str = preg_replace ("/\ {include \ s + (. +) \}/", "\ n \ N ", $ str );
/* The {include xx. php} in the template is compiled into **/
$ Str = preg_replace ("/\ {php \ s + (. +) \}/", "\ n \ N ", $ str );
/* Compile {php xxxx} in the template You should also understand. Xxxx must be the standard language of PHP. Therefore, the phpcms template statement {php} is used to write the PHP statement to run in the template. This function is also available in smarty **/
$ Str = preg_replace ("/\ {if \ s + (. + ?) \}/"," ", $ Str );
/* This is simpler. Compile {if xxxx} in the template Let's take a look at this step to compile some of your own statements into the standard PHP syntax. This is called the template engine. **/
$ Str = preg_replace ("/\ {else \}/"," ", $ Str );
/* {Else} **/
$ Str = preg_replace ("/\ {elseif \ s + (. + ?) \}/"," ", $ Str );
/* {Elseif} **/
$ Str = preg_replace ("/\{\/ if \}/"," ", $ Str );
/* {/If} to convert The phpcms template syntax is: {/if. Don't forget that php cannot run **/
$ Str = preg_replace ("/\ {loop \ s + (\ S + )\}/"," ", $ Str );
/* Below is the loop. In the template, {loop xx jj} is actually compiled into the PHP foreach (xx AS jj) so that everyone will use it **/
$ Str = preg_replace ("/\ {loop \ s + (\ S + )\}/", "\ n \ 3) {?> ", $ Str );
/* This sentence is similar to the above one. However, an array name {loop xx jj yy} is added to foreach (xx as jj => yy )**/
$ Str = preg_replace ("/\{\/ loop \}/", "\ n \ N ", $ str );
/* Do not forget to end the loop {/loop} corresponding to the PHP **/
$ Str = preg_replace ("/\ {tag _ ([^}] +) \}/e", "get_tag ('\ 1')", $ str );
/* Replace {tag_xx} with get_tag ('XX'). The get_tag () function is a custom function, because the phpcms template engine applies the tag function. This function is used to call tags. **/
$ Str = preg_replace ("/\ {([a-zA-Z _ \ x7f-\ xff] [a-zA-Z0-9 _ \ x7f-\ xff] * \ ([^ {}] *) \))\}/"," ", $ Str );
/* {Xxx (jj)} is such a strange thing. I found the PHPCMS template file. No such monster was found after several files were found. If anyone finds it, let me see it. I am afraid I understand the regular expression. Thank you first **/
$ Str = preg_replace ("// {\\$ ([a-zA-Z _ \ x7f-\ xff] [a-zA-Z0-9 _ \ x7f-\ xff] * \ ([ ^ {}] *) \))\}/"," ", $ Str );
/* {$ Xxx (wwsd)} is dedicated Of course, xxx () is a function defined in the program **/
$ Str = preg_replace ("/\ {([a-zA-Z0-9 _ \ x7f-\ xff] *) \}/] \ $ [a-zA-Z _ \ x7f-\ xff] [a-zA-Z0-9 _ \ x7f-\ xff] *) \}/"," ", $ Str );
/* Convert {$ xxjj} Of course, the variable is output **/
$ Str = preg_replace ("/\ {(\] \ $[a-zA-Z0-9 _ \ [\] \ '\" \ $ \ x7f-\ xff] +) \}/es "," addquote (' ') ", $ Str );
/* Convert {$ xxx ['JJ ']} The addquote () function is defined by itself. See the following for secondary filtering. There are tokens for verification, and the yellow words are too dizzy to read for too long. I'm dizzy **/
$ Str = preg_replace ("/\ {([A-Z _ \ x7f-\ xff] [A-Z0-9 _ \ x7f-\ xff] *) \}/s "," ", $ Str );
/* {XXJJ} XXJJ is the constant we define **/
$ Str =" ". $ Str;
/* Do not forget to add this to each of your compiled files. I have talked about it before and cannot find the previous example **/
Return $ str; // Finally, the filtered content is returned.
}
Function get_tag ($ tagname) // This function is displayed in the above compiled function. Actually, it is to get the content of the corresponding tag. the header is a bit dizzy. let's talk about the tag in the next section.
{
Global $ tags, $ html, $ CONFIG;
If (! $ Tags) require PHPCMS_ROOT. '/templates/'. $ CONFIG ['defaulttemplate']. '/tags. php ';
If (! $ Html) require PHPCMS_ROOT. '/templates/'. $ CONFIG ['defaulttemplate']. '/html. php ';
If (! Isset ($ tags [$ tagname]) return '{tag _'. $ tagname .'}';
$ Code = isset ($ html [$ tagname])? 'Tag _ read ('. $ html [$ tagname].') ': $ tags [$ tagname];
Return" ";
}
Function addquote ($ var)
{
Return str_replace ("[url =] \ [/url]" ",", preg_replace ("/\ [([a-zA-Z0-9 _\-\. \ x7f-\ xff] +) \]/s "," ['\ 1'] ", $ var ));
}
?>
[Size = xx-small] [/size]
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