模版引擎改进

This commit is contained in:
ThinkPHP
2013-05-15 14:55:03 +08:00
parent 90d7ec19e4
commit 04a30eeaba
2 changed files with 25 additions and 8 deletions

View File

@@ -100,6 +100,10 @@ class Template {
$this->config[$name] = $value;
}
public function config($config){
$this->config = array_merge($this->config,$config);
}
public function get($name){
return $this->tVar[$name];
}
@@ -109,10 +113,16 @@ class Template {
* @access public
* @param string $template 模板文件
* @param array $vars 模板变量
* @param string $cacheId 模板缓存标识
* @param array $config 模板参数
* @return void
*/
public function display($template,$vars=[],$cacheId='') {
public function display($template,$vars=[],$config=[]) {
if($vars){
$this->tVar = $vars;
}
if($config){
$this->config($config);
}
$template = $this->parseTemplateFile($template);
$cacheFile = $this->config['cache_path'].$this->config['cache_prefix'].md5($template).$this->config['cache_suffix'];
if(!$this->checkCache($template,$cacheFile)) { // 缓存无效
@@ -123,12 +133,12 @@ class Template {
ob_start();
ob_implicit_flush(0);
// 读取编译存储
$this->storage->read($cacheFile,$vars?$vars:$this->tVar);
$this->storage->read($cacheFile,$this->tVar);
// 获取并清空缓存
$content = ob_get_clean();
if($cacheId && $this->config['display_cache']) {
if($this->config['cache_id'] && $this->config['display_cache']) {
// 缓存页面输出
Cache::set($cacheId,$content,$this->config['cache_time']);
Cache::set($this->config['cache_id'],$content,$this->config['cache_time']);
}
echo $content;
}
@@ -141,13 +151,16 @@ class Template {
* @return void
*/
public function fetch($content,$vars=[]) {
if($vars){
$this->tVar = $vars;
}
$cacheFile = $this->config['cache_path'].$this->config['cache_prefix'].md5($content).$this->config['cache_suffix'];
if(!$this->checkCache($content,$cacheFile)) { // 缓存无效
// 模板编译
$this->compiler($content,$cacheFile);
}
// 读取编译存储
$this->storage->read($cacheFile,$vars?$vars:$this->tVar);
$this->storage->read($cacheFile,$this->tVar);
}
/**
@@ -427,7 +440,7 @@ class Template {
$begin = $this->config['taglib_begin'];
$end = $this->config['taglib_end'];
$className = '\\Think\\Template\\TagLib\\'.ucwords($tagLib);
$tLib = new $className;
$tLib = new $className($this);
foreach ($tLib->getTags() as $name=>$val){
$tags = [$name];
if(isset($val['alias'])) {// 别名设置

View File

@@ -60,10 +60,14 @@ class TagLib {
* @var object
* @access protected
*/
public $tpl;
protected $tpl;
protected $comparison = [' nheq '=>' !== ',' heq '=>' === ',' neq '=>' != ',' eq '=>' == ',' egt '=>' >= ',' gt '=>' > ',' elt '=>' <= ',' lt '=>' < '];
public function __construct($template){
$this->tpl = $template;
}
/**
* TagLib标签属性分析 返回标签属性数组
* @access public