smarty_internal_compile_extends.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Compile extend
  4. *
  5. * Compiles the {extends} tag
  6. *
  7. * @package Smarty
  8. * @subpackage Compiler
  9. * @author Uwe Tews
  10. */
  11. /**
  12. * Smarty Internal Plugin Compile extend Class
  13. *
  14. * @package Smarty
  15. * @subpackage Compiler
  16. */
  17. class Smarty_Internal_Compile_Extends extends Smarty_Internal_CompileBase {
  18. /**
  19. * Attribute definition: Overwrites base class.
  20. *
  21. * @var array
  22. * @see Smarty_Internal_CompileBase
  23. */
  24. public $required_attributes = array('file');
  25. /**
  26. * Attribute definition: Overwrites base class.
  27. *
  28. * @var array
  29. * @see Smarty_Internal_CompileBase
  30. */
  31. public $shorttag_order = array('file');
  32. /**
  33. * Compiles code for the {extends} tag
  34. *
  35. * @param array $args array with attributes from parser
  36. * @param object $compiler compiler object
  37. * @return string compiled code
  38. */
  39. public function compile($args, $compiler)
  40. {
  41. static $_is_stringy = array('string' => true, 'eval' => true);
  42. $this->_rdl = preg_quote($compiler->smarty->right_delimiter);
  43. $this->_ldl = preg_quote($compiler->smarty->left_delimiter);
  44. $filepath = $compiler->template->source->filepath;
  45. // check and get attributes
  46. $_attr = $this->getAttributes($compiler, $args);
  47. if ($_attr['nocache'] === true) {
  48. $compiler->trigger_template_error('nocache option not allowed', $compiler->lex->taglineno);
  49. }
  50. $_smarty_tpl = $compiler->template;
  51. $include_file = null;
  52. if (strpos($_attr['file'], '$_tmp') !== false) {
  53. $compiler->trigger_template_error('illegal value for file attribute', $compiler->lex->taglineno);
  54. }
  55. eval('$include_file = ' . $_attr['file'] . ';');
  56. // create template object
  57. $_template = new $compiler->smarty->template_class($include_file, $compiler->smarty, $compiler->template);
  58. // save file dependency
  59. if (isset($_is_stringy[$_template->source->type])) {
  60. $template_sha1 = sha1($include_file);
  61. } else {
  62. $template_sha1 = sha1($_template->source->filepath);
  63. }
  64. if (isset($compiler->template->properties['file_dependency'][$template_sha1])) {
  65. $compiler->trigger_template_error("illegal recursive call of \"{$include_file}\"", $compiler->lex->line - 1);
  66. }
  67. $compiler->template->properties['file_dependency'][$template_sha1] = array($_template->source->filepath, $_template->source->timestamp, $_template->source->type);
  68. $_content = substr($compiler->template->source->content, $compiler->lex->counter - 1);
  69. if (preg_match_all("!({$this->_ldl}block\s(.+?){$this->_rdl})!", $_content, $s) !=
  70. preg_match_all("!({$this->_ldl}/block{$this->_rdl})!", $_content, $c)) {
  71. $compiler->trigger_template_error('unmatched {block} {/block} pairs');
  72. }
  73. preg_match_all("!{$this->_ldl}block\s(.+?){$this->_rdl}|{$this->_ldl}/block{$this->_rdl}|{$this->_ldl}\*([\S\s]*?)\*{$this->_rdl}!", $_content, $_result, PREG_OFFSET_CAPTURE);
  74. $_result_count = count($_result[0]);
  75. $_start = 0;
  76. while ($_start+1 < $_result_count) {
  77. $_end = 0;
  78. $_level = 1;
  79. if (substr($_result[0][$_start][0],0,strlen($compiler->smarty->left_delimiter)+1) == $compiler->smarty->left_delimiter.'*') {
  80. $_start++;
  81. continue;
  82. }
  83. while ($_level != 0) {
  84. $_end++;
  85. if (substr($_result[0][$_start + $_end][0],0,strlen($compiler->smarty->left_delimiter)+1) == $compiler->smarty->left_delimiter.'*') {
  86. continue;
  87. }
  88. if (!strpos($_result[0][$_start + $_end][0], '/')) {
  89. $_level++;
  90. } else {
  91. $_level--;
  92. }
  93. }
  94. $_block_content = str_replace($compiler->smarty->left_delimiter . '$smarty.block.parent' . $compiler->smarty->right_delimiter, '%%%%SMARTY_PARENT%%%%',
  95. substr($_content, $_result[0][$_start][1] + strlen($_result[0][$_start][0]), $_result[0][$_start + $_end][1] - $_result[0][$_start][1] - + strlen($_result[0][$_start][0])));
  96. Smarty_Internal_Compile_Block::saveBlockData($_block_content, $_result[0][$_start][0], $compiler->template, $filepath);
  97. $_start = $_start + $_end + 1;
  98. }
  99. if ($_template->source->type == 'extends') {
  100. $_template->block_data = $compiler->template->block_data;
  101. }
  102. $compiler->template->source->content = $_template->source->content;
  103. if ($_template->source->type == 'extends') {
  104. $compiler->template->block_data = $_template->block_data;
  105. foreach ($_template->source->components as $key => $component) {
  106. $compiler->template->properties['file_dependency'][$key] = array($component->filepath, $component->timestamp, $component->type);
  107. }
  108. }
  109. $compiler->template->source->filepath = $_template->source->filepath;
  110. $compiler->abort_and_recompile = true;
  111. return '';
  112. }
  113. }
  114. ?>