You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

smarty_internal_runtime_subtemplate.php 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /**
  3. * Sub Template Runtime Methods render, setupSubTemplate
  4. *
  5. * @package Smarty
  6. * @subpackage PluginsInternal
  7. * @author Uwe Tews
  8. *
  9. **/
  10. class Smarty_Internal_Runtime_SubTemplate
  11. {
  12. /**
  13. * Subtemplate template object cache
  14. *
  15. * @var Smarty_Internal_Template[]
  16. */
  17. public $tplObjects = array();
  18. /**
  19. * Subtemplate call count
  20. *
  21. * @var int[]
  22. */
  23. public $subTplInfo = array();
  24. /**
  25. * Runtime function to render subtemplate
  26. *
  27. * @param \Smarty_Internal_Template $parent
  28. * @param string $template template name
  29. * @param mixed $cache_id cache id
  30. * @param mixed $compile_id compile id
  31. * @param integer $caching cache mode
  32. * @param integer $cache_lifetime life time of cache data
  33. * @param array $data passed parameter template variables
  34. * @param int $scope scope in which {include} should execute
  35. * @param bool $forceTplCache cache template object
  36. * @param string $uid file dependency uid
  37. * @param string $content_func function name
  38. *
  39. */
  40. public function render(Smarty_Internal_Template $parent, $template, $cache_id, $compile_id, $caching,
  41. $cache_lifetime, $data, $scope, $forceTplCache, $uid = null, $content_func = null)
  42. {
  43. // if there are cached template objects calculate $templateID
  44. $_templateId =
  45. !empty($this->tplObjects) ? $parent->smarty->_getTemplateId($template, $cache_id, $compile_id, $caching) :
  46. null;
  47. // already in template cache?
  48. /* @var Smarty_Internal_Template $tpl */
  49. if (isset($_templateId) && isset($this->tplObjects[$_templateId])) {
  50. // clone cached template object because of possible recursive call
  51. $tpl = clone $this->tplObjects[$_templateId];
  52. $tpl->parent = $parent;
  53. // if $caching mode changed the compiled resource is invalid
  54. if ((bool) $tpl->caching !== (bool) $caching) {
  55. unset($tpl->compiled);
  56. }
  57. // get variables from calling scope
  58. $tpl->tpl_vars = $parent->tpl_vars;
  59. $tpl->config_vars = $parent->config_vars;
  60. // get template functions
  61. $tpl->tpl_function = $parent->tpl_function;
  62. // copy inheritance object?
  63. if (isset($parent->ext->_inheritance)) {
  64. $tpl->ext->_inheritance = $parent->ext->_inheritance;
  65. } else {
  66. unset($tpl->ext->_inheritance);
  67. }
  68. } else {
  69. $tpl = clone $parent;
  70. $tpl->parent = $parent;
  71. if (!isset($tpl->templateId) || $tpl->templateId !== $_templateId) {
  72. $tpl->templateId = $_templateId;
  73. $tpl->template_resource = $template;
  74. $tpl->cache_id = $cache_id;
  75. $tpl->compile_id = $compile_id;
  76. if (isset($uid)) {
  77. // for inline templates we can get all resource information from file dependency
  78. if (isset($tpl->compiled->file_dependency[$uid])) {
  79. list($filepath, $timestamp, $resource) = $tpl->compiled->file_dependency[$uid];
  80. $tpl->source =
  81. new Smarty_Template_Source(isset($tpl->smarty->_cache['resource_handlers'][$resource]) ?
  82. $tpl->smarty->_cache['resource_handlers'][$resource] :
  83. Smarty_Resource::load($tpl->smarty, $resource), $tpl->smarty,
  84. $filepath, $resource, $filepath);
  85. $tpl->source->filepath = $filepath;
  86. $tpl->source->timestamp = $timestamp;
  87. $tpl->source->exists = true;
  88. $tpl->source->uid = $uid;
  89. } else {
  90. $tpl->source = null;
  91. }
  92. } else {
  93. $tpl->source = null;
  94. }
  95. if (!isset($tpl->source)) {
  96. $tpl->source = Smarty_Template_Source::load($tpl);
  97. unset($tpl->compiled);
  98. }
  99. unset($tpl->cached);
  100. }
  101. }
  102. $tpl->caching = $caching;
  103. $tpl->cache_lifetime = $cache_lifetime;
  104. if ($caching == 9999) {
  105. $tpl->cached = $parent->cached;
  106. }
  107. // set template scope
  108. $tpl->scope = $scope;
  109. $scopePtr = false;
  110. if ($scope & ~Smarty::SCOPE_BUBBLE_UP) {
  111. if ($scope == Smarty::SCOPE_GLOBAL) {
  112. $tpl->tpl_vars = Smarty::$global_tpl_vars;
  113. $tpl->config_vars = $tpl->smarty->config_vars;
  114. $scopePtr = true;
  115. } else {
  116. if ($scope == Smarty::SCOPE_PARENT) {
  117. $scopePtr = $parent;
  118. } elseif ($scope == Smarty::SCOPE_SMARTY) {
  119. $scopePtr = $tpl->smarty;
  120. } else {
  121. $scopePtr = $tpl;
  122. while (isset($scopePtr->parent)) {
  123. if ($scopePtr->parent->_objType != 2 && $scope & Smarty::SCOPE_TPL_ROOT) {
  124. break;
  125. }
  126. $scopePtr = $scopePtr->parent;
  127. }
  128. }
  129. $tpl->tpl_vars = $scopePtr->tpl_vars;
  130. $tpl->config_vars = $scopePtr->config_vars;
  131. }
  132. }
  133. if (!isset($this->tplObjects[$tpl->_getTemplateId()]) && !$tpl->source->handler->recompiled) {
  134. // if template is called multiple times set flag to to cache template objects
  135. $forceTplCache = $forceTplCache ||
  136. (isset($this->subTplInfo[$tpl->template_resource]) && $this->subTplInfo[$tpl->template_resource] > 1);
  137. // check if template object should be cached
  138. if ($tpl->parent->_objType == 2 && isset($this->tplObjects[$tpl->parent->templateId]) ||
  139. ($forceTplCache && $tpl->smarty->resource_cache_mode & Smarty::RESOURCE_CACHE_AUTOMATIC) ||
  140. ($tpl->smarty->resource_cache_mode & Smarty::RESOURCE_CACHE_ON)
  141. ) {
  142. $this->tplObjects[$tpl->_getTemplateId()] = $tpl;
  143. }
  144. }
  145. if (!empty($data)) {
  146. // set up variable values
  147. foreach ($data as $_key => $_val) {
  148. $tpl->tpl_vars[$_key] = new Smarty_Variable($_val);
  149. }
  150. }
  151. if (isset($uid)) {
  152. if ($parent->smarty->debugging) {
  153. $parent->smarty->_debug->start_template($tpl);
  154. $parent->smarty->_debug->start_render($tpl);
  155. }
  156. $tpl->compiled->getRenderedTemplateCode($tpl, $content_func);
  157. if ($parent->smarty->debugging) {
  158. $parent->smarty->_debug->end_template($tpl);
  159. $parent->smarty->_debug->end_render($tpl);
  160. }
  161. if ($tpl->caching == 9999 && $tpl->compiled->has_nocache_code) {
  162. $parent->cached->hashes[$tpl->compiled->nocache_hash] = true;
  163. }
  164. } else {
  165. if (isset($tpl->compiled)) {
  166. $tpl->compiled->render($tpl);
  167. } else {
  168. $tpl->render();
  169. }
  170. }
  171. if ($scopePtr) {
  172. if ($scope == Smarty::SCOPE_GLOBAL) {
  173. Smarty::$global_tpl_vars = $tpl->tpl_vars;
  174. $tpl->smarty->config_vars = $tpl->config_vars;
  175. } else {
  176. $scopePtr->tpl_vars = $tpl->tpl_vars;
  177. $scopePtr->config_vars = $tpl->config_vars;
  178. }
  179. }
  180. }
  181. /**
  182. * Get called subtemplates from compiled template and save call count
  183. *
  184. * @param \Smarty_Internal_Template $tpl
  185. */
  186. public function registerSubTemplates(Smarty_Internal_Template $tpl)
  187. {
  188. foreach ($tpl->compiled->includes as $name => $count) {
  189. if (isset($this->subTplInfo[$name])) {
  190. $this->subTplInfo[$name] += $count;
  191. } else {
  192. $this->subTplInfo[$name] = $count;
  193. }
  194. }
  195. }
  196. }