From 04a30eeaba3416ecadbe6c560adbf7fc5e8e44f4 Mon Sep 17 00:00:00 2001 From: ThinkPHP Date: Wed, 15 May 2013 14:55:03 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A8=A1=E7=89=88=E5=BC=95=E6=93=8E=E6=94=B9?= =?UTF-8?q?=E8=BF=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Library/Think/Template.php | 27 ++++++++++++++++++++------- Library/Think/Template/TagLib.php | 6 +++++- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/Library/Think/Template.php b/Library/Think/Template.php index 60c7a721..b6bedc98 100644 --- a/Library/Think/Template.php +++ b/Library/Think/Template.php @@ -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'])) {// 别名设置 diff --git a/Library/Think/Template/TagLib.php b/Library/Think/Template/TagLib.php index 1d63e46e..d6bf9a6e 100644 --- a/Library/Think/Template/TagLib.php +++ b/Library/Think/Template/TagLib.php @@ -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