123456789101112131415161718192021222324252627282930313233343536 |
- <?php
- declare(strict_types=1);
- namespace App\View\Engine;
- use Hyperf\Contract\ConfigInterface;
- use Hyperf\View\Engine\EngineInterface;
- use Smarty;
- class SmartyEngine implements EngineInterface
- {
- protected Smarty $smarty;
- public function __construct(protected ConfigInterface $config)
- {
- $this->smarty = new Smarty();
- // 确保路径以斜杠结尾
- $viewPath = rtrim($this->config->get('view.config.view_path'), '/') . '/';
- $compileDir = rtrim($this->config->get('view.config.compile_dir'), '/') . '/';
- $this->smarty->setTemplateDir($viewPath);
- $this->smarty->setCompileDir($compileDir);
- // 调试输出路径(生产环境移除)
- error_log("Smarty模板路径: " . $viewPath);
- }
- public function render(string $template, array $data, array $config): string
- {
- foreach ($data as $key => $value) {
- $this->smarty->assign($key, $value);
- }
- return $this->smarty->fetch($template);
- }
- }
|