修正 模板解析路径

This commit is contained in:
thinkphp
2016-03-13 21:35:23 +08:00
parent 4ed4fba8b9
commit 401466a389
2 changed files with 59 additions and 47 deletions

View File

@@ -24,6 +24,7 @@ class Template
protected $config = [ protected $config = [
'view_path' => '', // 模板路径 'view_path' => '', // 模板路径
'view_suffix' => '.html', // 默认模板文件后缀 'view_suffix' => '.html', // 默认模板文件后缀
'view_depr' => DS,
'cache_suffix' => '.php', // 默认模板缓存后缀 'cache_suffix' => '.php', // 默认模板缓存后缀
'tpl_deny_func_list' => 'echo,exit', // 模板引擎禁用函数 'tpl_deny_func_list' => 'echo,exit', // 模板引擎禁用函数
'tpl_deny_php' => false, // 默认模板引擎是否禁用PHP原生代码 'tpl_deny_php' => false, // 默认模板引擎是否禁用PHP原生代码
@@ -34,7 +35,6 @@ class Template
'compile_type' => 'file', // 模板编译类型 'compile_type' => 'file', // 模板编译类型
'cache_prefix' => '', // 模板缓存前缀标识,可以动态改变 'cache_prefix' => '', // 模板缓存前缀标识,可以动态改变
'cache_time' => 0, // 模板缓存有效期 0 为永久,(以数字为值,单位:秒) 'cache_time' => 0, // 模板缓存有效期 0 为永久,(以数字为值,单位:秒)
'cache_record_file' => 'cache_record_file', // 记录模板更新时间的文件
'layout_on' => false, // 布局模板开关 'layout_on' => false, // 布局模板开关
'layout_name' => 'layout', // 布局模板入口文件 'layout_name' => 'layout', // 布局模板入口文件
'layout_item' => '{__CONTENT__}', // 布局模板的内容替换标识 'layout_item' => '{__CONTENT__}', // 布局模板的内容替换标识
@@ -61,7 +61,7 @@ class Template
public function __construct(array $config = []) public function __construct(array $config = [])
{ {
$this->config['cache_path'] = RUNTIME_PATH . 'temp' . DS; $this->config['cache_path'] = RUNTIME_PATH . 'temp' . DS;
$this->config = array_merge($this->config, empty($config) ? Config::get() : $config); $this->config = array_merge($this->config, $config);
$this->config['taglib_begin'] = $this->stripPreg($this->config['taglib_begin']); $this->config['taglib_begin'] = $this->stripPreg($this->config['taglib_begin']);
$this->config['taglib_end'] = $this->stripPreg($this->config['taglib_end']); $this->config['taglib_end'] = $this->stripPreg($this->config['taglib_end']);
$this->config['tpl_begin'] = $this->stripPreg($this->config['tpl_begin']); $this->config['tpl_begin'] = $this->stripPreg($this->config['tpl_begin']);
@@ -688,7 +688,8 @@ 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);
@@ -750,22 +751,26 @@ 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 = '';
@@ -1011,10 +1016,13 @@ class Template
private function parseTemplateFile($template) private function parseTemplateFile($template)
{ {
if (false === strpos($template, '.')) { if (false === strpos($template, '.')) {
$template = str_replace(['/', ':'], $this->config['view_depr'], $template);
// 跨模块支持 // 跨模块支持
$template = strpos($template, '@') ? if (strpos($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'];
(defined('THEME_PATH') && substr_count($template, '/') < 2 ? THEME_PATH : $this->config['view_path']) . $template . $this->config['view_suffix']; } else {
$template = $this->config['view_path'] . $template . $this->config['view_suffix'];
}
} }
if (is_file($template)) { if (is_file($template)) {
// 记录模板文件的更新时间 // 记录模板文件的更新时间
@@ -1022,7 +1030,6 @@ class Template
return $template; return $template;
} else { } else {
throw new Exception('template not exist:' . $template, 10700); throw new Exception('template not exist:' . $template, 10700);
return false;
} }
} }

View File

@@ -39,7 +39,12 @@ class View
public function __construct(array $config = []) public function __construct(array $config = [])
{ {
$this->config($config); $this->config($config);
$this->engine($this->config['engine_type']); $engineConfig = array_merge(Config::get(), [
'view_path' => $this->config['view_path'],
'view_suffix' => $this->config['view_suffix'],
'view_depr' => $this->config['view_depr'],
]);
$this->engine($this->config['engine_type'], $engineConfig);
} }
/** /**
@@ -229,7 +234,7 @@ class View
list($module, $template) = explode('@', $template); list($module, $template) = explode('@', $template);
} }
// 获取当前主题的模版路径 // 获取当前主题的模版路径
defined('THEME_PATH') || define('THEME_PATH', $this->getThemePath($module)); $path = $this->getThemePath($module);
// 分析模板文件规则 // 分析模板文件规则
if (defined('CONTROLLER_NAME')) { if (defined('CONTROLLER_NAME')) {
@@ -240,7 +245,7 @@ class View
$template = str_replace('.', DS, CONTROLLER_NAME) . $depr . $template; $template = str_replace('.', DS, CONTROLLER_NAME) . $depr . $template;
} }
} }
return THEME_PATH . $template . $this->config['view_suffix']; return $path . $template . $this->config['view_suffix'];
} }
/** /**