视图驱动类增加exists方法用于判断模板文件是否存在

This commit is contained in:
thinkphp
2016-05-20 11:27:21 +08:00
parent 6dd393bd71
commit 952d18af17
2 changed files with 32 additions and 10 deletions

View File

@@ -31,6 +31,21 @@ class Php
$this->config = array_merge($this->config, $config); $this->config = array_merge($this->config, $config);
} }
/**
* 检测是否存在模板文件
* @access public
* @param string $template 模板文件或者模板规则
* @return bool
*/
public function exists($template)
{
if (!is_file($template)) {
// 获取模板文件名
$template = $this->parseTemplate($template);
}
return is_file($template);
}
/** /**
* 渲染模板文件 * 渲染模板文件
* @access public * @access public
@@ -40,12 +55,8 @@ class Php
*/ */
public function fetch($template, $data = []) public function fetch($template, $data = [])
{ {
if (!is_file($template)) {
// 获取模板文件名
$template = $this->parseTemplate($template);
}
// 模板不存在 抛出异常 // 模板不存在 抛出异常
if (!is_file($template)) { if (!$this->exists($template)) {
throw new Exception('template file not exists:' . $template, 10700); throw new Exception('template file not exists:' . $template, 10700);
} }
// 记录视图信息 // 记录视图信息

View File

@@ -40,6 +40,21 @@ class Think
$this->template = new Template($this->config); $this->template = new Template($this->config);
} }
/**
* 检测是否存在模板文件
* @access public
* @param string $template 模板文件或者模板规则
* @return bool
*/
public function exists($template)
{
if (!is_file($template)) {
// 获取模板文件名
$template = $this->parseTemplate($template);
}
return is_file($template);
}
/** /**
* 渲染模板文件 * 渲染模板文件
* @access public * @access public
@@ -50,12 +65,8 @@ class Think
*/ */
public function fetch($template, $data = [], $config = []) public function fetch($template, $data = [], $config = [])
{ {
if (!is_file($template)) {
// 获取模板文件名
$template = $this->parseTemplate($template);
}
// 模板不存在 抛出异常 // 模板不存在 抛出异常
if (!is_file($template)) { if (!$this->exists($template)) {
throw new Exception('template file not exists:' . $template, 10700); throw new Exception('template file not exists:' . $template, 10700);
} }
// 记录视图信息 // 记录视图信息