修正 模板解析路径

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

View File

@@ -39,7 +39,12 @@ class View
public function __construct(array $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);
}
// 获取当前主题的模版路径
defined('THEME_PATH') || define('THEME_PATH', $this->getThemePath($module));
$path = $this->getThemePath($module);
// 分析模板文件规则
if (defined('CONTROLLER_NAME')) {
@@ -240,7 +245,7 @@ class View
$template = str_replace('.', DS, CONTROLLER_NAME) . $depr . $template;
}
}
return THEME_PATH . $template . $this->config['view_suffix'];
return $path . $template . $this->config['view_suffix'];
}
/**