How to Implement the PHP template engine

Source: Internet
Author: User
Tags php example php template
Assume that the tag format is used as the template syntax. In the parsing process, I think of two implementation methods: 1. obtain the TAG content using regular expressions, replace the tags with regular expressions, and use eval for parsing. After parsing, replace the tags with regular expressions. 2. use regular expressions to obtain the TAG content, and then regulate the tag... assume that the tag format is used as the template syntax

In the parsing stage, I think of two implementation methods.
1. Obtain the TAG content using regular expressions, replace the tags with regular expressions, and use eval for parsing. After parsing, replace the tags with regular expressions.
2. Obtain the TAG content using regular expressions and replace the tags with regular expressions. After replacement, run the script using cli and return the content.

The problem is that it is not clear whether the entire process and the two implementation methods are correct, or there are other methods.

Thank you.

Reply content:

Assume that the tag format is used as the template syntax

In the parsing stage, I think of two implementation methods.
1. Obtain the TAG content using regular expressions, replace the tags with regular expressions, and use eval for parsing. After parsing, replace the tags with regular expressions.
2. Obtain the TAG content using regular expressions and replace the tags with regular expressions. After replacement, run the script using cli and return the content.

The problem is that it is not clear whether the entire process and the two implementation methods are correct, or there are other methods.

Thank you.

This is the case.


  
  

File.html is the file. php template.

Another idea is this.

  
 {{var}}

From Slim:

    protected function render($template, $data = null){    $templatePathname = $this->getTemplatePathname($template);    if (!is_file($templatePathname)) {        throw new \RuntimeException("View cannot render `$template` because the template does not exist");    }    $data = array_merge($this->data->all(), (array) $data);    extract($data);    ob_start();    require $templatePathname;    return ob_get_clean();}

After obtaining the data, you need to further process the data or display it on your own.

Give you a simple column.


   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) ;}?>

The above is a template engine. Let's see how to use it.

Php example


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

Template example


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

PHP itself can be seen as a "template engine 」.

<?=$title?>
   
    
   

As long:

$title = 'test'; $main = '';require('template.inc.php');

You can.

If you want to get content rather than directly output content, as long:

ob_start();$title = 'test'; $main = '';require('template.inc.php');$content = ob_get_clean();

In terms of functions, isn't the template engine implemented using PHP?

The php template is generally a flip type, which is used to translate a specific file into php or html, and then the result of the file is returned.

Php itself can be seen as a C language template...

The template engine mentioned by the author is generally implemented using regular expressions.

{$ User_name} ---> Regular Expression matching --->
There is nothing too advanced in it. If you want to, simply embed it into html in the native mode. After all, the regular expression efficiency is not that high.

The c ++ version I wrote many years ago is for reference, implementation is relatively good, and compilation principles are also returned to the teacher.
Https://github.com/pi1ot/webapplib/blob/master/waTemplate.h
Https://github.com/pi1ot/webapplib/blob/master/waTemplate.cpp

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.