PHP self-writing simple template engine for beginners to learn

Source: Internet
Author: User

Today to write something small, suddenly thought to use the template engine. On the "Handy" wrote a, sent up for beginners to learn. Took so long PHP, think also really feeling, that year the thing that studied for a few days now one or two hours to finish, then the goddess of playing together now has not known the trace ... Cough, here is the code:

<?php/**************** * @author: a sad * @email: [email protected] * @discription: simple template engine */Class View {var    $tpl _dir = ' template ';    var $cache _dir = ' cache ';    var $tpl _ext = '. html ';    var $var _left = ' {';     var $var _right = '} ';        function __construct ($config =array ()) {extract ($config);        if (Isset ($tpl _dir)) $this->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 ($ Tplname).'.        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 ("<?php include Show (' \\1 ');?>", "<?php if (\\1):?>", "<?php else:? &G            t; "," <?php ElseIf (\\1):?> "," <?php endif;?> "," <?php endif;?> ", "<?php if (count ($\\1) >0): \ $autoindex =0;foreach ($\\1 as \\2): \ $autoindex + +;?>", "<?php endf             Oreach;endif,?> "," <?php endforeach;endif;?> "," <?php echo $\\1[' \\2 ' [' \\3 '];?> ", "<?php Echo $\\1[' \\2 ';?>", "<?php echo $\\1;?>", "<?php Echo \\1 (\\2)         ;?> ");        $body = Preg_replace ($patterns, $replacements, $body);    File_put_contents ($cache, $body);    }} $view = new view (), function Show ($TPL) {global $view; Return $view->load ($TPL);}

How to use PHP files:

<?phpinclude ' view.php '; $title = ' document title '; $a =array (' a ' = ' + ' rows ', ' B ' =>array (' c ' = ' inarray ')); Include Show (' Index ');

To use the method HTML template file:


The template file is placed in the templates directory, and stored in the CAHCE directory, both directories are created manually. can use

{include: Child template}

{if condition}

{foreach variable: as statement}, $autoindex as a self-increment variable

{Calling function: parameter}

{variable}, containing array variables simplified notation (up to three-dimensional arrays only)

and other grammars.

OK, the entire template engine implementation is relatively simple, because has been more like this include mode of the template, only for beginners to learn the principle of good.


===================== International Practice =======================

Just started to learn to write some blog, write a bad place please forgive me, I think you can praise it!

My QQ group (PHP) 336533596,qq 451309839, Welcome to join. Communicate with each other, chat and brag ~


PHP self-writing simple template engine for beginners to learn

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.