PageRenderTime 76ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Core/ThinkPHP/Lib/Core/View.class.php

https://bitbucket.org/thinkphp/cms
PHP | 130 lines | 50 code | 6 blank | 74 comment | 8 complexity | 23e0ce761d06d298a0197d8fc936b74d MD5 | raw file
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2012 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. /**
  12. * ThinkPHP 视图类
  13. * @category Think
  14. * @package Think
  15. * @subpackage Core
  16. * @author liu21st <liu21st@gmail.com>
  17. */
  18. class View {
  19. /**
  20. * 模板输出变量
  21. * @var tVar
  22. * @access protected
  23. */
  24. protected $tVar = array();
  25. /**
  26. * 模板变量赋值
  27. * @access public
  28. * @param mixed $name
  29. * @param mixed $value
  30. */
  31. public function assign($name,$value=''){
  32. if(is_array($name)) {
  33. $this->tVar = array_merge($this->tVar,$name);
  34. }else {
  35. $this->tVar[$name] = $value;
  36. }
  37. }
  38. /**
  39. * 取得模板变量的值
  40. * @access public
  41. * @param string $name
  42. * @return mixed
  43. */
  44. public function get($name=''){
  45. if('' === $name) {
  46. return $this->tVar;
  47. }
  48. return isset($this->tVar[$name])?$this->tVar[$name]:false;
  49. }
  50. /**
  51. * 加载模板和页面输出 可以返回输出内容
  52. * @access public
  53. * @param string $templateFile 模板文件名
  54. * @param string $charset 模板输出字符集
  55. * @param string $contentType 输出类型
  56. * @param string $content 模板输出内容
  57. * @param string $prefix 模板缓存前缀
  58. * @return mixed
  59. */
  60. public function display($templateFile='',$charset='',$contentType='',$content='',$prefix='') {
  61. G('viewStartTime');
  62. // 视图开始标签
  63. tag('view_begin',$templateFile);
  64. // 解析并获取模板内容
  65. $content = $this->fetch($templateFile,$content,$prefix);
  66. // 输出模板内容
  67. $this->render($content,$charset,$contentType);
  68. // 视图结束标签
  69. tag('view_end');
  70. }
  71. /**
  72. * 输出内容文本可以包括Html
  73. * @access private
  74. * @param string $content 输出内容
  75. * @param string $charset 模板输出字符集
  76. * @param string $contentType 输出类型
  77. * @return mixed
  78. */
  79. private function render($content,$charset='',$contentType=''){
  80. if(empty($charset)) $charset = C('DEFAULT_CHARSET');
  81. if(empty($contentType)) $contentType = C('TMPL_CONTENT_TYPE');
  82. // 网页字符编码
  83. header('Content-Type:'.$contentType.'; charset='.$charset);
  84. header('Cache-control: '.C('HTTP_CACHE_CONTROL')); // 页面缓存控制
  85. header('X-Powered-By:ThinkPHP');
  86. // 输出模板文件
  87. echo $content;
  88. }
  89. /**
  90. * 解析和获取模板内容 用于输出
  91. * @access public
  92. * @param string $templateFile 模板文件名
  93. * @param string $content 模板输出内容
  94. * @param string $prefix 模板缓存前缀
  95. * @return string
  96. */
  97. public function fetch($templateFile='',$content='',$prefix='') {
  98. if(empty($content)) {
  99. // 模板文件解析标签
  100. tag('view_template',$templateFile);
  101. // 模板文件不存在直接返回
  102. if(!is_file($templateFile)) return NULL;
  103. }
  104. // 页面缓存
  105. ob_start();
  106. ob_implicit_flush(0);
  107. if('php' == strtolower(C('TMPL_ENGINE_TYPE'))) { // 使用PHP原生模板
  108. // 模板阵列变量分解成为独立变量
  109. extract($this->tVar, EXTR_OVERWRITE);
  110. // 直接载入PHP模板
  111. empty($content)?include $templateFile:eval('?>'.$content);
  112. }else{
  113. // 视图解析标签
  114. $params = array('var'=>$this->tVar,'file'=>$templateFile,'content'=>$content,'prefix'=>$prefix);
  115. tag('view_parse',$params);
  116. }
  117. // 获取并清空缓存
  118. $content = ob_get_clean();
  119. // 内容过滤标签
  120. tag('view_filter',$content);
  121. // 输出模板文件
  122. return $content;
  123. }
  124. }