SmartyRenderer.php 697 B

1234567891011121314151617181920212223242526
  1. <?php
  2. namespace App\Service;
  3. use Hyperf\Context\ApplicationContext;
  4. use Smarty;
  5. class SmartyRenderer
  6. {
  7. protected Smarty $smarty;
  8. public function __construct()
  9. {
  10. $this->smarty = new Smarty();
  11. $this->smarty->setTemplateDir(BASE_PATH . '/storage/view/');
  12. $this->smarty->setCompileDir(BASE_PATH . '/runtime/smarty/compile/');
  13. $this->smarty->setCacheDir(BASE_PATH . '/runtime/smarty/cache/');
  14. }
  15. public function render(string $template, array $data): string
  16. {
  17. foreach ($data as $key => $value) {
  18. $this->smarty->assign($key, $value);
  19. }
  20. return $this->smarty->fetch($template);
  21. }
  22. }