1234567891011121314151617181920212223242526272829303132333435363738 |
- <?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);
- $this->smarty->setLeftDelimiter($options['left_delimiter'] ?? '{{');
- $this->smarty->setRightDelimiter($options['right_delimiter'] ?? '}}');
- // 调试输出路径(生产环境移除)
- 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);
- }
- }
|