View.class.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. // +--------------------------------------------------------------------------
  3. // | Senthot [ DEVELOPED BY ME ]
  4. // +--------------------------------------------------------------------------
  5. // | Copyright (c) 2005-2013 http://www.senthot.com All rights reserved.
  6. // | License ( http://www.apache.org/licenses/LICENSE-2.0 )
  7. // | Author: ms134n ( [email protected] )
  8. // +--------------------------------------------------------------------------
  9. /**
  10. * Senthot View class
  11. * @category Sen
  12. * @package Sen
  13. * @subpackage Core
  14. * @author ms134n <[email protected]>
  15. */
  16. class View {
  17. /**
  18. * Template output variables
  19. * @var tVar
  20. * @access protected
  21. */
  22. protected $tVar = array();
  23. /**
  24. * Template variable assignment
  25. * @access public
  26. * @param mixed $name
  27. * @param mixed $value
  28. */
  29. public function assign($name,$value=''){
  30. if(is_array($name)) {
  31. $this->tVar = array_merge($this->tVar,$name);
  32. }else {
  33. $this->tVar[$name] = $value;
  34. }
  35. }
  36. /**
  37. * Template variable value obtained
  38. * @access public
  39. * @param string $name
  40. * @return mixed
  41. */
  42. public function get($name=''){
  43. if('' === $name) {
  44. return $this->tVar;
  45. }
  46. return isset($this->tVar[$name])?$this->tVar[$name]:false;
  47. }
  48. /**
  49. * Loaded templates and page output Can return output
  50. * @access public
  51. * @param string $templateFile Template file name
  52. * @param string $charset Template output character set
  53. * @param string $contentType Output Type
  54. * @param string $content Template output
  55. * @param string $prefix Template cache prefix
  56. * @return mixed
  57. */
  58. public function display($templateFile='',$charset='',$contentType='',$content='',$prefix='') {
  59. G('viewStartTime');
  60. // View of the start tag
  61. tag('view_begin',$templateFile);
  62. // Parse and get the template content
  63. $content = $this->fetch($templateFile,$content,$prefix);
  64. // Output template content
  65. $this->render($content,$charset,$contentType);
  66. // View closing tag
  67. tag('view_end');
  68. }
  69. /**
  70. * Html output text can include
  71. * @access private
  72. * @param string $content Output
  73. * @param string $charset Template output character set
  74. * @param string $contentType Output Type
  75. * @return mixed
  76. */
  77. private function render($content,$charset='',$contentType=''){
  78. if(empty($charset)) $charset = C('DEFAULT_CHARSET');
  79. if(empty($contentType)) $contentType = C('TMPL_CONTENT_TYPE');
  80. // Web Character Encoding
  81. header('Content-Type:'.$contentType.'; charset='.$charset);
  82. header('Cache-control: '.C('HTTP_CACHE_CONTROL')); // Page cache control
  83. header('X-Powered-By:Senthot');
  84. // Output template file
  85. echo $content;
  86. }
  87. /**
  88. * Parse and access template content For outputs
  89. * @access public
  90. * @param string $templateFile Template file name
  91. * @param string $content Template output
  92. * @param string $prefix Template cache prefix
  93. * @return string
  94. */
  95. public function fetch($templateFile='',$content='',$prefix='') {
  96. if(empty($content)) {
  97. // Template file parsing tags
  98. tag('view_template',$templateFile);
  99. // Template file does not exist return directly
  100. if(!is_file($templateFile)) return NULL;
  101. }
  102. // Page cache
  103. ob_start();
  104. ob_implicit_flush(0);
  105. if('php' == strtolower(C('TMPL_ENGINE_TYPE'))) { // Using native PHP template
  106. // Template array variable decomposed into independent variable
  107. extract($this->tVar, EXTR_OVERWRITE);
  108. // PHP template loaded directly
  109. empty($content)?include $templateFile:eval('?>'.$content);
  110. }else{
  111. // View Resolution tab
  112. $params = array('var'=>$this->tVar,'file'=>$templateFile,'content'=>$content,'prefix'=>$prefix);
  113. tag('view_parse',$params);
  114. }
  115. // Obtain and empty the cache
  116. $content = ob_get_clean();
  117. // Content Filtering tab
  118. tag('view_filter',$content);
  119. // Output template file
  120. return $content;
  121. }
  122. }