smarty_template_resource_base.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * Smarty Template Resource Base Object
  4. *
  5. * @package Smarty
  6. * @subpackage TemplateResources
  7. * @author Rodney Rehm
  8. */
  9. abstract class Smarty_Template_Resource_Base
  10. {
  11. /**
  12. * Compiled Filepath
  13. *
  14. * @var string
  15. */
  16. public $filepath = null;
  17. /**
  18. * Compiled Timestamp
  19. *
  20. * @var integer|bool
  21. */
  22. public $timestamp = false;
  23. /**
  24. * Compiled Existence
  25. *
  26. * @var boolean
  27. */
  28. public $exists = false;
  29. /**
  30. * Template Compile Id (Smarty_Internal_Template::$compile_id)
  31. *
  32. * @var string
  33. */
  34. public $compile_id = null;
  35. /**
  36. * Compiled Content Loaded
  37. *
  38. * @var boolean
  39. */
  40. public $processed = false;
  41. /**
  42. * unique function name for compiled template code
  43. *
  44. * @var string
  45. */
  46. public $unifunc = '';
  47. /**
  48. * flag if template does contain nocache code sections
  49. *
  50. * @var bool
  51. */
  52. public $has_nocache_code = false;
  53. /**
  54. * resource file dependency
  55. *
  56. * @var array
  57. */
  58. public $file_dependency = array();
  59. /**
  60. * Content buffer
  61. *
  62. * @var string
  63. */
  64. public $content = null;
  65. /**
  66. * required plugins
  67. *
  68. * @var array
  69. */
  70. public $required_plugins = array();
  71. /**
  72. * Included subtemplates
  73. *
  74. * @var array
  75. */
  76. public $includes = array();
  77. /**
  78. * Flag if this is a cache resource
  79. *
  80. * @var bool
  81. */
  82. public $isCache = false;
  83. /**
  84. * Process resource
  85. *
  86. * @param Smarty_Internal_Template $_template template object
  87. */
  88. abstract public function process(Smarty_Internal_Template $_template);
  89. /**
  90. * get rendered template content by calling compiled or cached template code
  91. *
  92. * @param \Smarty_Internal_Template $_template
  93. * @param string $unifunc function with template code
  94. *
  95. * @throws \Exception
  96. */
  97. public function getRenderedTemplateCode(Smarty_Internal_Template $_template, $unifunc = null)
  98. {
  99. $smarty = &$_template->smarty;
  100. $_template->isRenderingCache = $this->isCache;
  101. $level = ob_get_level();
  102. try {
  103. if (!isset($unifunc)) {
  104. $unifunc = $this->unifunc;
  105. }
  106. if (empty($unifunc) || !function_exists($unifunc)) {
  107. throw new SmartyException("Invalid compiled template for '{$_template->template_resource}'");
  108. }
  109. if ($_template->startRenderCallbacks) {
  110. foreach ($_template->startRenderCallbacks as $callback) {
  111. call_user_func($callback, $_template);
  112. }
  113. }
  114. $unifunc($_template);
  115. foreach ($_template->endRenderCallbacks as $callback) {
  116. call_user_func($callback, $_template);
  117. }
  118. $_template->isRenderingCache = false;
  119. }
  120. catch (Exception $e) {
  121. $_template->isRenderingCache = false;
  122. while (ob_get_level() > $level) {
  123. ob_end_clean();
  124. }
  125. if (isset($smarty->security_policy)) {
  126. $smarty->security_policy->endTemplate();
  127. }
  128. throw $e;
  129. }
  130. }
  131. /**
  132. * Get compiled time stamp
  133. *
  134. * @return int
  135. */
  136. public function getTimeStamp()
  137. {
  138. if ($this->exists && !$this->timestamp) {
  139. $this->timestamp = filemtime($this->filepath);
  140. }
  141. return $this->timestamp;
  142. }
  143. }