How the PHP template engine is implemented

Source: Internet
Author: User
Assume that the label form is used as the template syntax

In the analysis, I think of two ways of realization
1. Use the regular to get the label content, and then the label will be a regular replacement and use the eval to parse, after parsing is completed and then replaced with regular
2. Use the regular to get the label content, and then make a regular replacement of the label, after the replacement is completed using the CLI to execute the script and return the content

The problem is that it is not clear whether the process and the two implementations are correct, or otherwise.

Thank you

Reply content:

Assume that the label form is used as the template syntax

In the analysis, I think of two ways of realization
1. Use the regular to get the label content, and then the label will be a regular replacement and use the eval to parse, after parsing is completed and then replaced with regular
2. Use the regular to get the label content, and then make a regular replacement of the label, after the replacement is completed using the CLI to execute the script and return the content

The problem is that it is not clear whether the process and the two implementations are correct, or otherwise.

Thank you

A simple comprehension, that's it.


  
   
  
   

file.html就是file.php的模板

Another idea is this.


  
   
 {{var}}

Excerpt 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();}

If you get the data, you have to deal with it further or display.

Let me give you a simple example.


   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;        The 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); }//Determine if the template is cached, such as the template file has a more Gerze recompile function cache ($tplname, $tpl _file) {$cache _file = $this->cache_dir. '/'. MD5 ($t plname). '.        PHP '; if (!file_exists ($cache _file) | | filemtime ($TPL _file) >filemtime ($cache _file)) $this->compile ($tpl _file, $c        Ache_file);    return $cache _file;        }//compile template contents into PHP format and write 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);}? >

Here is a template engine, see how to use

PHP usage Examples


'rows','b'=>array('c'=>'inarray'));include show('index');?>

Template usage Examples


{title} 你好,仅供参考
{a.a}{a.b.c}
{foreach a:$v}{autoindex}.{v}
{/foreach}{include:footer}

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

<?=$title?>
  
    
   
     
  
    

Just:

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

Can.

If you need to obtain the content rather than the direct output, as long as:

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

Packaged as a function, from a functional perspective, does not calculate the template engine with PHP actually?

PHP template is usually a translation type, is to translate a specific file into PHP or HTML, and then output is the result of the file

PHP itself can be viewed as a C-language template ...

The template engine that the landlord says, probably all uses the regular expression type realization.

{$user _name}---> regular match--->
Nothing too advanced in the inside, if you want to, directly using native mode embedded in HTML, after all, the efficiency is not so high

I wrote the C + + version many years ago, for reference, the implementation of the comparison of soil, the principle of the compilation is 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.