-
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.
- 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.
- 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.
- 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 relatively simple and is expected to be improved. |