View.class.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace KIF\Core;
  3. use Smarty;
  4. include KIF_PATH . "/smarty/Smarty.class.php";
  5. use KIF\Core\Config;
  6. /**
  7. * 定义MVC里的V,即View
  8. * @author gaoxiaogang@gmail.com
  9. */
  10. class View extends Smarty
  11. {
  12. /**
  13. *
  14. * 存放要自动注册的 函数 信息
  15. * @var array
  16. */
  17. static private $auto_registers = array();
  18. private $tpl_type ; //静态文件的类型
  19. private $directory; //模板子目录
  20. public function __construct($directory = '')
  21. {
  22. $this->left_delimiter = "<{";
  23. $this->right_delimiter = "}>";
  24. $this->directory = $directory;
  25. foreach (self::$auto_registers as $tmpV) {
  26. $this->register_function($tmpV['smarty_func'], $tmpV['php_func'], $tmpV['cacheable'], $tmpV['cache_attrs']);
  27. }
  28. }
  29. /**
  30. *
  31. * 类方法.通知模版引擎自动注册函数
  32. * 目的:在使用项目处,方便的自动注册一些全局性的模版函数
  33. * @param string $function 注册后,模版使用时的 函数名
  34. * @param string $function_impl 要被注册的php函数名
  35. * @param bookean $cacheable
  36. * @param unknown_type $cache_attrs
  37. * @author gxg@gaojie100.com
  38. */
  39. static function auto_register_function($function, $function_impl, $cacheable=true, $cache_attrs=null) {
  40. self::$auto_registers[] = array(
  41. 'smarty_func' => $function,
  42. 'php_func' => $function_impl,
  43. 'cacheable' => $cacheable,
  44. 'cache_attrs' => $cache_attrs,
  45. );
  46. }
  47. /**
  48. *
  49. * 渲染模版并打印出来
  50. * @param string $tpl 模版文件名
  51. * @param unknown_type $cache_id
  52. * @param unknown_type $compile_id
  53. * @return NULL 无返回值
  54. */
  55. public function d($tpl, $cache_id = null, $compile_id = null)
  56. {
  57. $this->r($tpl, $cache_id , $compile_id ,true);
  58. }
  59. /**
  60. *
  61. * 渲染模版。默认时返回内容
  62. * @param string $tpl 模版文件名
  63. * @param unknown_type $cache_id
  64. * @param unknown_type $compile_id
  65. * @param boolean $display 是否打印出来?
  66. * @return string
  67. */
  68. public function r($tpl, $cache_id = null, $compile_id = null, $display = false)
  69. {
  70. $smarty_config = Config::getInstance()->get('smarty');
  71. $this->template_dir = $smarty_config['template_dir'];
  72. $this->compile_dir = $smarty_config['compile_dir'];
  73. $this->tpl_type = $smarty_config['tpl_type'] ? $smarty_config['tpl_type'] : 'html';
  74. $content = $this->fetch($tpl . "." . $this->tpl_type, $cache_id , $compile_id , $display);
  75. return $content;
  76. }
  77. }