Php template parsing class-PHP source code

Source: Internet
Author: User
Tags php template
Php template parsing class

1. [PHP] code
 TVal = array ();}/*** set the template file directory * @ param string $ dir */public function setTemplateDir ($ dir) {$ this-> tDir = $ dir;}/*** whether to compile in real time * @ param bool $ real */public function setReal ($ real) {$ this-> real = (bool) $ real;}/*** set the template file extension * @ param string $ ext extension */public function setExtName ($ ext) {$ this-> tExtName = $ ext;}/*** temporary file directory * @ param string $ dir */public function setTmpDir ($ dir) {if (! File_exists ($ dir) {if (! Mkdir ($ dir, 0, true) die ("tmp dir $ dir can't to mkdir");} $ this-> tTmpDir = realpath ($ dir );} /*** URL scheduler * @ param Dispatcher $ dispatcher */public function setU (& $ dispatcher) {if (is_object ($ dispatcher) & method_exists ($ dispatcher, 'U') {$ this-> uDispatcher = $ dispatcher;}/*** register your own function * when $ function is a string, it is a square function, if it is an array of key-value pairs, the key is the class name, and the value is the static method in the class * @ param mix $ function */public function regist ErFunction ($ function) {if (is_array ($ function) {foreach ($ function as $ key => $ value) {$ this-> userFunctions ['class'] [$ key] = $ value ;}} else {$ this-> userFunctions ['functions'] [] = $ function;}/*** variable assignment * if $ name is an array of key-value pairs, directly assign a value to the $ name array * @ param mixed $ name variable name * @ param mixed $ value */public function assign ($ name, $ value = null) {if (is_array ($ name) {foreach ($ name as $ key => $ va L) {$ this-> tVal [$ key] = $ val ;}} else {$ this-> tVal [$ name] = $ value ;}} /*** get the template variable * @ param string $ name */public function getVal ($ name) {if (isset ($ this-> tVal [$ name]) {return $ this-> tVal [$ name];} else return false;}/*** the content after running, save to an html file * @ param string $ tFile * @ param string $ html */public function saveHtml ($ tFile, $ html) {ob_start (); $ this-> display ($ tFile); $ buffer = ob_get_c Ontents (); ob_end_clean (); file_put_contents ($ html, $ buffer );} /*** run and display the template content * @ param string $ tfile */public function display ($ tFile) {$ this-> tFile = $ this-> parseTemplatePath ($ tFile); if (! File_exists ($ this-> getTmpFile () | $ this-> real) {$ this-> parse () ;} extract ($ this-> tVal, EXTR_OVERWRITE ); include $ this-> getTmpFile ();}/*** compiled file * @ return string $ filepath */private function getTmpFile () {$ basename = basename ($ this-> tFile); $ pos = strrpos ($ basename ,'. '); $ tmp = 'tpl _'. substr ($ basename, 0, $ pos ). '. php '; return $ this-> tTmpDir. '/'. $ tmp;} private function parse () {$ This-> tContent = file_get_contents ($ this-> tFile); $ this-> parseInclude (); $ this-> parseSection (); $ this-> parseVal (); $ this-> parseEval (); if (! $ This-> real) {// if it is in a non-debugging environment, some useless content will be replaced. $ search = array ("/\ r? \ N/","/\ s {2,}/"); $ repace = array ('', ''); $ this-> tContent = preg_replace ($ search, $ repace, $ this-> tContent);} file_put_contents ($ this-> getTmpFile (), $ this-> tContent );} /*** resolve the sub-template in the template */private function parseInclude () {for ($ I = 0; $ I <6; $ I ++) {$ this-> tContent = preg_replace ("/\ {template \ s + ([a-zA-z0-9 \. _] +) \}/ies "," \ $ this-> subtemplate ('$ 1') ", $ this-> tContent );}} /*** get template only ** @ param str Ing $ file */private function subtemplate ($ file) {return file_get_contents ($ this-> parseTemplatePath ($ file ));} /*** resolution template path * @ param string $ file * @ return string $ filepath */private function parseTemplatePath ($ tFile) {$ tFile. = $ this-> tExtName; $ tFile = $ this-> tDir? $ This-> tDir. '/'. $ tFile: $ tFile; if (! File_exists ($ tFile) {die ("No template file $ tFile");} else {$ tFile = realpath ($ tFile);} return $ tFile ;} /*** resolve the variable */private function parseVal () {$ this-> tContent = preg_replace ("/\ {(\\\ \ S + ?) \}/Is ","
 ", $ This-> tContent);}/*** parse section */private function parseSection () {// Logic $ this-> tContent = preg_replace ("// \ {elseif \ s + (. + ?) \}/Ies "," \ $ this-> stripvtags ('
 ', '')", $ This-> tContent); $ this-> tContent = preg_replace ("// \ {else \}/is ","
 ", $ This-> tContent); $ this-> tContent = preg_replace ("/\ {U \ (. + ?) \) \}/Ies "," \ $ this-> parseUrl ('$ 1') ", $ this-> tContent); // loop for ($ I = 0; $ I <6; $ I ++) {$ this-> tContent = preg_replace ("/\ {loop \ s + (\ S +) \}(. + ?) \ {\/Loop \}/ies "," \ $ this-> str1_tags ('
 ',' \ 3
 ') ", $ This-> tContent); $ this-> tContent = preg_replace ("/\ {loop \ s + (\ S +) \ s + (\ S + )\}(. + ?) \ {\/Loop \}/ies "," \ $ this-> str1_tags ('
 \ 3) {?> ',' \ 4
 ') ", $ This-> tContent); $ this-> tContent = preg_replace ("/\ {if \ s + (. + ?) \} (. + ?) \ {\/If \}/ies "," \ $ this-> stripvtags ('
 ',' \ 2
 ') ", $ This-> tContent) ;}} private function str1_tags ($ expr, $ statement ='') {$ expr = str_replace ("\\\"", "\" ", preg_replace ("/\ <\? \= (\\\$. + ?) \? \>/S "," \ 1 ", $ expr); $ statement = str_replace (" \ "", "\" ", $ statement ); return $ expr. $ statement;}/*** parse PHP statement */private function parseEval () {$ this-> tContent = preg_replace ("/\ {eval \ s + (. + ?) \}/Is ","
 ", $ This-> tContent);}/*** parse URL */private function parseUrl ($ url) {if (is_object ($ this-> uDispatcher )) {return $ this-> uDispatcher-> U ($ url) ;}else {return $ url ;}}?>

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.