mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-01 12:42:48 +08:00
Template类增加layout方法 模板引擎驱动支持直接调用模板引擎的方法
This commit is contained in:
@@ -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 = '<?php echo ' . $name . ' ? ' . $name . ' : ' . substr($str, 1) . '; ?>';
|
||||
break;
|
||||
case '=':
|
||||
case '=':
|
||||
$str = '<?php if(' . $name . ') echo ' . substr($str, 1) . '; ?>';
|
||||
break;
|
||||
default:
|
||||
default:
|
||||
$str = '<?php echo ' . $name . '?' . $str . '; ?>';
|
||||
}
|
||||
} else {
|
||||
// $name为数组
|
||||
switch ($first) {
|
||||
case '?':
|
||||
case '?':
|
||||
// {$varname??'xxx'} $varname有定义则输出$varname,否则输出xxx
|
||||
$str = '<?php echo isset(' . $name . ') ? ' . $name . ' : ' . substr($str, 1) . '; ?>';
|
||||
break;
|
||||
case '=':
|
||||
case '=':
|
||||
// {$varname?='xxx'} $varname为真时才输出xxx
|
||||
$str = '<?php if(!empty(' . $name . ')) echo ' . substr($str, 1) . '; ?>';
|
||||
break;
|
||||
case ':':
|
||||
case ':':
|
||||
// {$varname?:'xxx'} $varname为真时输出$varname,否则输出xxx
|
||||
$str = '<?php echo !empty(' . $name . ')?' . $name . $str . '; ?>';
|
||||
break;
|
||||
default:
|
||||
default:
|
||||
if (strpos($str, ':')) {
|
||||
// {$varname ? 'a' : 'b'} $varname为真时输出a,否则输出b
|
||||
$str = '<?php echo !empty(' . $name . ')?' . $str . '; ?>';
|
||||
@@ -718,22 +737,22 @@ class Template
|
||||
$str = '<?php echo ' . $str . '; ?>';
|
||||
}
|
||||
break;
|
||||
case ':': // 输出某个函数的结果
|
||||
case ':': // 输出某个函数的结果
|
||||
$str = substr($str, 1);
|
||||
$this->parseVar($str);
|
||||
$str = '<?php echo ' . $str . '; ?>';
|
||||
break;
|
||||
case '~': // 执行某个函数
|
||||
case '~': // 执行某个函数
|
||||
$str = substr($str, 1);
|
||||
$this->parseVar($str);
|
||||
$str = '<?php ' . $str . '; ?>';
|
||||
break;
|
||||
case '-':
|
||||
case '+': // 输出计算
|
||||
case '+': // 输出计算
|
||||
$this->parseVar($str);
|
||||
$str = '<?php echo ' . $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], '###')) {
|
||||
|
||||
@@ -8,7 +8,6 @@
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user