From b7632a5a528d12ae7dd7fa9c975c11a8bd573a38 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sat, 20 Feb 2016 18:24:55 +0800 Subject: [PATCH] =?UTF-8?q?Template=E7=B1=BB=E5=A2=9E=E5=8A=A0layout?= =?UTF-8?q?=E6=96=B9=E6=B3=95=20=E6=A8=A1=E6=9D=BF=E5=BC=95=E6=93=8E?= =?UTF-8?q?=E9=A9=B1=E5=8A=A8=E6=94=AF=E6=8C=81=E7=9B=B4=E6=8E=A5=E8=B0=83?= =?UTF-8?q?=E7=94=A8=E6=A8=A1=E6=9D=BF=E5=BC=95=E6=93=8E=E7=9A=84=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Template.php | 65 +++++++++++++++++++---------- library/think/view/driver/Think.php | 31 +++++++------- 2 files changed, 56 insertions(+), 40 deletions(-) diff --git a/library/think/Template.php b/library/think/Template.php index 26d8c039..c57ff89c 100644 --- a/library/think/Template.php +++ b/library/think/Template.php @@ -50,10 +50,10 @@ class Template 'namespace' => '\\think\\template\\driver\\', ]; - private $literal = []; - private $includeFile = []; // 记录所有模板包含的文件路径及更新时间 - private $md5Key = ''; // 保存当前模板的md5码 - protected $storage = null; + private $literal = []; + private $includeFile = []; // 记录所有模板包含的文件路径及更新时间 + private $md5Key = ''; // 保存当前模板的md5码 + protected $storage = null; /** * 架构函数 @@ -212,6 +212,25 @@ class Template $this->storage->read($cacheFile, $this->data); } + /** + * 设置布局 + * @access public + * @param mixed $name 布局模板名称 false 则关闭布局 + * @return void + */ + public function layout($name) + { + if (false !== $name) { + $this->config['layout_on'] = true; + if (is_string($name)) { + $this->config['layout_name'] = $name; + } + } else { + // 关闭布局 + $this->config['layout_on'] = false; + } + } + /** * 检查编译缓存是否有效 * 如果无效则需要重新编译 @@ -465,7 +484,7 @@ class Template private function parseExtend(&$content) { $regex = $this->getRegex('extend'); - $array = $blocks = $extBlocks = []; + $array = $blocks = $extBlocks = []; $extend = ''; $fun = function ($template) use (&$fun, &$regex, &$array, &$extend, &$blocks, &$extBlocks) { if (preg_match($regex, $template, $matches)) { @@ -558,7 +577,7 @@ class Template 'content' => substr($content, $start, $len), 'end' => $end, ]; - $keys[] = $begin['offset']; + $keys[] = $begin['offset']; } else { continue; } @@ -656,7 +675,7 @@ class Template $str = stripslashes($match[1]); $flag = substr($str, 0, 1); switch ($flag) { - case '$': // 解析模板变量 格式 {$varName} + case '$': // 解析模板变量 格式 {$varName} // 是否带有?号 if (false !== $pos = strpos($str, '?')) { $array = preg_split('/([!=]={1,2}|(?<]={0,1})/', substr($str, 0, $pos), 2, PREG_SPLIT_DELIM_CAPTURE); @@ -679,31 +698,31 @@ class Template } elseif (')' == substr($name, -1, 1)) { // $name为对象或是自动识别,或者含有函数 switch ($first) { - case '?': + case '?': $str = ''; break; - case '=': + case '=': $str = ''; break; - default: + default: $str = ''; } } else { // $name为数组 switch ($first) { - case '?': + case '?': // {$varname??'xxx'} $varname有定义则输出$varname,否则输出xxx $str = ''; break; - case '=': + case '=': // {$varname?='xxx'} $varname为真时才输出xxx $str = ''; break; - case ':': + case ':': // {$varname?:'xxx'} $varname为真时输出$varname,否则输出xxx $str = ''; break; - default: + default: if (strpos($str, ':')) { // {$varname ? 'a' : 'b'} $varname为真时输出a,否则输出b $str = ''; @@ -718,22 +737,22 @@ class Template $str = ''; } break; - case ':': // 输出某个函数的结果 + case ':': // 输出某个函数的结果 $str = substr($str, 1); $this->parseVar($str); $str = ''; break; - case '~': // 执行某个函数 + case '~': // 执行某个函数 $str = substr($str, 1); $this->parseVar($str); $str = ''; break; case '-': - case '+': // 输出计算 + case '+': // 输出计算 $this->parseVar($str); $str = ''; break; - case '/': // 注释标签 + case '/': // 注释标签 $flag2 = substr($str, 1, 1); if ('/' == $flag2 || ('*' == $flag2 && substr(rtrim($str), -2) == '*/')) { $str = ''; @@ -777,13 +796,13 @@ class Template $parseStr = $this->parseThinkVar($vars); } else { switch ($this->config['tpl_var_identify']) { - case 'array': // 识别为数组 + case 'array': // 识别为数组 $parseStr = $first . '[\'' . implode('\'][\'', $vars) . '\']'; break; - case 'obj': // 识别为对象 + case 'obj': // 识别为对象 $parseStr = $first . '->' . implode('->', $vars); break; - default: // 自动判断数组或对象 + default: // 自动判断数组或对象 $parseStr = '(is_array(' . $first . ')?' . $first . '[\'' . implode('\'][\'', $vars) . '\']:' . $first . '->' . implode('->', $vars) . ')'; } } @@ -828,14 +847,14 @@ class Template // 模板函数过滤 $fun = trim($args[0]); switch ($fun) { - case 'default': // 特殊模板函数 + case 'default': // 特殊模板函数 if (false === strpos($name, '(')) { $name = '(isset(' . $name . ') && (' . $name . ' !== \'\')?' . $name . ':' . $args[1] . ')'; } else { $name = '(' . $name . ' !== \'\'?' . $name . ':' . $args[1] . ')'; } break; - default: // 通用模板函数 + default: // 通用模板函数 if (!in_array($fun, $template_deny_funs)) { if (isset($args[1])) { if (strstr($args[1], '###')) { diff --git a/library/think/view/driver/Think.php b/library/think/view/driver/Think.php index 24259c8f..cb092c4e 100644 --- a/library/think/view/driver/Think.php +++ b/library/think/view/driver/Think.php @@ -8,7 +8,6 @@ // +---------------------------------------------------------------------- // | Author: liu21st // +---------------------------------------------------------------------- - namespace think\view\driver; use think\Template; @@ -16,33 +15,31 @@ use think\Template; class Think { private $template = null; + public function __construct($config = []) { $this->template = new Template($config); } - public function fetch($template, $data = [], $cache = []) + /** + * 渲染模板文件 + * @access public + * @param string $template 模板文件或者内容 + * @param array $data 模板变量 + * @param array $config 模板参数 + * @return void + */ + public function fetch($template, $data = [], $config = []) { if (is_file($template)) { - $this->template->display($template, $data, $cache); + $this->template->display($template, $data, $config); } else { $this->template->fetch($template, $data); } } - - /** - * 修改模板引擎配置项 - * @access public - * @param array|string $config - * @return string|array - */ - public function config($config) + + public function __call($method, $params) { - if(is_array($config)){ - $this->template->config($config); - return $this; - }else{ - return $this->template->config($config); - } + return call_user_func_array([$this->template, $method], $params); } }