12345678910111213141516171819202122232425262728293031323334 |
- <?php
- declare(strict_types=1);
- namespace App\Engine;
- use Hyperf\View\Engine\EngineInterface;
- use Smarty;
- class TemplateEngine implements EngineInterface
- {
- public function render($template, $data, $config): string
- {
- // 实例化 Smarty 模板引擎
- $smarty = new Smarty();
- // 配置 Smarty 的模板目录、编译目录等(根据你的实际配置修改)
- $smarty->setTemplateDir($config['template_dir']?? BASE_PATH. '/storage/view');
- $smarty->setCompileDir($config['compile_dir']?? BASE_PATH. '/runtime/smarty/compile');
- $smarty->setCacheDir($config['cache_dir']?? BASE_PATH. '/runtime/smarty/cache');
- // 设置定界符(根据你的配置修改)
- $smarty->setLeftDelimiter($config['left_delimiter']?? '{');
- $smarty->setRightDelimiter($config['right_delimiter']?? '}');
- // 将数据分配给模板
- foreach ($data as $key => $value) {
- $smarty->assign($key, $value);
- }
- // 调用 Smarty 的 fetch 方法进行渲染
- return $smarty->fetch($template);
- }
- }
|