TemplateEngine.php 1.1 KB

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