/app/smarty/sysplugins/smarty_internal_compile_extends.php

https://github.com/pws5068/RSNCorp · PHP · 110 lines · 83 code · 3 blank · 24 comment · 21 complexity · 4b1a8050858c4f5399742dd83aa337c6 MD5 · raw file

  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. class Smarty_Internal_Compile_Extends extends Smarty_Internal_CompileBase {
  15. /**
  16. * Compiles code for the {extends} 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->smarty = $compiler->smarty;
  26. $this->_rdl = preg_quote($this->smarty->right_delimiter);
  27. $this->_ldl = preg_quote($this->smarty->left_delimiter);
  28. $this->required_attributes = array('file');
  29. // check and get attributes
  30. $_attr = $this->_get_attributes($args);
  31. $_smarty_tpl = $compiler->template;
  32. // $include_file = '';
  33. eval('$include_file = ' . $_attr['file'] . ';');
  34. // create template object
  35. $_template = new $compiler->smarty->template_class($include_file, $this->smarty, $compiler->template);
  36. // save file dependency
  37. $compiler->template->properties['file_dependency'][sha1($_template->getTemplateFilepath())] = array($_template->getTemplateFilepath(), $_template->getTemplateTimestamp());
  38. $_content = $compiler->template->template_source;
  39. if (preg_match_all("!({$this->_ldl}block(.+?){$this->_rdl})!", $_content, $s) !=
  40. preg_match_all("!({$this->_ldl}/block(.*?){$this->_rdl})!", $_content, $c)) {
  41. $this->compiler->trigger_template_error('unmatched {block} {/block} pairs');
  42. }
  43. preg_match_all("!{$this->_ldl}block(.+?){$this->_rdl}|{$this->_ldl}/block.*{$this->_rdl}!", $_content, $_result, PREG_OFFSET_CAPTURE);
  44. $_result_count = count($_result[0]);
  45. $_start = 0;
  46. while ($_start < $_result_count) {
  47. $_end = 0;
  48. $_level = 1;
  49. while ($_level != 0) {
  50. $_end++;
  51. if (!strpos($_result[0][$_start + $_end][0], '/')) {
  52. $_level++;
  53. } else {
  54. $_level--;
  55. }
  56. }
  57. $_block_content = str_replace($this->smarty->left_delimiter . '$smarty.block.parent' . $this->smarty->right_delimiter, '%%%%SMARTY_PARENT%%%%',
  58. 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])));
  59. $this->saveBlockData($_block_content, $_result[0][$_start][0], $compiler->template);
  60. $_start = $_start + $_end + 1;
  61. }
  62. $compiler->template->template_source = $_template->getTemplateSource();
  63. $compiler->template->template_filepath = $_template->getTemplateFilepath();
  64. $compiler->abort_and_recompile = true;
  65. return ' ';
  66. }
  67. protected function saveBlockData($block_content, $block_tag, $template)
  68. {
  69. if (0 == preg_match("!(.?)(name=)(.*?)(?=(\s|{$this->_rdl}))!", $block_tag, $_match)) {
  70. $this->compiler->trigger_template_error("\"" . $block_tag . "\" missing name attribute");
  71. } else {
  72. $_name = trim($_match[3], '\'"');
  73. // replace {$smarty.block.child}
  74. if (strpos($block_content, $this->smarty->left_delimiter . '$smarty.block.child' . $this->smarty->right_delimiter) !== false) {
  75. if (isset($this->smarty->block_data[$_name])) {
  76. $block_content = str_replace($this->smarty->left_delimiter . '$smarty.block.child' . $this->smarty->right_delimiter,
  77. $this->smarty->block_data[$_name]['source'], $block_content);
  78. unset($this->smarty->block_data[$_name]);
  79. } else {
  80. $block_content = str_replace($this->smarty->left_delimiter . '$smarty.block.child' . $this->smarty->right_delimiter,
  81. '', $block_content);
  82. }
  83. }
  84. if (isset($this->smarty->block_data[$_name])) {
  85. if (strpos($this->smarty->block_data[$_name]['source'], '%%%%SMARTY_PARENT%%%%') !== false) {
  86. $this->smarty->block_data[$_name]['source'] =
  87. str_replace('%%%%SMARTY_PARENT%%%%', $block_content, $this->smarty->block_data[$_name]['source']);
  88. } elseif ($this->smarty->block_data[$_name]['mode'] == 'prepend') {
  89. $this->smarty->block_data[$_name]['source'] .= $block_content;
  90. } elseif ($this->smarty->block_data[$_name]['mode'] == 'append') {
  91. $this->smarty->block_data[$_name]['source'] = $block_content . $this->smarty->block_data[$_name]['source'];
  92. }
  93. } else {
  94. $this->smarty->block_data[$_name]['source'] = $block_content;
  95. }
  96. if (preg_match('/(.?)(append)(.*)/', $block_tag, $_match) != 0) {
  97. $this->smarty->block_data[$_name]['mode'] = 'append';
  98. } elseif (preg_match('/(.?)(prepend)(.*)/', $block_tag, $_match) != 0) {
  99. $this->smarty->block_data[$_name]['mode'] = 'prepend';
  100. } else {
  101. $this->smarty->block_data[$_name]['mode'] = 'replace';
  102. }
  103. $this->smarty->block_data[$_name]['file'] = $template->getTemplateFilepath();
  104. }
  105. }
  106. }
  107. ?>