mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-07 07:12:47 +08:00
Template类增加layout方法 模板引擎驱动支持直接调用模板引擎的方法
This commit is contained in:
@@ -50,10 +50,10 @@ class Template
|
|||||||
'namespace' => '\\think\\template\\driver\\',
|
'namespace' => '\\think\\template\\driver\\',
|
||||||
];
|
];
|
||||||
|
|
||||||
private $literal = [];
|
private $literal = [];
|
||||||
private $includeFile = []; // 记录所有模板包含的文件路径及更新时间
|
private $includeFile = []; // 记录所有模板包含的文件路径及更新时间
|
||||||
private $md5Key = ''; // 保存当前模板的md5码
|
private $md5Key = ''; // 保存当前模板的md5码
|
||||||
protected $storage = null;
|
protected $storage = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 架构函数
|
* 架构函数
|
||||||
@@ -212,6 +212,25 @@ class Template
|
|||||||
$this->storage->read($cacheFile, $this->data);
|
$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)
|
private function parseExtend(&$content)
|
||||||
{
|
{
|
||||||
$regex = $this->getRegex('extend');
|
$regex = $this->getRegex('extend');
|
||||||
$array = $blocks = $extBlocks = [];
|
$array = $blocks = $extBlocks = [];
|
||||||
$extend = '';
|
$extend = '';
|
||||||
$fun = function ($template) use (&$fun, &$regex, &$array, &$extend, &$blocks, &$extBlocks) {
|
$fun = function ($template) use (&$fun, &$regex, &$array, &$extend, &$blocks, &$extBlocks) {
|
||||||
if (preg_match($regex, $template, $matches)) {
|
if (preg_match($regex, $template, $matches)) {
|
||||||
@@ -558,7 +577,7 @@ class Template
|
|||||||
'content' => substr($content, $start, $len),
|
'content' => substr($content, $start, $len),
|
||||||
'end' => $end,
|
'end' => $end,
|
||||||
];
|
];
|
||||||
$keys[] = $begin['offset'];
|
$keys[] = $begin['offset'];
|
||||||
} else {
|
} else {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -656,7 +675,7 @@ class Template
|
|||||||
$str = stripslashes($match[1]);
|
$str = stripslashes($match[1]);
|
||||||
$flag = substr($str, 0, 1);
|
$flag = substr($str, 0, 1);
|
||||||
switch ($flag) {
|
switch ($flag) {
|
||||||
case '$': // 解析模板变量 格式 {$varName}
|
case '$': // 解析模板变量 格式 {$varName}
|
||||||
// 是否带有?号
|
// 是否带有?号
|
||||||
if (false !== $pos = strpos($str, '?')) {
|
if (false !== $pos = strpos($str, '?')) {
|
||||||
$array = preg_split('/([!=]={1,2}|(?<!-)[><]={0,1})/', substr($str, 0, $pos), 2, PREG_SPLIT_DELIM_CAPTURE);
|
$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)) {
|
} elseif (')' == substr($name, -1, 1)) {
|
||||||
// $name为对象或是自动识别,或者含有函数
|
// $name为对象或是自动识别,或者含有函数
|
||||||
switch ($first) {
|
switch ($first) {
|
||||||
case '?':
|
case '?':
|
||||||
$str = '<?php echo ' . $name . ' ? ' . $name . ' : ' . substr($str, 1) . '; ?>';
|
$str = '<?php echo ' . $name . ' ? ' . $name . ' : ' . substr($str, 1) . '; ?>';
|
||||||
break;
|
break;
|
||||||
case '=':
|
case '=':
|
||||||
$str = '<?php if(' . $name . ') echo ' . substr($str, 1) . '; ?>';
|
$str = '<?php if(' . $name . ') echo ' . substr($str, 1) . '; ?>';
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
$str = '<?php echo ' . $name . '?' . $str . '; ?>';
|
$str = '<?php echo ' . $name . '?' . $str . '; ?>';
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// $name为数组
|
// $name为数组
|
||||||
switch ($first) {
|
switch ($first) {
|
||||||
case '?':
|
case '?':
|
||||||
// {$varname??'xxx'} $varname有定义则输出$varname,否则输出xxx
|
// {$varname??'xxx'} $varname有定义则输出$varname,否则输出xxx
|
||||||
$str = '<?php echo isset(' . $name . ') ? ' . $name . ' : ' . substr($str, 1) . '; ?>';
|
$str = '<?php echo isset(' . $name . ') ? ' . $name . ' : ' . substr($str, 1) . '; ?>';
|
||||||
break;
|
break;
|
||||||
case '=':
|
case '=':
|
||||||
// {$varname?='xxx'} $varname为真时才输出xxx
|
// {$varname?='xxx'} $varname为真时才输出xxx
|
||||||
$str = '<?php if(!empty(' . $name . ')) echo ' . substr($str, 1) . '; ?>';
|
$str = '<?php if(!empty(' . $name . ')) echo ' . substr($str, 1) . '; ?>';
|
||||||
break;
|
break;
|
||||||
case ':':
|
case ':':
|
||||||
// {$varname?:'xxx'} $varname为真时输出$varname,否则输出xxx
|
// {$varname?:'xxx'} $varname为真时输出$varname,否则输出xxx
|
||||||
$str = '<?php echo !empty(' . $name . ')?' . $name . $str . '; ?>';
|
$str = '<?php echo !empty(' . $name . ')?' . $name . $str . '; ?>';
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
if (strpos($str, ':')) {
|
if (strpos($str, ':')) {
|
||||||
// {$varname ? 'a' : 'b'} $varname为真时输出a,否则输出b
|
// {$varname ? 'a' : 'b'} $varname为真时输出a,否则输出b
|
||||||
$str = '<?php echo !empty(' . $name . ')?' . $str . '; ?>';
|
$str = '<?php echo !empty(' . $name . ')?' . $str . '; ?>';
|
||||||
@@ -718,22 +737,22 @@ class Template
|
|||||||
$str = '<?php echo ' . $str . '; ?>';
|
$str = '<?php echo ' . $str . '; ?>';
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case ':': // 输出某个函数的结果
|
case ':': // 输出某个函数的结果
|
||||||
$str = substr($str, 1);
|
$str = substr($str, 1);
|
||||||
$this->parseVar($str);
|
$this->parseVar($str);
|
||||||
$str = '<?php echo ' . $str . '; ?>';
|
$str = '<?php echo ' . $str . '; ?>';
|
||||||
break;
|
break;
|
||||||
case '~': // 执行某个函数
|
case '~': // 执行某个函数
|
||||||
$str = substr($str, 1);
|
$str = substr($str, 1);
|
||||||
$this->parseVar($str);
|
$this->parseVar($str);
|
||||||
$str = '<?php ' . $str . '; ?>';
|
$str = '<?php ' . $str . '; ?>';
|
||||||
break;
|
break;
|
||||||
case '-':
|
case '-':
|
||||||
case '+': // 输出计算
|
case '+': // 输出计算
|
||||||
$this->parseVar($str);
|
$this->parseVar($str);
|
||||||
$str = '<?php echo ' . $str . '; ?>';
|
$str = '<?php echo ' . $str . '; ?>';
|
||||||
break;
|
break;
|
||||||
case '/': // 注释标签
|
case '/': // 注释标签
|
||||||
$flag2 = substr($str, 1, 1);
|
$flag2 = substr($str, 1, 1);
|
||||||
if ('/' == $flag2 || ('*' == $flag2 && substr(rtrim($str), -2) == '*/')) {
|
if ('/' == $flag2 || ('*' == $flag2 && substr(rtrim($str), -2) == '*/')) {
|
||||||
$str = '';
|
$str = '';
|
||||||
@@ -777,13 +796,13 @@ class Template
|
|||||||
$parseStr = $this->parseThinkVar($vars);
|
$parseStr = $this->parseThinkVar($vars);
|
||||||
} else {
|
} else {
|
||||||
switch ($this->config['tpl_var_identify']) {
|
switch ($this->config['tpl_var_identify']) {
|
||||||
case 'array': // 识别为数组
|
case 'array': // 识别为数组
|
||||||
$parseStr = $first . '[\'' . implode('\'][\'', $vars) . '\']';
|
$parseStr = $first . '[\'' . implode('\'][\'', $vars) . '\']';
|
||||||
break;
|
break;
|
||||||
case 'obj': // 识别为对象
|
case 'obj': // 识别为对象
|
||||||
$parseStr = $first . '->' . implode('->', $vars);
|
$parseStr = $first . '->' . implode('->', $vars);
|
||||||
break;
|
break;
|
||||||
default: // 自动判断数组或对象
|
default: // 自动判断数组或对象
|
||||||
$parseStr = '(is_array(' . $first . ')?' . $first . '[\'' . implode('\'][\'', $vars) . '\']:' . $first . '->' . implode('->', $vars) . ')';
|
$parseStr = '(is_array(' . $first . ')?' . $first . '[\'' . implode('\'][\'', $vars) . '\']:' . $first . '->' . implode('->', $vars) . ')';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -828,14 +847,14 @@ class Template
|
|||||||
// 模板函数过滤
|
// 模板函数过滤
|
||||||
$fun = trim($args[0]);
|
$fun = trim($args[0]);
|
||||||
switch ($fun) {
|
switch ($fun) {
|
||||||
case 'default': // 特殊模板函数
|
case 'default': // 特殊模板函数
|
||||||
if (false === strpos($name, '(')) {
|
if (false === strpos($name, '(')) {
|
||||||
$name = '(isset(' . $name . ') && (' . $name . ' !== \'\')?' . $name . ':' . $args[1] . ')';
|
$name = '(isset(' . $name . ') && (' . $name . ' !== \'\')?' . $name . ':' . $args[1] . ')';
|
||||||
} else {
|
} else {
|
||||||
$name = '(' . $name . ' !== \'\'?' . $name . ':' . $args[1] . ')';
|
$name = '(' . $name . ' !== \'\'?' . $name . ':' . $args[1] . ')';
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default: // 通用模板函数
|
default: // 通用模板函数
|
||||||
if (!in_array($fun, $template_deny_funs)) {
|
if (!in_array($fun, $template_deny_funs)) {
|
||||||
if (isset($args[1])) {
|
if (isset($args[1])) {
|
||||||
if (strstr($args[1], '###')) {
|
if (strstr($args[1], '###')) {
|
||||||
|
|||||||
@@ -8,7 +8,6 @@
|
|||||||
// +----------------------------------------------------------------------
|
// +----------------------------------------------------------------------
|
||||||
// | Author: liu21st <liu21st@gmail.com>
|
// | Author: liu21st <liu21st@gmail.com>
|
||||||
// +----------------------------------------------------------------------
|
// +----------------------------------------------------------------------
|
||||||
|
|
||||||
namespace think\view\driver;
|
namespace think\view\driver;
|
||||||
|
|
||||||
use think\Template;
|
use think\Template;
|
||||||
@@ -16,33 +15,31 @@ use think\Template;
|
|||||||
class Think
|
class Think
|
||||||
{
|
{
|
||||||
private $template = null;
|
private $template = null;
|
||||||
|
|
||||||
public function __construct($config = [])
|
public function __construct($config = [])
|
||||||
{
|
{
|
||||||
$this->template = new Template($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)) {
|
if (is_file($template)) {
|
||||||
$this->template->display($template, $data, $cache);
|
$this->template->display($template, $data, $config);
|
||||||
} else {
|
} else {
|
||||||
$this->template->fetch($template, $data);
|
$this->template->fetch($template, $data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function __call($method, $params)
|
||||||
* 修改模板引擎配置项
|
|
||||||
* @access public
|
|
||||||
* @param array|string $config
|
|
||||||
* @return string|array
|
|
||||||
*/
|
|
||||||
public function config($config)
|
|
||||||
{
|
{
|
||||||
if(is_array($config)){
|
return call_user_func_array([$this->template, $method], $params);
|
||||||
$this->template->config($config);
|
|
||||||
return $this;
|
|
||||||
}else{
|
|
||||||
return $this->template->config($config);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user