Smarty.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. * @author Jinhui.zhu <jinhui.zhu@live.cn>
  4. * @version $Id: smarty.php 2 2012-07-06 08:50:19Z jinhui.zhu $
  5. *
  6. * 视图部分,提供供显示的方法,支持smarty
  7. *
  8. */
  9. namespace Qii\View;
  10. \Qii\Autoloader\Import::requires(Qii_DIR . DS . 'View' . DS . 'smarty' . DS . 'Autoloader.php');
  11. \Smarty_Autoloader::register(true);
  12. \Qii\Autoloader\Import::requires(Qii_DIR . DS . DS . 'View' . DS . 'smarty' . DS . 'SmartyBC.class.php');
  13. \Qii\Autoloader\Import::requires(Qii_DIR . DS . DS . 'View' . DS . 'smarty' . DS . 'sysplugins' .DS. 'smartyexception.php');
  14. \Qii\Autoloader\Import::requires(Qii_DIR . DS . DS . 'View' . DS . 'smarty' . DS . 'sysplugins' .DS. 'smartycompilerexception.php');
  15. class Smarty extends \SmartyBC
  16. {
  17. const VERSION = '1.2';
  18. public $caching = false;//是否缓存
  19. public $left_delimiter = '{#';//变量左边界
  20. public $right_delimiter = '#}';//变量右边界
  21. public $compile_check = true;//是否检查模板有变动
  22. public $debugging = false;//是否调试
  23. public $compile_dir = 'tmp/compile';//编译目录
  24. public $template_dir = 'view';//模板目录
  25. public $config_dir = 'configure';//配置文件目录
  26. public $plugins_dir = 'smarty/plugins/';//插件目录
  27. public $cache_dir = 'tmp/cache/';//缓存目录
  28. public $cache_id = '';//缓存文件ID
  29. public $cache_lifetime = 3600;//缓存时间
  30. /**
  31. * 用户直接输出这个实例化的类后会输出当前类的名称
  32. *
  33. * @return String
  34. */
  35. public function __toString()
  36. {
  37. return get_class($this);
  38. }
  39. /**
  40. * 设置块,可以将块放在页面上任意位置,块的开始,setEndBlock为结束,内容将会缓存到$this->_blocks中
  41. *
  42. * @param String $block
  43. */
  44. public function setStartBlock($block)
  45. {
  46. $this->_blocks[$block] = '';
  47. ob_clean();
  48. ob_start();
  49. }
  50. /**
  51. * 设置块,此处是结束
  52. *
  53. * @param String $block
  54. */
  55. public function setEndBlock($block)
  56. {
  57. $content = ob_get_contents();
  58. ob_end_clean();
  59. $this->_blocks[$block] = $content;
  60. }
  61. /**
  62. * 返回块里边的内容
  63. *
  64. * @param String $block
  65. * @return String
  66. */
  67. public function getBlock($block)
  68. {
  69. return isset($this->_blocks[$block]) ? $this->_blocks[$block] : '';
  70. }
  71. /**
  72. * 显示块里边的内容
  73. *
  74. * @param String $block
  75. * @return null
  76. */
  77. public function displayBlock($block)
  78. {
  79. echo $this->getBlock($block);
  80. }
  81. /**
  82. * View构造函数
  83. *
  84. */
  85. public function __construct()
  86. {
  87. parent::__construct();
  88. $appConfigure = \Qii\Config\Register::getAppConfigure(\Qii\Config\Register::get(\Qii\Config\Consts::APP_INI_FILE));
  89. $viewInfo = $appConfigure['view']['smarty'];
  90. if (isset($viewInfo['ldelimiter']) && !empty($viewInfo['ldelimiter'])) $this->left_delimiter = $viewInfo['ldelimiter'];//变量左边界
  91. if (isset($viewInfo['rdelimiter']) && !empty($viewInfo['rdelimiter'])) $this->right_delimiter = $viewInfo['rdelimiter'];//变量右边界
  92. if (isset($viewInfo['path']) && !empty($viewInfo['path'])) $this->template_dir = \Qii\Autoloader\Psr4::realpath(\Qii\Autoloader\Psr4::getInstance()->getFolderByPrefix($viewInfo['path']));
  93. if (isset($viewInfo['compile']) && !empty($viewInfo['compile'])) $this->compile_dir = \Qii\Autoloader\Psr4::realpath(\Qii\Autoloader\Psr4::getInstance()->getFolderByPrefix($viewInfo['compile']));
  94. if (isset($viewInfo['cache']) && !empty($viewInfo['cache'])) $this->cache_dir = \Qii\Autoloader\Psr4::realpath(\Qii\Autoloader\Psr4::getInstance()->getFolderByPrefix($viewInfo['cache']));
  95. if (isset($viewInfo['lifetime']) && !empty($viewInfo['lifetime'])) $this->cache_lifetime = $viewInfo['lifetime'];
  96. //将老版本过度到新版本
  97. $this->setTemplateDir($this->template_dir)
  98. ->setCompileDir($this->compile_dir)
  99. ->setPluginsDir(SMARTY_PLUGINS_DIR)
  100. ->setCacheDir($this->cache_dir)
  101. ->setConfigDir($this->config_dir);
  102. $this->disableSecurity();
  103. $this->allow_php_templates = true;
  104. }
  105. /**
  106. * displays a Smarty template
  107. *
  108. * @param string $template the resource handle of the template file or template object
  109. * @param mixed $cache_id cache id to be used with this template
  110. * @param mixed $compile_id compile id to be used with this template
  111. * @param object $parent next higher level of Smarty variables
  112. */
  113. public function display($template = null, $cache_id = null, $compile_id = null, $parent = null)
  114. {
  115. if (!empty($this->_blocks)) {
  116. $this->assign($this->_blocks);
  117. }
  118. parent::display($template, $cache_id, $compile_id, $parent);
  119. }
  120. }
  121. ?>