From 8481ddd0acfb8630331e708d5dfc0d8747b02c9e Mon Sep 17 00:00:00 2001 From: augushong Date: Tue, 26 Sep 2023 10:52:32 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9fetch=E7=9A=84=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- extend/base/common/provider/ViewBase.php | 27 ++++++++++++++++++++++-- extend/think/Template.php | 10 ++++----- extend/think/view/driver/Think.php | 6 +++--- 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/extend/base/common/provider/ViewBase.php b/extend/base/common/provider/ViewBase.php index 2219bfc..117efce 100644 --- a/extend/base/common/provider/ViewBase.php +++ b/extend/base/common/provider/ViewBase.php @@ -61,7 +61,7 @@ class ViewBase extends ThinkView return $this->config([ 'view_suffix' => 'html', ])->getContent(function () use ($vars, $template) { - $this->engine()->fetch($template, array_merge($this->data, $vars)); + return $this->engine()->fetch($template, array_merge($this->data, $vars)); }); } @@ -77,7 +77,30 @@ class ViewBase extends ThinkView return $this->config([ 'view_suffix' => 'js', ])->getContent(function () use ($vars, $template) { - $this->engine()->fetch($template, array_merge($this->data, $vars)); + return $this->engine()->fetch($template, array_merge($this->data, $vars)); }); } + + /** + * 获取模板引擎渲染内容. + * @param $callback + * @return string + * @throws \Exception + */ + protected function getContent($callback): string + { + // 渲染输出 + try { + $content = $callback(); + } catch (\Exception $e) { + ob_end_clean(); + throw $e; + } + + if ($this->filter) { + $content = call_user_func_array($this->filter, [$content]); + } + + return $content; + } } diff --git a/extend/think/Template.php b/extend/think/Template.php index 77aa218..38f0d7f 100644 --- a/extend/think/Template.php +++ b/extend/think/Template.php @@ -210,9 +210,9 @@ class Template * 渲染模板文件. * @param string $template 模板文件 * @param array $vars 模板变量 - * @return void + * @return string */ - public function fetch(string $template, array $vars = []): void + public function fetch(string $template, array $vars = []): string { if ($vars) { $this->data = array_merge($this->data, $vars); @@ -220,9 +220,7 @@ class Template if ($this->isCache($this->config['cache_id'])) { // 读取渲染缓存 - echo $this->cache->get($this->config['cache_id']); - - return; + return $this->cache->get($this->config['cache_id']); } $template = $this->parseTemplateFile($template); @@ -251,7 +249,7 @@ class Template $this->cache->set($this->config['cache_id'], $content, $this->config['cache_time']); } - echo $content; + return $content; } } diff --git a/extend/think/view/driver/Think.php b/extend/think/view/driver/Think.php index 19f0df6..499a035 100644 --- a/extend/think/view/driver/Think.php +++ b/extend/think/view/driver/Think.php @@ -97,9 +97,9 @@ class Think * 渲染模板文件. * @param string $template 模板文件 * @param array $data 模板变量 - * @return void + * @return string */ - public function fetch(string $template, array $data = []): void + public function fetch(string $template, array $data = []): string { if ('' == pathinfo($template, PATHINFO_EXTENSION)) { // 获取模板文件名 @@ -111,7 +111,7 @@ class Think throw new TemplateNotFoundException('template not exists:' . $template, $template); } - $this->template->fetch($template, $data); + return $this->template->fetch($template, $data); } /**