namespace think
class Template
fetch()
~~~
/**
* 渲染模板文件
* @access public
* @param string $template 模板文件
* @param array $vars 模板变量
* @param array $config 模板参数
* @return void
*/
public function fetch($template, $vars = [], $config = [])
{
if ($vars) {
$this->data = $vars;
}
if ($config) {
$this->config($config);
}
if (!empty($this->config['cache_id']) && $this->config['display_cache']) {
// 读取渲染缓存
$cacheContent = Cache::get($this->config['cache_id']);
if (false !== $cacheContent) {
echo $cacheContent;
return;
}
}
$template = $this->parseTemplateFile($template);
if ($template) {
$cacheFile = $this->config['cache_path'] . $this->config['cache_prefix'] . md5($template) . '.' . ltrim($this->config['cache_suffix'], '.');
if (!$this->checkCache($cacheFile)) {
// 缓存无效 重新模板编译
$content = file_get_contents($template);
$this->compiler($content, $cacheFile);
}
// 页面缓存
ob_start();
ob_implicit_flush(0);
// 读取编译存储
$this->storage->read($cacheFile, $this->data);
// 获取并清空缓存
$content = ob_get_clean();
if (!empty($this->config['cache_id']) && $this->config['display_cache']) {
// 缓存页面输出
Cache::set($this->config['cache_id'], $content, $this->config['cache_time']);
}
echo $content;
}
}
~~~