/core/model/smarty/sysplugins/smarty_internal_compile_block.php

https://github.com/hatone/revolution · PHP · 181 lines · 148 code · 6 blank · 27 comment · 25 complexity · 416b15cd211da620eb0e89172bb5a9e0 MD5 · raw file

  1. <?php
  2. /**
  3. * Smarty Internal Plugin Compile Block
  4. *
  5. * Compiles the {block}{/block} tags
  6. *
  7. * @package Smarty
  8. * @subpackage Compiler
  9. * @author Uwe Tews
  10. */
  11. /**
  12. * Smarty Internal Plugin Compile Block Class
  13. */
  14. class Smarty_Internal_Compile_Block extends Smarty_Internal_CompileBase {
  15. // attribute definitions
  16. public $required_attributes = array('name');
  17. public $shorttag_order = array('name');
  18. /**
  19. * Compiles code for the {block} tag
  20. *
  21. * @param array $args array with attributes from parser
  22. * @param object $compiler compiler object
  23. * @return boolean true
  24. */
  25. public function compile($args, $compiler)
  26. {
  27. $this->compiler = $compiler;
  28. // check and get attributes
  29. $_attr = $this->_get_attributes($args);
  30. $save = array($_attr, $compiler->parser->current_buffer, $this->compiler->nocache, $this->compiler->smarty->merge_compiled_includes, $compiler->smarty->inheritance);
  31. $this->_open_tag('block', $save);
  32. if ($_attr['nocache'] == true) {
  33. $compiler->nocache = true;
  34. }
  35. // set flag for {block} tag
  36. $compiler->smarty->inheritance = true;
  37. // must merge includes
  38. $this->compiler->smarty->merge_compiled_includes = true;
  39. $compiler->parser->current_buffer = new _smarty_template_buffer($compiler->parser);
  40. $compiler->has_code = false;
  41. return true;
  42. }
  43. static function saveBlockData($block_content, $block_tag, $template, $filepath)
  44. {
  45. $_rdl = preg_quote($template->smarty->right_delimiter);
  46. $_ldl = preg_quote($template->smarty->left_delimiter);
  47. if (0 == preg_match("!({$_ldl}block\s+)(name=)?(\w+|'\w+'|\"\w+\")(\s*?)?((append|prepend|nocache)(=true)?)?(\s*{$_rdl})!", $block_tag, $_match)) {
  48. $template->compiler_object->trigger_template_error('Illegal {block} tag syntax');
  49. } else {
  50. $_name = trim($_match[3], '\'"');
  51. // replace {$smarty.block.child}
  52. if (strpos($block_content, $template->smarty->left_delimiter . '$smarty.block.child' . $template->smarty->right_delimiter) !== false) {
  53. if (isset($template->block_data[$_name])) {
  54. $block_content = str_replace($template->smarty->left_delimiter . '$smarty.block.child' . $template->smarty->right_delimiter,
  55. $template->block_data[$_name]['source'], $block_content);
  56. unset($template->block_data[$_name]);
  57. } else {
  58. $block_content = str_replace($template->smarty->left_delimiter . '$smarty.block.child' . $template->smarty->right_delimiter,
  59. '', $block_content);
  60. }
  61. }
  62. if (isset($template->block_data[$_name])) {
  63. if (strpos($template->block_data[$_name]['source'], '%%%%SMARTY_PARENT%%%%') !== false) {
  64. $template->block_data[$_name]['source'] =
  65. str_replace('%%%%SMARTY_PARENT%%%%', $block_content, $template->block_data[$_name]['source']);
  66. } elseif ($template->block_data[$_name]['mode'] == 'prepend') {
  67. $template->block_data[$_name]['source'] .= $block_content;
  68. } elseif ($template->block_data[$_name]['mode'] == 'append') {
  69. $template->block_data[$_name]['source'] = $block_content . $template->block_data[$_name]['source'];
  70. }
  71. } else {
  72. $template->block_data[$_name]['source'] = $block_content;
  73. }
  74. if ($_match[6] == 'append') {
  75. $template->block_data[$_name]['mode'] = 'append';
  76. } elseif ($_match[6] == 'prepend') {
  77. $template->block_data[$_name]['mode'] = 'prepend';
  78. } else {
  79. $template->block_data[$_name]['mode'] = 'replace';
  80. }
  81. $template->block_data[$_name]['file'] = $filepath;
  82. }
  83. }
  84. static function compileChildBlock ($compiler, $_name = null)
  85. {
  86. $_output = '';
  87. // if called by {$smarty.block.child} we must search the name of enclosing {block}
  88. if ($_name == null) {
  89. $stack_count = count($compiler->_tag_stack);
  90. while (--$stack_count >= 0) {
  91. if ($compiler->_tag_stack[$stack_count][0] == 'block') {
  92. $_name = trim($compiler->_tag_stack[$stack_count][1][0]['name'] ,"'");
  93. break;
  94. }
  95. }
  96. }
  97. if ($_name == null) {
  98. $compiler->trigger_template_error('{$smarty.block.child} used out of context');
  99. }
  100. $_tpl = new Smarty_Internal_template ('eval:' . $compiler->template->block_data[$_name]['source'], $compiler->smarty, $compiler->template, $compiler->template->cache_id,
  101. $compiler->template->compile_id = null, $compiler->template->caching, $compiler->template->cache_lifetime);
  102. $_tpl->properties['nocache_hash'] = $compiler->template->properties['nocache_hash'];
  103. $_tpl->template_filepath = $compiler->template->block_data[$_name]['file'];
  104. if ($compiler->nocache) {
  105. $_tpl->forceNocache = 2;
  106. } else {
  107. $_tpl->forceNocache = 1;
  108. }
  109. $_tpl->suppressHeader = true;
  110. $_tpl->suppressFileDependency = true;
  111. if (strpos($compiler->template->block_data[$_name]['source'], '%%%%SMARTY_PARENT%%%%') !== false) {
  112. $_output = str_replace('%%%%SMARTY_PARENT%%%%', $compiler->parser->current_buffer->to_smarty_php(), $_tpl->getCompiledTemplate());
  113. } elseif ($compiler->template->block_data[$_name]['mode'] == 'prepend') {
  114. $_output = $_tpl->getCompiledTemplate() . $compiler->parser->current_buffer->to_smarty_php();
  115. } elseif ($compiler->template->block_data[$_name]['mode'] == 'append') {
  116. $_output = $compiler->parser->current_buffer->to_smarty_php() . $_tpl->getCompiledTemplate();
  117. } elseif (!empty($compiler->template->block_data[$_name])) {
  118. $_output = $_tpl->getCompiledTemplate();
  119. }
  120. $compiler->template->properties['file_dependency'] = array_merge($compiler->template->properties['file_dependency'], $_tpl->properties['file_dependency']);
  121. $compiler->template->properties['function'] = array_merge($compiler->template->properties['function'], $_tpl->properties['function']);
  122. if ($_tpl->has_nocache_code) {
  123. $compiler->template->has_nocache_code = true;
  124. }
  125. foreach($_tpl->required_plugins as $code => $tmp1) {
  126. foreach($tmp1 as $name => $tmp) {
  127. foreach($tmp as $type => $data) {
  128. $compiler->template->required_plugins[$code][$name][$type] = $data;
  129. }
  130. }
  131. }
  132. unset($_tpl);
  133. $compiler->template->block_data[$_name]['compiled'] = true;
  134. return $_output;
  135. }
  136. }
  137. /**
  138. * Smarty Internal Plugin Compile BlockClose Class
  139. */
  140. class Smarty_Internal_Compile_Blockclose extends Smarty_Internal_CompileBase {
  141. /**
  142. * Compiles code for the {/block} tag
  143. *
  144. * @param array $args array with attributes from parser
  145. * @param object $compiler compiler object
  146. * @return string compiled code
  147. */
  148. public function compile($args, $compiler)
  149. {
  150. $this->compiler = $compiler;
  151. $this->smarty = $compiler->smarty;
  152. $this->compiler->has_code = true;
  153. // check and get attributes
  154. $_attr = $this->_get_attributes($args);
  155. $saved_data = $this->_close_tag(array('block'));
  156. $_name = trim($saved_data[0]['name'], "\"'");
  157. if (isset($compiler->template->block_data[$_name]) && !isset($compiler->template->block_data[$_name]['compiled'])) {
  158. $_output = Smarty_Internal_Compile_Block::compileChildBlock($compiler, $_name);
  159. } else {
  160. $_output = $compiler->parser->current_buffer->to_smarty_php();
  161. unset ($compiler->template->block_data[$_name]['compiled']);
  162. }
  163. // reset flags
  164. $compiler->parser->current_buffer = $saved_data[1];
  165. $compiler->nocache = $saved_data[2];
  166. $compiler->smarty->merge_compiled_includes = $saved_data[3];
  167. $compiler->smarty->inheritance = $saved_data[4];
  168. // $_output content has already nocache code processed
  169. $compiler->suppressNocacheProcessing = true;
  170. return $_output;
  171. }
  172. }
  173. ?>