/app/smarty/sysplugins/smarty_internal_compile_include.php

https://github.com/pws5068/RSNCorp · PHP · 145 lines · 122 code · 1 blank · 22 comment · 5 complexity · 53d97215cd0721041fd77d5b2e67a316 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. eval("\$tmp = $include_file;");
  36. if ($this->compiler->template->template_resource != $tmp) {
  37. $tpl = $compiler->smarty->createTemplate ($tmp, $compiler->template->cache_id, $compiler->template->compile_id, $compiler->template);
  38. if ($this->compiler->template->caching) {
  39. // needs code for cached page but no cache file
  40. $tpl->caching = 9999;
  41. }
  42. if ($tpl->resource_object->usesCompiler && $tpl->isExisting()) {
  43. // make sure that template is up to date and merge template properties
  44. $tpl->renderTemplate();
  45. // compiled code for {function} tags
  46. $compiler->template->properties['function'] = array_merge($compiler->template->properties['function'], $tpl->properties['function']);
  47. // get compiled code
  48. $compiled_tpl = $tpl->getCompiledTemplate();
  49. // remove header code
  50. $compiled_tpl = preg_replace("/(<\?php \/\*%%SmartyHeaderCode:{$tpl->properties['nocache_hash']}%%\*\/(.+?)\/\*\/%%SmartyHeaderCode%%\*\/\?>\n)/s", '', $compiled_tpl);
  51. if ($tpl->has_nocache_code) {
  52. // replace nocache_hash
  53. $compiled_tpl = preg_replace("/{$tpl->properties['nocache_hash']}/", $compiler->template->properties['nocache_hash'], $compiled_tpl);
  54. $compiler->template->has_nocache_code = true;
  55. }
  56. $has_compiled_template = true;
  57. }
  58. }
  59. }
  60. }
  61. if (isset($_attr['assign'])) {
  62. // output will be stored in a smarty variable instead of beind displayed
  63. $_assign = $_attr['assign'];
  64. }
  65. $_parent_scope = SMARTY_LOCAL_SCOPE;
  66. if (isset($_attr['scope'])) {
  67. if ($_attr['scope'] == '\'parent\'') {
  68. $_parent_scope = SMARTY_PARENT_SCOPE;
  69. } elseif ($_attr['scope'] == '\'root\'') {
  70. $_parent_scope = SMARTY_ROOT_SCOPE;
  71. } elseif ($_attr['scope'] == '\'global\'') {
  72. $_parent_scope = SMARTY_GLOBAL_SCOPE;
  73. }
  74. }
  75. $_caching = 'null';
  76. // default for included templates
  77. if ($this->compiler->template->caching && !$this->compiler->nocache) {
  78. $_caching = 9999;
  79. }
  80. /*
  81. * if the {include} tag provides individual parameter for caching
  82. * it will not be included into the common cache file and treated like
  83. * a nocache section
  84. */
  85. if (isset($_attr['cache_lifetime'])) {
  86. $_cache_lifetime = $_attr['cache_lifetime'];
  87. $this->compiler->tag_nocache = true;
  88. $_caching = SMARTY_CACHING_LIFETIME_CURRENT;
  89. } else {
  90. $_cache_lifetime = 'null';
  91. }
  92. if (isset($_attr['nocache'])) {
  93. if ($_attr['nocache'] == 'true') {
  94. $this->compiler->tag_nocache = true;
  95. $_caching = SMARTY_CACHING_OFF;
  96. }
  97. }
  98. if (isset($_attr['caching'])) {
  99. if ($_attr['caching'] == 'true') {
  100. $_caching = SMARTY_CACHING_LIFETIME_CURRENT;
  101. } else {
  102. $this->compiler->tag_nocache = true;
  103. $_caching = SMARTY_CACHING_OFF;
  104. }
  105. }
  106. // create template object
  107. $_output = "<?php \$_template = new {$compiler->smarty->template_class}($include_file, \$_smarty_tpl->smarty, \$_smarty_tpl, \$_smarty_tpl->cache_id, \$_smarty_tpl->compile_id, $_caching, $_cache_lifetime);\n";
  108. // delete {include} standard attributes
  109. unset($_attr['file'], $_attr['assign'], $_attr['cache_lifetime'], $_attr['nocache'], $_attr['caching'], $_attr['scope'], $_attr['inline']);
  110. // remaining attributes must be assigned as smarty variable
  111. if (!empty($_attr)) {
  112. if ($_parent_scope == SMARTY_LOCAL_SCOPE) {
  113. // create variables
  114. foreach ($_attr as $_key => $_value) {
  115. $_output .= "\$_template->assign('$_key',$_value);";
  116. }
  117. } else {
  118. $this->compiler->trigger_template_error('variable passing not allowed in parent/global scope');
  119. }
  120. }
  121. // was there an assign attribute
  122. if (isset($_assign)) {
  123. $_output .= "\$_smarty_tpl->assign($_assign,\$_template->getRenderedTemplate());?>";
  124. } else {
  125. if ($has_compiled_template && !($compiler->template->caching && ($this->compiler->tag_nocache || $this->compiler->nocache))) {
  126. $_output .= "\$_template->properties['nocache_hash'] = '{$compiler->template->properties['nocache_hash']}';\n";
  127. $_output .= "\$_tpl_stack[] = \$_smarty_tpl; \$_smarty_tpl = \$_template;?>\n";
  128. $_output .= $compiled_tpl;
  129. $_output .= "<?php \$_smarty_tpl->updateParentVariables($_parent_scope);?>\n";
  130. $_output .= "<?php /* End of included template \"" . $tpl->getTemplateFilepath() . "\" */ ?>\n";
  131. $_output .= "<?php \$_smarty_tpl = array_pop(\$_tpl_stack);?>";
  132. } else {
  133. $_output .= " echo \$_template->getRenderedTemplate();?>";
  134. $_output .= "<?php \$_template->updateParentVariables($_parent_scope);?>";
  135. }
  136. }
  137. $_output .= "<?php unset(\$_template);?>\n";
  138. return $_output;
  139. }
  140. }
  141. ?>