View类和模板引擎驱动分离调整 模板主题功能取消

This commit is contained in:
thinkphp
2016-04-18 15:32:18 +08:00
parent 455a18e85a
commit c2ef6cc883
6 changed files with 231 additions and 169 deletions

View File

@@ -17,38 +17,22 @@ class View
protected static $instance = null;
// 模板引擎实例
public $engine = null;
// 模板主题名称
protected $theme = '';
// 模板变量
protected $data = [];
// 视图参数
protected $config = [
// 模板主题
'theme_on' => false,
// 默认主题 开启模板主题有效
'default_theme' => 'default',
// 视图文件路径
'view_path' => '',
// 视图文件后缀
'view_suffix' => '.html',
// 视图文件分隔符
'view_depr' => DS,
// 视图层目录名
'view_layer' => VIEW_LAYER,
// 视图输出字符串替换
'parse_str' => [],
// 视图驱动命名空间
'namespace' => '\\think\\view\\driver\\',
'engine_type' => 'think',
// 模板引擎配置参数
'template' => [],
'engine_config' => [],
];
public function __construct(array $config = [])
{
$this->config($config);
if (!isset($this->config['template']['type'])) {
$this->config['template']['type'] = 'think';
}
}
/**
@@ -70,7 +54,7 @@ class View
* @access public
* @param mixed $name 变量名
* @param mixed $value 变量值
* @return View
* @return $this
*/
public function assign($name, $value = '')
{
@@ -88,7 +72,7 @@ class View
* @access public
* @param mixed $config 视图参数或者数组
* @param string $value 值
* @return View
* @return mixed
*/
public function config($config = '', $value = null)
{
@@ -112,59 +96,21 @@ class View
* @access public
* @param string $engine 引擎名称
* @param array $config 引擎参数
* @return View
* @return $this
*/
public function engine($engine, array $config = [])
{
if ('php' == $engine) {
$this->engine = 'php';
} else {
$class = $this->config['namespace'] . ucfirst($engine);
if (empty($this->config['view_path']) && defined('VIEW_PATH')) {
$this->config['view_path'] = VIEW_PATH;
}
$config = array_merge($config, [
'view_path' => $this->config['view_path'],
'view_suffix' => $this->config['view_suffix'],
'view_depr' => $this->config['view_depr'],
]);
$this->engine = new $class($config);
}
return $this;
}
/**
* 设置当前输出的模板主题
* @access public
* @param mixed $theme 主题名称
* @return View
*/
public function theme($theme)
{
if (true === $theme) {
// 启用主题
$this->config['theme_on'] = true;
} elseif (false === $theme) {
// 关闭主题
$this->config['theme_on'] = false;
} else {
// 指定主题
$this->config['theme_on'] = true;
$this->theme = $theme;
}
$class = $this->config['namespace'] . ucfirst($engine);
$this->engine = new $class($config);
return $this;
}
/**
* 解析和获取模板内容 用于输出
* @access public
*
* @param string $template 模板文件名或者内容
* @param array $vars 模板输出变量
* @param array $config 模板参数
* @param bool $renderContent 是否渲染内容
*
* @return string
* @throws Exception
*/
@@ -172,32 +118,18 @@ class View
{
// 模板变量
$vars = array_merge($this->data, $vars);
if (!$renderContent) {
// 获取模板文件名
$template = $this->parseTemplate($template);
// 开启调试模式Win环境严格区分大小写
// 模板不存在 抛出异常
if (!is_file($template) || (APP_DEBUG && IS_WIN && realpath($template) != $template)) {
throw new Exception('template file not exists:' . $template, 10700);
}
// 记录视图信息
APP_DEBUG && Log::record('[ VIEW ] ' . $template . ' [ ' . var_export(array_keys($vars), true) . ' ]', 'info');
}
if (is_null($this->engine)) {
// 初始化模板引擎
$this->engine($this->config['template']['type'], $this->config['template']);
$this->engine($this->config['engine_type'], $this->config['engine_config']);
}
// 页面缓存
ob_start();
ob_implicit_flush(0);
if ('php' == $this->engine || empty($this->engine)) {
// 原生PHP解析
extract($vars, EXTR_OVERWRITE);
is_file($template) ? include $template : eval('?>' . $template);
} else {
// 指定模板引擎
$this->engine->fetch($template, $vars, $config);
}
// 渲染输出
$method = $renderContent ? 'display' : 'fetch';
$this->engine->$method($template, $vars, $config);
// 获取并清空缓存
$content = ob_get_clean();
// 内容过滤标签
@@ -219,68 +151,12 @@ class View
* @access public
* @param string $content 内容
* @param array $vars 模板输出变量
* @param array $config 模板参数
* @return mixed
*/
public function show($content, $vars = [])
public function display($content, $vars = [], $config = [])
{
return $this->fetch($content, $vars, '', true);
}
/**
* 自动定位模板文件
* @access private
* @param string $template 模板文件规则
* @return string
*/
private function parseTemplate($template)
{
if (is_file($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'];
$template = str_replace(['/', ':'], $depr, $template);
if (strpos($template, '@')) {
list($module, $template) = explode('@', $template);
$path = APP_PATH . (APP_MULTI_MODULE ? $module . DS : '') . $this->config['view_layer'] . DS;
} else {
$path = $this->config['view_path'];
}
// 分析模板文件规则
if (defined('CONTROLLER_NAME')) {
if ('' == $template) {
// 如果模板文件名为空 按照默认规则定位
$template = str_replace('.', DS, CONTROLLER_NAME) . $depr . ACTION_NAME;
} elseif (false === strpos($template, $depr)) {
$template = str_replace('.', DS, CONTROLLER_NAME) . $depr . $template;
}
}
return realpath($path) . DS . $template . $this->config['view_suffix'];
}
/**
* 获取当前的模板主题
* @access private
* @return string
*/
private function getTemplateTheme()
{
if ($this->config['theme_on']) {
if ($this->theme) {
// 指定模板主题
$theme = $this->theme;
} else {
$theme = $this->config['default_theme'];
}
}
return isset($theme) ? $theme . DS : '';
return $this->fetch($content, $vars, $config, true);
}
/**