View.Class.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace Mall\Framework\Core;
  3. require_once ROOT_PATH . DS . 'Framework/Smarty/libs/Smarty.class.php';
  4. use Mall\Framework\Core\Config;
  5. class View {
  6. protected $template;
  7. protected $smartyConfig;
  8. private static $_instance;
  9. private function __construct($options = [])
  10. {
  11. $this->template = new \Smarty();
  12. $this->smartyConfig = $options ?: Config::getInstance()->get('smarty');
  13. $this->template
  14. ->setTemplateDir($this->smartyConfig['template_dir'])
  15. ->setCompileDir($this->smartyConfig['compile_dir'])
  16. ->setCacheDir($this->smartyConfig['cache_dir']);
  17. $this->template->caching = $this->smartyConfig['is_caching'];
  18. $this->template->left_delimiter="<{";
  19. $this->template->right_delimiter="}>";
  20. }
  21. public static function getInstance($options = [])
  22. {
  23. if (!self::$_instance instanceof self) {
  24. self::$_instance = new self($options);
  25. }
  26. return self::$_instance;
  27. }
  28. public function assign($tpl_var, $value = null, $nocache = false)
  29. {
  30. $this->template->assign($tpl_var, $value, $nocache);
  31. }
  32. public function display($template = null, $cache_id = null, $compile_id = null, $parent = null)
  33. {
  34. $template = explode('.', $template)[0] . $this->smartyConfig['tpl_type'];
  35. $this->template->display($template, $cache_id, $compile_id, $parent);
  36. }
  37. public function fetch($template = null, $cache_id = null, $compile_id = null, $parent = null)
  38. {
  39. $template = explode('.', $template)[0] . $this->smartyConfig['tpl_type'];
  40. return $this->template->fetch($template, $cache_id, $compile_id, $parent);
  41. }
  42. }