Smarty.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. public $allowTplExt = array('tpl', 'html', 'twig');//设置允许的文件后缀名,避免把PHP文件给输出出来了
  31. /**
  32. * 用户直接输出这个实例化的类后会输出当前类的名称
  33. *
  34. * @return String
  35. */
  36. public function __toString()
  37. {
  38. return get_class($this);
  39. }
  40. /**
  41. * 设置块,可以将块放在页面上任意位置,块的开始,setEndBlock为结束,内容将会缓存到$this->_blocks中
  42. *
  43. * @param String $block
  44. */
  45. public function setStartBlock($block)
  46. {
  47. $this->_blocks[$block] = '';
  48. ob_clean();
  49. ob_start();
  50. }
  51. /**
  52. * 设置块,此处是结束
  53. *
  54. * @param String $block
  55. */
  56. public function setEndBlock($block)
  57. {
  58. $content = ob_get_contents();
  59. ob_end_clean();
  60. $this->_blocks[$block] = $content;
  61. }
  62. /**
  63. * 返回块里边的内容
  64. *
  65. * @param String $block
  66. * @return String
  67. */
  68. public function getBlock($block)
  69. {
  70. return isset($this->_blocks[$block]) ? $this->_blocks[$block] : '';
  71. }
  72. /**
  73. * 显示块里边的内容
  74. *
  75. * @param String $block
  76. * @return null
  77. */
  78. public function displayBlock($block)
  79. {
  80. echo $this->getBlock($block);
  81. }
  82. /**
  83. * View构造函数
  84. *
  85. */
  86. public function __construct()
  87. {
  88. parent::__construct();
  89. $appConfigure = \Qii\Config\Register::getAppConfigure(\Qii\Config\Register::get(\Qii\Config\Consts::APP_INI_FILE));
  90. $viewInfo = $appConfigure['view']['smarty'];
  91. if (isset($viewInfo['ldelimiter']) && !empty($viewInfo['ldelimiter'])) $this->left_delimiter = $viewInfo['ldelimiter'];//变量左边界
  92. if (isset($viewInfo['rdelimiter']) && !empty($viewInfo['rdelimiter'])) $this->right_delimiter = $viewInfo['rdelimiter'];//变量右边界
  93. if (isset($viewInfo['path']) && !empty($viewInfo['path'])) $this->template_dir = \Qii\Autoloader\Psr4::realpath(\Qii\Autoloader\Psr4::getInstance()->getFolderByPrefix($viewInfo['path']));
  94. if (isset($viewInfo['compile']) && !empty($viewInfo['compile'])) $this->compile_dir = \Qii\Autoloader\Psr4::realpath(\Qii\Autoloader\Psr4::getInstance()->getFolderByPrefix($viewInfo['compile']));
  95. if (isset($viewInfo['cache']) && !empty($viewInfo['cache'])) $this->cache_dir = \Qii\Autoloader\Psr4::realpath(\Qii\Autoloader\Psr4::getInstance()->getFolderByPrefix($viewInfo['cache']));
  96. if (isset($viewInfo['lifetime']) && !empty($viewInfo['lifetime'])) $this->cache_lifetime = $viewInfo['lifetime'];
  97. //将老版本过度到新版本
  98. $this->setTemplateDir($this->template_dir)
  99. ->setCompileDir($this->compile_dir)
  100. ->setPluginsDir(SMARTY_PLUGINS_DIR)
  101. ->setCacheDir($this->cache_dir)
  102. ->setConfigDir($this->config_dir);
  103. $this->disableSecurity();
  104. $this->allow_php_templates = true;
  105. }
  106. /**
  107. * fetches a rendered Smarty template
  108. *
  109. * @param string $template the resource handle of the template file or template object
  110. * @param mixed $cache_id cache id to be used with this template
  111. * @param mixed $compile_id compile id to be used with this template
  112. * @param object $parent next higher level of Smarty variables
  113. *
  114. * @throws Exception
  115. * @throws SmartyException
  116. * @return string rendered template output
  117. */
  118. public function fetch($template = null, $cache_id = null, $compile_id = null, $parent = null)
  119. {
  120. $this->checkTplIsValid($template);
  121. return parent::fetch($template, $cache_id, $compile_id, $parent);
  122. }
  123. /**
  124. * displays a Smarty template
  125. *
  126. * @param string $template the resource handle of the template file or template object
  127. * @param mixed $cache_id cache id to be used with this template
  128. * @param mixed $compile_id compile id to be used with this template
  129. * @param object $parent next higher level of Smarty variables
  130. */
  131. public function display($template = null, $cache_id = null, $compile_id = null, $parent = null)
  132. {
  133. $this->checkTplIsValid($template);
  134. if (!empty($this->_blocks)) {
  135. $this->assign($this->_blocks);
  136. }
  137. parent::display($template, $cache_id, $compile_id, $parent);
  138. }
  139. /**
  140. * 设置模板存放路径
  141. * @param string $template_dir 模板路径
  142. * @param book $isConfig 是否配置
  143. */
  144. public function setTemplateDir($template_dir, $isConfig = false)
  145. {
  146. return parent::setTemplateDir($template_dir, $isConfig = false);
  147. }
  148. /**
  149. * 检查模板文件名称,只允许使用tpl
  150. * @param string $template 模板文件
  151. * @return bool | throw Exception
  152. */
  153. protected function checkTplIsValid($template)
  154. {
  155. $extension = pathinfo($template, PATHINFO_EXTENSION);
  156. if(!in_array($extension, $this->allowTplExt))
  157. {
  158. throw new \Exception('模板文件不合法 : 模板不允许使用除'.join('、', $this->allowTplExt).'以外的后缀名你');
  159. }
  160. return true;
  161. }
  162. }
  163. ?>