PHP self-writing simple template engine for beginners to learn
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:
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); } //Determine if templates are cached, such as template files have more Gerze recompile function cache ($tplname, $tpl _file) {& nbsp $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); & nbsp return $cache _file; } //compile template content into PHP format, and write cache function compile ($TPL, $cache) { $body = file_get_contents ($TPL); $VL = $this->var_left; &N Bsp $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 ( & nbsp "
", "
", "
", "
", "
", "
", "
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);}
How to use PHP files:
' Rows ', ' B ' =>array (' c ' = ' inarray ')); Include Show (' Index ');
To use the method 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 Templates directory, which is stored in the CAHCE directory, and both directories are created manually, and the cache is not directly accessible by the judgment. 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 ~