Write a simple template engine by yourself

Source: Internet
Author: User
Tags php foreach

After leaving home, I suddenly found that nothing was done. I studied smarty and wrote a simple search engine. Include

Assign assignment, display template parsing method,

Create a new MyTpl. class. php (other names are also supported)

The Code is as follows:

<? Phpclass MyTpl {// initialize function _ construct ($ template_dir = 'templates/', $ compile_dir = 'templates _ C ') {$ this-> template_dir = rtrim ($ template_dir ,'/'). '/'; $ this-> compile_dir = rtrim ($ compile_dir ,'/'). '/'; $ this-> tpl_vars = array ();} // value assignment function assign ($ tpl_var, $ value = null) {if ($ tpl_var! = '') $ This-> tpl_vars [$ tpl_var] = $ value;} // template parsing function display ($ fileName) {$ tplFile = $ this-> template_dir. $ fileName; if (! File_exists ($ tplFile) return false; $ comFileName = $ this-> compile_dir. 'com _ '. basename ($ tplFile).'. php'; if (! File_exists ($ comFileName) | filemtime ($ comFileName) <filemtime ($ tplFile) {$ repContent = $ this-> tpl_replace (file_get_contents ($ tplFile )); $ handle = fopen ($ comFileName, 'W + '); fwrite ($ handle, $ repContent); fclose ($ handle);} require_once $ comFileName ;} // template replacement function private function tpl_replace ($ content) {$ pattern = array ('/<\ {\ s * \ $ ([a-zA-Z _ \ x7f-\ xff] [a-zA-Z0-9 _ \ x7f-xff] *) \ s * \}>/I ', // parses the variable'/<\ {\ s * if \ s *(. + ?) \ S * \}> (. + ?) <\ {\ S * \/if \ s * \}>/ies ', // if tag parsing '/<\ {\ s * else \ s * if \ s *(. + ?) \ S * \}>/', // elseif parsing'/<\ {\ s * else \ s * \}> /', // else parsing '/<\ {\ s * loop \ s + \ $ (\ S +) \ s + \ $ ([a-zA-Z _ \ x7f-\ xff] [a-zA-Z0-9 _ \ x7f-\ xff] *) \ s * \}> (. + ?) <\ {\ S * \/loop \ s *}>/is ', // match the loop tag '/<\ {\ s * loop \ s + \ $ (\ S +) \ s + \ $ ([a-zA-Z _ \ x7f-\ xff] [a-zA-Z0-9 _ \ x7f-\ xff] *) \ s * => \ s * \ $ (\ S +) \ s * \}> (. + ?) <\ {\ S * \/loop \ s * \}>/is> /', // traverse the keys and values in the loop '/<\ {\ s * include \ s + [\ "\']? (. + ?) [\ "\ ']? \ S * \}>/ie ', // match include); // replace it with php $ replacement = array (' <? Php echo $ this-> tpl_vars ["$ {1}"]?> ', // Replace the variable' $ this-> stripvtags (\ '<? Php if ($ {1}) {?> \ ', \' $ {2} <? Php }?> \ ')', '$ This-> stripvtags (\' <? Php} elseif ($ {1}) {?> \ ', "")', '<? Php} else {?> ',' <? Php foreach ($ this-> tpl_vars ["$ {1}"] as $ this-> tpl_vars ["$ {2}"]) {?> $ {3} <? Php }?> ',' <? Php foreach ($ this-> tpl_vars ["$ {1}"] as $ this-> tpl_vars ["$ {2}"] =>$ this-> tpl_vars ["$ {3} "]) {?> $ (4) <? Php }?> ', 'File _ get_contents ($ this-> template_dir. "$ {1}") '); $ repContent = preg_replace ($ pattern, $ replacement, $ content ); if (preg_match ('/<\ {([^ (\}>)] {1,}) \}>/', $ repContent )) {$ repContent = $ this-> tpl_replace ($ repContent);} return $ repContent;} // replace the value of private function str1_tags ($ expr, $ statement) in the Condition statement) {$ var_pattern = '/\ s * \ $ ([a-zA-Z _ \ x7f-\ xff] [a-zA-Z0-9 _ \ x7f-\ xff] *) \ s */is '; $ expr = preg_replace ($ var_pattern,' $ this-> tpl_vars ["$ {1}"] ', $ expr ); $ expr = str_replace ("\\\" "," \ "", $ expr); $ statement = str_replace ("\\\"","\"", $ statement); return $ expr. $ statement ;}}


The above is very simple, that is, assigning values, and then outputting the template after parsing. the most complicated part is the regular expression replacement, which has been written for a long time. If you have time, please refer to smarty.

Next, let's test and create an inde. php

Require_once 'ins INS/MyTpl. class. php '; $ Tpl = new MyTpl (); $ title = 'first title'; $ Tpl-> assign ('title', $ title ); $ Tpl-> display ('index. tpl ');

Create a new templates directory, and write other templates_c directories according to the actual situation. then create an index. tpl in templates and write the corresponding code. I just did a simple test,

Then, you can preview it in the browser. In the regular expression replacement that I wrote, the foreach replacement label of a loop is also included. You can continue to expand and improve, and I will continue to improve my own template engine.

This article is from the "Thunder" blog, please be sure to keep this source http://a3147972.blog.51cto.com/2366547/1253462

Related Article

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.