think\Template::fetch PHP Method

fetch() public method

渲染模板文件
public fetch ( string $template, array $vars = [], array $config = [] ) : void
$template string 模板文件
$vars array 模板变量
$config array 模板参数
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;
        }
    }

Usage Example

Example #1
0
    public function testFetch()
    {
        $template = new Template();
        $template->tpl_path = dirname(__FILE__) . '/';
        $data = ['name' => 'value'];
        $content = <<<EOF
{\$name}
EOF;
        $template->fetch($content, $data);
        $this->expectOutputString('value');
    }
All Usage Examples Of think\Template::fetch