/mzz/libs/smarty/sysplugins/smarty_internal_compile_include.php

https://github.com/greevex/mzz-framework-blank-application · PHP · 168 lines · 144 code · 2 blank · 22 comment · 8 complexity · b251584078f3d755c6255870fb7ab664 MD5 · raw file

  1. <?php
  2. /**
  3. * Smarty Internal Plugin Compile Include
  4. *
  5. * Compiles the {include} tag
  6. *
  7. * @package Smarty
  8. * @subpackage Compiler
  9. * @author Uwe Tews
  10. */
  11. /**
  12. * Smarty Internal Plugin Compile Include Class
  13. */
  14. class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase {
  15. /**
  16. * Compiles code for the {include} tag
  17. *
  18. * @param array $args array with attributes from parser
  19. * @param object $compiler compiler object
  20. * @return string compiled code
  21. */
  22. public function compile($args, $compiler)
  23. {
  24. $this->compiler = $compiler;
  25. $this->required_attributes = array('file');
  26. $this->optional_attributes = array('_any');
  27. // check and get attributes
  28. $_attr = $this->_get_attributes($args);
  29. // save posible attributes
  30. $include_file = $_attr['file'];
  31. $has_compiled_template = false;
  32. if ($compiler->smarty->merge_compiled_includes || isset($_attr['inline'])) {
  33. // check if compiled code can be merged (contains no variable part)
  34. if (!$compiler->has_variable_string && (substr_count($include_file, '"') == 2 or substr_count($include_file, "'") == 2) and substr_count($include_file, '(') == 0) {
  35. $tmp = null;
  36. eval("\$tmp = $include_file;");
  37. if ($this->compiler->template->template_resource != $tmp) {
  38. $tpl = new $compiler->smarty->template_class ($tmp, $compiler->smarty, $compiler->template, $compiler->template->cache_id, $compiler->template->compile_id);
  39. if ($this->compiler->template->caching) {
  40. // needs code for cached page but no cache file
  41. $tpl->caching = 9999;
  42. }
  43. if ($this->compiler->template->mustCompile) {
  44. // make sure whole chain gest compiled
  45. $tpl->mustCompile = true;
  46. }
  47. if ($tpl->resource_object->usesCompiler && $tpl->isExisting()) {
  48. // get compiled code
  49. $compiled_tpl = $tpl->getCompiledTemplate();
  50. // merge compiled code for {function} tags
  51. $compiler->template->properties['function'] = array_merge($compiler->template->properties['function'], $tpl->properties['function']);
  52. // merge filedependency by evaluating header code
  53. preg_match_all("/(<\?php \/\*%%SmartyHeaderCode:{$tpl->properties['nocache_hash']}%%\*\/(.+?)\/\*\/%%SmartyHeaderCode%%\*\/\?>\n)/s", $compiled_tpl, $result);
  54. $saved_has_nocache_code = $compiler->template->has_nocache_code;
  55. $saved_nocache_hash = $compiler->template->properties['nocache_hash'];
  56. $_smarty_tpl = $compiler->template;
  57. eval($result[2][0]);
  58. $compiler->template->properties['nocache_hash'] = $saved_nocache_hash;
  59. $compiler->template->has_nocache_code = $saved_has_nocache_code;
  60. // remove header code
  61. $compiled_tpl = preg_replace("/(<\?php \/\*%%SmartyHeaderCode:{$tpl->properties['nocache_hash']}%%\*\/(.+?)\/\*\/%%SmartyHeaderCode%%\*\/\?>\n)/s", '', $compiled_tpl);
  62. if ($tpl->has_nocache_code) {
  63. // replace nocache_hash
  64. $compiled_tpl = preg_replace("/{$tpl->properties['nocache_hash']}/", $compiler->template->properties['nocache_hash'], $compiled_tpl);
  65. $compiler->template->has_nocache_code = true;
  66. }
  67. $has_compiled_template = true;
  68. }
  69. }
  70. }
  71. }
  72. if (isset($_attr['assign'])) {
  73. // output will be stored in a smarty variable instead of beind displayed
  74. $_assign = $_attr['assign'];
  75. }
  76. $_parent_scope = SMARTY_LOCAL_SCOPE;
  77. if (isset($_attr['scope'])) {
  78. $_attr['scope'] = trim($_attr['scope'], "'\"");
  79. if ($_attr['scope'] == 'parent') {
  80. $_parent_scope = SMARTY_PARENT_SCOPE;
  81. } elseif ($_attr['scope'] == 'root') {
  82. $_parent_scope = SMARTY_ROOT_SCOPE;
  83. } elseif ($_attr['scope'] == 'global') {
  84. $_parent_scope = SMARTY_GLOBAL_SCOPE;
  85. }
  86. }
  87. $_caching = 'null';
  88. if ($this->compiler->nocache || $this->compiler->tag_nocache) {
  89. $_caching = SMARTY_CACHING_OFF;
  90. }
  91. // default for included templates
  92. if ($this->compiler->template->caching && !$this->compiler->nocache && !$this->compiler->tag_nocache) {
  93. $_caching = 9999;
  94. }
  95. /*
  96. * if the {include} tag provides individual parameter for caching
  97. * it will not be included into the common cache file and treated like
  98. * a nocache section
  99. */
  100. if (isset($_attr['cache_lifetime'])) {
  101. $_cache_lifetime = $_attr['cache_lifetime'];
  102. $this->compiler->tag_nocache = true;
  103. $_caching = SMARTY_CACHING_LIFETIME_CURRENT;
  104. } else {
  105. $_cache_lifetime = 'null';
  106. }
  107. if (isset($_attr['cache_id'])) {
  108. $_cache_id = $_attr['cache_id'];
  109. $this->compiler->tag_nocache = true;
  110. $_caching = SMARTY_CACHING_LIFETIME_CURRENT;
  111. } else {
  112. $_cache_id = '$_smarty_tpl->cache_id';
  113. }
  114. if (isset($_attr['nocache'])) {
  115. if (trim($_attr['nocache'], "'\"") == 'true') {
  116. $this->compiler->tag_nocache = true;
  117. $_caching = SMARTY_CACHING_OFF;
  118. }
  119. }
  120. if (isset($_attr['caching'])) {
  121. if (trim($_attr['caching'], "'\"") == 'true') {
  122. $_caching = SMARTY_CACHING_LIFETIME_CURRENT;
  123. } else {
  124. $this->compiler->tag_nocache = true;
  125. $_caching = SMARTY_CACHING_OFF;
  126. }
  127. }
  128. // create template object
  129. $_output = "<?php \$_template = new {$compiler->smarty->template_class}($include_file, \$_smarty_tpl->smarty, \$_smarty_tpl, $_cache_id, \$_smarty_tpl->compile_id, $_caching, $_cache_lifetime);\n";
  130. // delete {include} standard attributes
  131. unset($_attr['file'], $_attr['assign'], $_attr['cache_id'], $_attr['cache_lifetime'], $_attr['nocache'], $_attr['caching'], $_attr['scope'], $_attr['inline']);
  132. // remaining attributes must be assigned as smarty variable
  133. if (!empty($_attr)) {
  134. if ($_parent_scope == SMARTY_LOCAL_SCOPE) {
  135. // create variables
  136. foreach ($_attr as $_key => $_value) {
  137. $_output .= "\$_template->assign('$_key',$_value);";
  138. }
  139. } else {
  140. $this->compiler->trigger_template_error('variable passing not allowed in parent/global scope', $this->compiler->lex->taglineno);
  141. }
  142. }
  143. // was there an assign attribute
  144. if (isset($_assign)) {
  145. $_output .= "\$_smarty_tpl->assign($_assign,\$_template->getRenderedTemplate());?>";
  146. } else {
  147. if ($has_compiled_template && !($compiler->template->caching && ($this->compiler->tag_nocache || $this->compiler->nocache))) {
  148. $_output .= "\$_template->properties['nocache_hash'] = '{$compiler->template->properties['nocache_hash']}';\n";
  149. $_output .= "\$_tpl_stack[] = \$_smarty_tpl; \$_smarty_tpl = \$_template;?>\n";
  150. $_output .= $compiled_tpl;
  151. $_output .= "<?php \$_smarty_tpl->updateParentVariables($_parent_scope);?>\n";
  152. $_output .= "<?php /* End of included template \"" . $tpl->getTemplateFilepath() . "\" */ ?>\n";
  153. $_output .= "<?php \$_smarty_tpl = array_pop(\$_tpl_stack);?>";
  154. } else {
  155. $_output .= " echo \$_template->getRenderedTemplate();?>";
  156. $_output .= "<?php \$_template->updateParentVariables($_parent_scope);?>";
  157. }
  158. }
  159. $_output .= "<?php unset(\$_template);?>";
  160. return $_output;
  161. }
  162. }
  163. ?>