コード解釈smartテンプレートエンジンの原理に似ています
ディレクトリ構造を貼り付けます.
簡単な呼び出しは以下の通りです.(
ここを見た以上,実はこの文章はもっとよく書けている.
ls
compiled/ index.php source/ ss.class.PHP
compiled
index.php
source
ss.clsss.php smarty
ss.clsss.php
templateDir=$templateDir;
$this->compileDir=$compileDir;
if(!empty($leftTag))$this->leftTag=$leftTag;
if(!empty($rightTag))$this->rightTag=$rightTag;
if(!empty($templateExtName))$this->templateExtName=$templateExtName;
}
//
public function assign($tag,$var){
$this->varPool[$tag]=$var;
}
// , str_replace
public function getVar($tag){
return $this->varPool[$tag];
}
//
public function getSourceTemplate($templateName){
$this->currentTemp=$templateName;
//
$sourceFileName=$this->templateDir.$this->currentTemp.$this->templateExtName;
$this->outputHtml=file_get_contents($sourceFileName);
}
//
public function compileTemplate($templateName=null){
//
$templateName=empty($templateName)?$this->currentTemp:$templateName;
// $pattern='/\{#(\$[a-zA-Z_]\w+)#\}/';// php
//preg_quote . \ + * ? [ ^ ] $ ( ) { } = ! < > | : -
$pattern="/".preg_quote($this->leftTag);
// {# $name #}
$pattern.=' *\$([a-zA-Z_]\w*) *';
$pattern.=preg_quote($this->rightTag)."/";
// $1 \\1 ,$1 , ( )
$this->outputHtml=preg_replace($pattern, 'getVar(\'$1\');?>', $this->outputHtml);
// , getvar Template::$getVar notice
$this->outputHtml=preg_replace($pattern, "getVar(\\1);?>", $this->outputHtml);
$compileFileName=$this->compileDir.md5($templateName).$this->templateExtName;
file_put_contents($compileFileName, $this->outputHtml);
}
//
public function display($templateName=null){
$templateName=empty($templateName)?$this->currentTemp:$templateName;
include($this->compileDir.md5($templateName).$this->templateExtName);
}
}
簡単な呼び出しは以下の通りです.(
index.php
の内容)assign('name','iamtb');
$test->assign('pageTitle','tbtbt');
$test->getSourceTemplate('index');
$test->compileTemplate('index');
$test->display('index');
ここを見た以上,実はこの文章はもっとよく書けている.