mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-06 07:02:47 +08:00
改进View类和Template类 简化模板主题(取消自动侦测) 当前主题作为view_path传入template 模板标签中可以使用 theme:controller/action 指定其他主题 或者 module@theme/controller/action 跨模块调用模板
This commit is contained in:
@@ -712,31 +712,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 . '; ?>';
|
||||||
@@ -814,13 +814,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) . ')';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -866,14 +866,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], '###')) {
|
||||||
@@ -1015,13 +1015,21 @@ class Template
|
|||||||
*/
|
*/
|
||||||
private function parseTemplateFile($template)
|
private function parseTemplateFile($template)
|
||||||
{
|
{
|
||||||
if (false === strpos($template, '.')) {
|
if (false === strpos($template, $this->config['view_suffix'])) {
|
||||||
$template = str_replace(['/', ':'], $this->config['view_depr'], $template);
|
|
||||||
// 跨模块支持
|
|
||||||
if (strpos($template, '@')) {
|
if (strpos($template, '@')) {
|
||||||
|
// 跨模块调用模板
|
||||||
|
$template = str_replace(['/', ':'], $this->config['view_depr'], $template);
|
||||||
$template = APP_PATH . str_replace('@', '/' . basename($this->config['view_path']) . '/', $template) . $this->config['view_suffix'];
|
$template = APP_PATH . str_replace('@', '/' . basename($this->config['view_path']) . '/', $template) . $this->config['view_suffix'];
|
||||||
} else {
|
} else {
|
||||||
$template = $this->config['view_path'] . $template . $this->config['view_suffix'];
|
if (strpos($template, ':')) {
|
||||||
|
// 指定主题
|
||||||
|
list($theme, $template) = explode(':', $template, 2);
|
||||||
|
$path = dirname($this->config['view_path']) . DS . $theme . DS;
|
||||||
|
} else {
|
||||||
|
$path = $this->config['view_path'];
|
||||||
|
}
|
||||||
|
$template = str_replace(['/', ':'], $this->config['view_depr'], $template);
|
||||||
|
$template = $path . $template . $this->config['view_suffix'];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (is_file($template)) {
|
if (is_file($template)) {
|
||||||
|
|||||||
@@ -170,14 +170,6 @@ class View
|
|||||||
*/
|
*/
|
||||||
public function fetch($template = '', $vars = [], $config = [], $renderContent = false)
|
public function fetch($template = '', $vars = [], $config = [], $renderContent = false)
|
||||||
{
|
{
|
||||||
if (is_null($this->engine)) {
|
|
||||||
// 初始化模板引擎
|
|
||||||
if (empty($this->config['view_path']) && defined('VIEW_PATH')) {
|
|
||||||
$this->config['view_path'] = VIEW_PATH;
|
|
||||||
}
|
|
||||||
$this->engine($this->config['view_engine']['type'], $this->config['view_engine']);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 模板变量
|
// 模板变量
|
||||||
$vars = $vars ? $vars : $this->data;
|
$vars = $vars ? $vars : $this->data;
|
||||||
if (!$renderContent) {
|
if (!$renderContent) {
|
||||||
@@ -191,6 +183,10 @@ class View
|
|||||||
// 记录视图信息
|
// 记录视图信息
|
||||||
APP_DEBUG && Log::record('[ VIEW ] ' . $template . ' [ ' . var_export($vars, true) . ' ]', 'info');
|
APP_DEBUG && Log::record('[ VIEW ] ' . $template . ' [ ' . var_export($vars, true) . ' ]', 'info');
|
||||||
}
|
}
|
||||||
|
if (is_null($this->engine)) {
|
||||||
|
// 初始化模板引擎
|
||||||
|
$this->engine($this->config['view_engine']['type'], $this->config['view_engine']);
|
||||||
|
}
|
||||||
// 页面缓存
|
// 页面缓存
|
||||||
ob_start();
|
ob_start();
|
||||||
ob_implicit_flush(0);
|
ob_implicit_flush(0);
|
||||||
@@ -241,14 +237,20 @@ class View
|
|||||||
if (is_file($template)) {
|
if (is_file($template)) {
|
||||||
return realpath($template);
|
return realpath($template);
|
||||||
}
|
}
|
||||||
|
if (empty($this->config['view_path']) && defined('VIEW_PATH')) {
|
||||||
|
$this->config['view_path'] = VIEW_PATH;
|
||||||
|
}
|
||||||
|
// 获取当前主题
|
||||||
|
$theme = $this->getTemplateTheme();
|
||||||
|
$this->config['view_path'] .= $theme;
|
||||||
|
|
||||||
$depr = $this->config['view_depr'];
|
$depr = $this->config['view_depr'];
|
||||||
$template = str_replace(['/', ':'], $depr, $template);
|
$template = str_replace(['/', ':'], $depr, $template);
|
||||||
$theme = $this->getTemplateTheme();
|
|
||||||
$path = $this->config['view_path'];
|
|
||||||
if (strpos($template, '@')) {
|
if (strpos($template, '@')) {
|
||||||
list($module, $template) = explode('@', $template);
|
list($module, $template) = explode('@', $template);
|
||||||
$path = APP_PATH . (APP_MULTI_MODULE ? $module . DS : '') . $this->config['view_layer'] . DS;
|
$path = APP_PATH . (APP_MULTI_MODULE ? $module . DS : '') . $this->config['view_layer'] . DS;
|
||||||
|
} else {
|
||||||
|
$path = $this->config['view_path'];
|
||||||
}
|
}
|
||||||
|
|
||||||
// 分析模板文件规则
|
// 分析模板文件规则
|
||||||
@@ -274,24 +276,11 @@ class View
|
|||||||
if ($this->theme) {
|
if ($this->theme) {
|
||||||
// 指定模板主题
|
// 指定模板主题
|
||||||
$theme = $this->theme;
|
$theme = $this->theme;
|
||||||
} elseif ($this->config['auto_detect_theme']) {
|
|
||||||
// 自动侦测模板主题
|
|
||||||
$t = $this->config['var_theme'];
|
|
||||||
if (isset($_GET[$t])) {
|
|
||||||
$theme = $_GET[$t];
|
|
||||||
} elseif (Cookie::get('think_theme')) {
|
|
||||||
$theme = Cookie::get('think_theme');
|
|
||||||
}
|
|
||||||
if (!isset($theme) || !is_dir($this->config['view_path'] . DS . $theme)) {
|
|
||||||
$theme = $this->config['default_theme'];
|
|
||||||
}
|
|
||||||
Cookie::set('think_theme', $theme, 864000);
|
|
||||||
} else {
|
} else {
|
||||||
$theme = $this->config['default_theme'];
|
$theme = $this->config['default_theme'];
|
||||||
}
|
}
|
||||||
return $theme . DS;
|
|
||||||
}
|
}
|
||||||
return '';
|
return isset($theme) ? $theme . DS : '';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user