PHP Basic Template engine
Last Update:2016-08-15
Source: Internet
Author: User
<span id="Label3"></p><p><p>1, Configuration File: config.php</p></p><p><p><?php</p></p><p><p>/**</p></p><p><p>* @yzt</p></p><p><p>The *tpl_cache is used to specify the path (file) of the build. PHP</p></p><p><p>*tpl_path used to specify the file path of the build template</p></p><p><p>**/</p></p><p><p>Define (' Tpl_cache ', './cache/');<br>Define (' Tpl_path ', './views/');</p></p><p><p></p></p><p><p>2, Test demo1.php</p></p><p><p>/**</p></p><p><p>*include file Import</p></p><p><p>*compact Array of data</p></p><p><p>***/</p></p><p><p><?php<br>Include ' config.php ';</p></p><p><p>Include ' tpl.func.php ';</p></p><p><p><br>$title = ' See the goddess easily from sorrow ';</p></p><p><p>$content = ' Try to flatten your relationship, or you will fail ';</p></p><p><p>$footercontent = ' because you will tweaked, unnatural, the goddess will fight you low score ';</p></p><p><p>$data = [<br>' Yzt ' = ' yzt ',<br>' XYY ' = ' XYY ',<br>];</p></p><p><p>Display (' moban.html ', compact (' title ', ' content ', ' data ', ' footercontent '));</p></p><p><p></p></p><p><p>3, engine (core) tpl.func.php</p></p><p><p><?php</p></p><p><p><br>Two parameters 1,html template; 2, parameters to be modified<br>function display ($tplFile, $tplVars = Null)<br>{<br>$tplFilePath = RTrim (tpl_path, '/'). ‘/‘ . $tplFile;</p></p><p><p>If (!file_exists ($tplFilePath)) {<br><br>Exit (' template file does not exist ');<br>}</p></p><p><p>$html = Compile ($tplFilePath);</p></p><p><p>$cacheFileName = Parsepath ($tplFile);</p></p><p><p>If (!check_cache_dir (tpl_cache)) {<br><br>Exit (' Cache Directory not writable ');<br>}</p></p><p><p>If (!file_put_contents ($cacheFileName, $html)) {<br><br>Exit (' Cache file write failed ');<br>}</p></p><p><p>If (is_array ($TPLVARS)) {<br><br>Extract ($tplVars);<br><br>Include $cacheFileName;<br><br>}</p></p><p><p>}</p></p><p><p>function Check_cache_dir ($path)<br>{<br>If (!file_exists ($path) | |!is_dir ($path)) {<br><br>return mkdir ($path, 0755,true);<br>}<br>If (!is_writeable ($path) | |!is_readable ($path)) {<br><br>return chmod ($path, 0755);<br>}<br>Return true;<br>}</p></p><p><p>function Parsepath ($tplFile)<br>{<br>$path = RTrim (tpl_cache, '/'). ' /'. str_replace ('. ', ' _ ', $tplFile). PHP ';<br>Return $path;<br>}</p></p><p><p>function Compile ($path)<br>{<br>$keys = [</p></p><p>' {if percent} ' = ' <?php if (\1):?> ',<br>' {else} ' = ' <?php else:?> ',<br>' {else if percent} ' = ' <?php elseif (\1):?> ',<br>' {elseif%} ' = ' <?php elseif (\1):?> ',<br>' {/if} ' = ' <?php endif;? > ',<br>' {$%%} ' = ' <?=$\1;? > ',<br>' {foreach percent} ' = ' <?php foreach (\1):?> ',<br>' {/foreach} ' = ' <?php endforeach;? > ',<br>' {for-percent} ' = ' <?php for (\1):?> ',<br>' {/for} ' = ' <?php endfor;? > ',<br>' {while percent '} ' = ' <?php while (\1):?> ',<br>' {/while} ' = ' <?php endwhile;? > ',<br>' {continue} ' = ' <?php continue;? > ',<br>' {break} ' = ' <?php break;? > ',<br>' {$%% = $%%} = ' <?php $\1 = $\2;? > ',<br>' {$%%++} ' = ' <?php $\1++;? > ',<br>' {$%%--} ' = ' <?php $\1--;? > ',<br>' {comment} ' = ' <?php/* ',<br>' {/comment} ' = ' */?> ',<br>' {/*} ' = ' <?php/* ',<br>' {* *} ' = ' *?> ',<br>' {section} ' = ' <?php ',<br>' {/section} ' = '?> ',<br>' {include percent} ' = ' <?php include \1;? > ',</p><p><p>];</p></p><p><p>$file = file_get_contents ($path);</p></p><p><p>foreach ($keys as $key = = $val) {<br><br>$pattern = ' # '. Str_replace ('% ', ' (. +) ', preg_quote ($key, ' # ')). ' #imsU ';<br><br>$replace = $val;<br><br><br>If (stripos ($pattern, ' include ')) {<br><br>$file = Preg_replace_callback ($pattern, ' parseinclude ', $file);</p></p><p><p>} else{</p></p><p><p>$file = preg_replace ($pattern, $replace, $file);<br>}</p></p><p><p>}</p></p><p><p>Return $file;<br>}</p></p><p><p>function Parseinclude ($data)<br>{</p></p><p><p>$path = str_replace (array (' \ ', ' "'),", $data [1]);<br>data[1] is-------footer.html<br><br>$cacheFileName = Parsepath ($path);<br><br>Display ($path);<br><br>Return ' <?php include ' '. $cacheFileName. ';? > ';</p></p><p><p>}</p></p><p><p></p></p><p><p>4, Template moban.html</p></p><p><p>