PHP self-writing simple template engine for beginners to learn

Source: Internet
Author: User
PHP self-writing simple template engine, for beginners to learn something to write today, suddenly think of using the template engine. I wrote one easily and sent it to new users for learning. After such a long time in PHP, I thought it was a real emotion. after studying things for a few days, now I have finished it in an hour or two. the goddess who completed the game together is unknown now... Here is the code:

 Tpl_dir = $ tpl_dir; if (isset ($ cache_dir) $ this-> cache_dir = $ cache_dir; if (isset ($ tpl_ext) $ this-> tpl_ext = $ tpl_ext; if (isset ($ var_left) $ this-> var_left = $ var_left; if (isset ($ var_right) $ this-> var_right = $ var_right ;} function load ($ tplfilename) {$ tplfile = $ this-> tpl_dir. '/'. $ tplfilename. $ this-> tpl_ext; if (! File_exists ($ tplfile) die ('Template not found :'. $ tplfile); return $ this-> cache ($ tplfilename, $ tplfile);} // determines whether the template is cached. if the template file is changed, recompile the function cache ($ tplname, $ tpl_file) {$ cache_file = $ this-> cache_dir. '/'. md5 ($ tplname ). '. php'; if (! File_exists ($ cache_file) | filemtime ($ tpl_file)> filemtime ($ cache_file) $ this-> compile ($ tpl_file, $ cache_file); return $ cache_file ;} // compile the template content to the PHP format and write the cache function compile ($ tpl, $ cache) {$ body = file_get_contents ($ tpl); $ vl = $ this-> var_left; $ vr = $ this-> var_right; $ patterns = array ("# $ vl \ s * include :(. + ?) \ S * $ vr # I "," # $ vl \ s * if \ s + (. + ?) \ S * $ vr # I "," # $ vl \ s * else \ s * $ vr # I "," # $ vl \ s * elseif \ s + (. + ?) \ S * $ vr # I "," # $ vl \ s * endif \ s * $ vr # I ", "# $ vl \ s */if \ s * $ vr # I", "# $ vl \ s * foreach \ s + (. + ?) :(. + ?) \ S * $ vr # I "," # $ vl \ s * endforeach \ s * $ vr # I ", "# $ vl \ s */foreach \ s * $ vr # I", "# $ vl ([0-9a-zA-Z _] + ?) \. ([0-9a-zA-Z _] + ?) \. ([0-9a-zA-Z _] + ?) $ Vr # I "," # $ vl ([0-9a-zA-Z _] + ?) \. ([0-9a-zA-Z _] + ?) $ Vr # I "," # $ vl ([0-9a-zA-Z _ \ [\] \ '\ "] + ?) $ Vr # I "," # $ vl ([0-9a-zA-Z _] + ?) :(.*?) $ Vr # I "); $ replacements = array ("
 ","
 ","
 ","
 ","
 ","
 ","
 0): \ $ autoindex = 0; foreach ($ \ 1 as \ 2): \ $ autoindex ++;?> ","
 ","
 ","
 ","
 ","
 ","
 "); $ Body = preg_replace ($ patterns, $ replacements, $ body); file_put_contents ($ cache, $ body) ;}}$ view = new view (); function show ($ tpl) {global $ view; return $ view-> load ($ tpl );}

Usage php file:

 'Rows ',' B '=> array ('C' => 'array'); include show ('index ');

How to use the HTML template file:

{Title}
 Hello, for reference only
{A. a} {a. B. c}
{Foreach a: $ v} {autoindex}. {v}
{/Foreach}{Include: footer}

The template file is placed in the template directory, and the cache is placed in the cahce Directory. both directories must be created manually. Available

{Include: subtemplate}

{If condition}

{Foreach variable: as Statement}, $ autoindex is the auto-incrementing variable

{Call function: parameter}

{Variable}, including simplified writing of array variables (up to three-dimensional arrays are supported)

.

Well, the implementation of the entire template engine is relatively simple, because it has always liked this include method template, just for beginners to learn the principle.


================================== International convention ======================== ======

At the beginning, I learned to write some blogs. sorry for poor writing. I think you can give me a thumbs up!

My QQ group (PHP) 336533596 and QQ 451309839 are welcome to join. Communicate with each other and chat with others ~

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.