public function __construct($templateDir,$compileDir,$leftTag=null,$rightTag=null){ $this->templateDir=$templateDir; $this->compileDir=$compileDir; if (!empty($leftTag)) $this->leftTag=$leftTag; //传递左右标记的时候,需要做一下非空的判断 if (!empty($rightTag)) $this->rightTag=$rightTag; }
//assign函数——把模板中需要用到的变量,放到地址池中,并给定标记 public function assign($tag,$var){ $this->varpool[$tag]=$var; }
//与上面的assign对应,从地址池中取出变量 public function getVar($tag){ return $this->varpool[$tag]; }
//getSourceTemplate:获取编译的源文件_需要知道文件名,和完整的路径(所以要有拓展名 public function getSourceTemplate($templateName,$ext='.html'){ $this->currentTemp=$templateName; $sourceFilename=$this->templateDir.$this->currentTemp.$ext; $this->outputHtml=file_get_contents($sourceFilename); }
//compileTemplate:编译方法 public function compileTemplate($templateName=null,$ext='.html'){ $templateName=empty($templateName)? $this->currentTemp:$templateName; //核心代码,正则编译 //\{#\$(\w+)#\} $pattern='/\{#\$(\w+)#\}/'; //更加清晰的写法$pattern='/'.preg_quote($this->leftTag).' *\$([a-zA-Z_]\w*) *'.preg_quote($this->rightTag).'/'; //核心代码要做的就是找到标记内的东西,然后替换为php可以识别的内容 $this->outputHtml=preg_replace($pattern, 'getVar(\'$1\')?>', $this->outputHtml); //注意这里的preg_replace的用法,$1表示匹配的子模式
http://www.bkjia.com/PHPjc/1067097.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1067097.htmlTechArticlejQuery入门第二天正则表达式完结篇——仿smarty引擎的制作,jquery入门 hi 周一完全的不在状态。。。中午还去观战,没有睡觉的我,晚上的...