PageRenderTime 59ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/modules/smarty/libs/sysplugins/smarty_internal_compile_block.php

https://github.com/pedramphp/alhussain
PHP | 187 lines | 154 code | 6 blank | 27 comment | 25 complexity | aea011fc85c11beb4c947a0d36687af2 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+|'.*'|\".*\")(\s*?)?((append|prepend|nocache)(=true)?)?(\s*{$_rdl})!", $block_tag, $_match)) {
  48. $error_text = 'Syntax Error in template "' . $template->getTemplateFilepath() . '" "' . htmlspecialchars($block_tag) . '" illegal options';
  49. throw new SmartyCompilerException($error_text);
  50. } else {
  51. $_name = trim($_match[3], '\'"');
  52. // replace {$smarty.block.child}
  53. if (strpos($block_content, $template->smarty->left_delimiter . '$smarty.block.child' . $template->smarty->right_delimiter) !== false) {
  54. if (isset($template->block_data[$_name])) {
  55. $block_content = str_replace($template->smarty->left_delimiter . '$smarty.block.child' . $template->smarty->right_delimiter,
  56. $template->block_data[$_name]['source'], $block_content);
  57. unset($template->block_data[$_name]);
  58. } else {
  59. $block_content = str_replace($template->smarty->left_delimiter . '$smarty.block.child' . $template->smarty->right_delimiter,
  60. '', $block_content);
  61. }
  62. }
  63. if (isset($template->block_data[$_name])) {
  64. if (strpos($template->block_data[$_name]['source'], '%%%%SMARTY_PARENT%%%%') !== false) {
  65. $template->block_data[$_name]['source'] =
  66. str_replace('%%%%SMARTY_PARENT%%%%', $block_content, $template->block_data[$_name]['source']);
  67. } elseif ($template->block_data[$_name]['mode'] == 'prepend') {
  68. $template->block_data[$_name]['source'] .= $block_content;
  69. } elseif ($template->block_data[$_name]['mode'] == 'append') {
  70. $template->block_data[$_name]['source'] = $block_content . $template->block_data[$_name]['source'];
  71. }
  72. } else {
  73. $template->block_data[$_name]['source'] = $block_content;
  74. }
  75. if ($_match[6] == 'append') {
  76. $template->block_data[$_name]['mode'] = 'append';
  77. } elseif ($_match[6] == 'prepend') {
  78. $template->block_data[$_name]['mode'] = 'prepend';
  79. } else {
  80. $template->block_data[$_name]['mode'] = 'replace';
  81. }
  82. $template->block_data[$_name]['file'] = $filepath;
  83. }
  84. }
  85. static function compileChildBlock ($compiler, $_name = null)
  86. {
  87. $_output = '';
  88. // if called by {$smarty.block.child} we must search the name of enclosing {block}
  89. if ($_name == null) {
  90. $stack_count = count($compiler->_tag_stack);
  91. while (--$stack_count >= 0) {
  92. if ($compiler->_tag_stack[$stack_count][0] == 'block') {
  93. $_name = trim($compiler->_tag_stack[$stack_count][1][0]['name'] ,"'");
  94. break;
  95. }
  96. }
  97. // flag that child is already compile by {$smarty.block.child} inclusion
  98. $compiler->template->block_data[$_name]['compiled'] = true;
  99. }
  100. if ($_name == null) {
  101. $compiler->trigger_template_error('{$smarty.block.child} used out of context', $this->compiler->lex->taglineno);
  102. }
  103. // undefined child?
  104. if (!isset($compiler->template->block_data[$_name])) {
  105. return '';
  106. }
  107. $_tpl = new Smarty_Internal_template ('eval:' . $compiler->template->block_data[$_name]['source'], $compiler->smarty, $compiler->template, $compiler->template->cache_id,
  108. $compiler->template->compile_id = null, $compiler->template->caching, $compiler->template->cache_lifetime);
  109. $_tpl->properties['nocache_hash'] = $compiler->template->properties['nocache_hash'];
  110. $_tpl->template_filepath = $compiler->template->block_data[$_name]['file'];
  111. if ($compiler->nocache) {
  112. $_tpl->forceNocache = 2;
  113. } else {
  114. $_tpl->forceNocache = 1;
  115. }
  116. $_tpl->suppressHeader = true;
  117. $_tpl->suppressFileDependency = true;
  118. if (strpos($compiler->template->block_data[$_name]['source'], '%%%%SMARTY_PARENT%%%%') !== false) {
  119. $_output = str_replace('%%%%SMARTY_PARENT%%%%', $compiler->parser->current_buffer->to_smarty_php(), $_tpl->getCompiledTemplate());
  120. } elseif ($compiler->template->block_data[$_name]['mode'] == 'prepend') {
  121. $_output = $_tpl->getCompiledTemplate() . $compiler->parser->current_buffer->to_smarty_php();
  122. } elseif ($compiler->template->block_data[$_name]['mode'] == 'append') {
  123. $_output = $compiler->parser->current_buffer->to_smarty_php() . $_tpl->getCompiledTemplate();
  124. } elseif (!empty($compiler->template->block_data[$_name])) {
  125. $_output = $_tpl->getCompiledTemplate();
  126. }
  127. $compiler->template->properties['file_dependency'] = array_merge($compiler->template->properties['file_dependency'], $_tpl->properties['file_dependency']);
  128. $compiler->template->properties['function'] = array_merge($compiler->template->properties['function'], $_tpl->properties['function']);
  129. if ($_tpl->has_nocache_code) {
  130. $compiler->template->has_nocache_code = true;
  131. }
  132. foreach($_tpl->required_plugins as $code => $tmp1) {
  133. foreach($tmp1 as $name => $tmp) {
  134. foreach($tmp as $type => $data) {
  135. $compiler->template->required_plugins[$code][$name][$type] = $data;
  136. }
  137. }
  138. }
  139. unset($_tpl);
  140. return $_output;
  141. }
  142. }
  143. /**
  144. * Smarty Internal Plugin Compile BlockClose Class
  145. */
  146. class Smarty_Internal_Compile_Blockclose extends Smarty_Internal_CompileBase {
  147. /**
  148. * Compiles code for the {/block} tag
  149. *
  150. * @param array $args array with attributes from parser
  151. * @param object $compiler compiler object
  152. * @return string compiled code
  153. */
  154. public function compile($args, $compiler)
  155. {
  156. $this->compiler = $compiler;
  157. $this->smarty = $compiler->smarty;
  158. $this->compiler->has_code = true;
  159. // check and get attributes
  160. $_attr = $this->_get_attributes($args);
  161. $saved_data = $this->_close_tag(array('block'));
  162. $_name = trim($saved_data[0]['name'], "\"'");
  163. if (isset($compiler->template->block_data[$_name]) && !isset($compiler->template->block_data[$_name]['compiled'])) {
  164. $_output = Smarty_Internal_Compile_Block::compileChildBlock($compiler, $_name);
  165. } else {
  166. $_output = $compiler->parser->current_buffer->to_smarty_php();
  167. unset ($compiler->template->block_data[$_name]['compiled']);
  168. }
  169. // reset flags
  170. $compiler->parser->current_buffer = $saved_data[1];
  171. $compiler->nocache = $saved_data[2];
  172. $compiler->smarty->merge_compiled_includes = $saved_data[3];
  173. $compiler->smarty->inheritance = $saved_data[4];
  174. // $_output content has already nocache code processed
  175. $compiler->suppressNocacheProcessing = true;
  176. return $_output;
  177. }
  178. }
  179. ?>