You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

smarty_internal_compile_block.php 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. /*
  3. * This file is part of Smarty.
  4. *
  5. * (c) 2015 Uwe Tews
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * Smarty Internal Plugin Compile Block Class
  12. *
  13. * @author Uwe Tews <uwe.tews@googlemail.com>
  14. */
  15. class Smarty_Internal_Compile_Block extends Smarty_Internal_Compile_Shared_Inheritance
  16. {
  17. /**
  18. * Attribute definition: Overwrites base class.
  19. *
  20. * @var array
  21. * @see Smarty_Internal_CompileBase
  22. */
  23. public $required_attributes = array('name');
  24. /**
  25. * Attribute definition: Overwrites base class.
  26. *
  27. * @var array
  28. * @see Smarty_Internal_CompileBase
  29. */
  30. public $shorttag_order = array('name');
  31. /**
  32. * Attribute definition: Overwrites base class.
  33. *
  34. * @var array
  35. * @see Smarty_Internal_CompileBase
  36. */
  37. public $option_flags = array('hide', 'nocache');
  38. /**
  39. * Attribute definition: Overwrites base class.
  40. *
  41. * @var array
  42. * @see Smarty_Internal_CompileBase
  43. */
  44. public $optional_attributes = array('assign');
  45. /**
  46. * Compiles code for the {block} tag
  47. *
  48. * @param array $args array with attributes from parser
  49. * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
  50. * @param array $parameter array with compilation parameter
  51. *
  52. */
  53. public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
  54. {
  55. if (!isset($compiler->_cache[ 'blockNesting' ])) {
  56. $compiler->_cache[ 'blockNesting' ] = 0;
  57. }
  58. if ($compiler->_cache[ 'blockNesting' ] === 0) {
  59. // make sure that inheritance gets initialized in template code
  60. $this->registerInit($compiler);
  61. $this->option_flags = array('hide', 'nocache', 'append', 'prepend');
  62. } else {
  63. $this->option_flags = array('hide', 'nocache');
  64. }
  65. // check and get attributes
  66. $_attr = $this->getAttributes($compiler, $args);
  67. ++$compiler->_cache[ 'blockNesting' ];
  68. $_className = 'Block_' . preg_replace('![^\w]+!', '_', uniqid(rand(), true));
  69. $compiler->_cache[ 'blockName' ][ $compiler->_cache[ 'blockNesting' ] ] = $_attr[ 'name' ];
  70. $compiler->_cache[ 'blockClass' ][ $compiler->_cache[ 'blockNesting' ] ] = $_className;
  71. $compiler->_cache[ 'blockParams' ][ $compiler->_cache[ 'blockNesting' ] ] = array();
  72. $compiler->_cache[ 'blockParams' ][ 1 ][ 'subBlocks' ][ trim($_attr[ 'name' ], '"\'') ][] = $_className;
  73. $this->openTag($compiler,
  74. 'block',
  75. array($_attr, $compiler->nocache, $compiler->parser->current_buffer,
  76. $compiler->template->compiled->has_nocache_code,
  77. $compiler->template->caching));
  78. $compiler->saveRequiredPlugins(true);
  79. $compiler->nocache = $compiler->nocache | $compiler->tag_nocache;
  80. $compiler->parser->current_buffer = new Smarty_Internal_ParseTree_Template();
  81. $compiler->template->compiled->has_nocache_code = false;
  82. $compiler->suppressNocacheProcessing = true;
  83. }
  84. }
  85. /**
  86. * Smarty Internal Plugin Compile BlockClose Class
  87. *
  88. */
  89. class Smarty_Internal_Compile_Blockclose extends Smarty_Internal_Compile_Shared_Inheritance
  90. {
  91. /**
  92. * Compiles code for the {/block} tag
  93. *
  94. * @param array $args array with attributes from parser
  95. * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
  96. * @param array $parameter array with compilation parameter
  97. *
  98. * @return bool true
  99. */
  100. public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
  101. {
  102. list($_attr, $_nocache, $_buffer, $_has_nocache_code, $_caching) = $this->closeTag($compiler, array('block'));
  103. // init block parameter
  104. $_block = $compiler->_cache[ 'blockParams' ][ $compiler->_cache[ 'blockNesting' ] ];
  105. unset($compiler->_cache[ 'blockParams' ][ $compiler->_cache[ 'blockNesting' ] ]);
  106. $_name = $_attr[ 'name' ];
  107. $_assign = isset($_attr[ 'assign' ]) ? $_attr[ 'assign' ] : null;
  108. unset($_attr[ 'assign' ], $_attr[ 'name' ]);
  109. foreach ($_attr as $name => $stat) {
  110. if ((is_bool($stat) && $stat !== false) || (!is_bool($stat) && $stat !== 'false')) {
  111. $_block[ $name ] = 'true';
  112. }
  113. }
  114. $_className = $compiler->_cache[ 'blockClass' ][ $compiler->_cache[ 'blockNesting' ] ];
  115. // get compiled block code
  116. $_functionCode = $compiler->parser->current_buffer;
  117. // setup buffer for template function code
  118. $compiler->parser->current_buffer = new Smarty_Internal_ParseTree_Template();
  119. $output = "<?php\n";
  120. $output .= "/* {block {$_name}} */\n";
  121. $output .= "class {$_className} extends Smarty_Internal_Block\n";
  122. $output .= "{\n";
  123. foreach ($_block as $property => $value) {
  124. $output .= "public \${$property} = " . var_export($value,true) .";\n";
  125. }
  126. $output .= "public function callBlock(Smarty_Internal_Template \$_smarty_tpl) {\n";
  127. $output .= $compiler->compileRequiredPlugins();
  128. $compiler->restoreRequiredPlugins();
  129. if ($compiler->template->compiled->has_nocache_code) {
  130. $output .= "\$_smarty_tpl->cached->hashes['{$compiler->template->compiled->nocache_hash}'] = true;\n";
  131. }
  132. if (isset($_assign)) {
  133. $output .= "ob_start();\n";
  134. }
  135. $output .= "?>\n";
  136. $compiler->parser->current_buffer->append_subtree($compiler->parser,
  137. new Smarty_Internal_ParseTree_Tag($compiler->parser,
  138. $output));
  139. $compiler->parser->current_buffer->append_subtree($compiler->parser, $_functionCode);
  140. $output = "<?php\n";
  141. if (isset($_assign)) {
  142. $output .= "\$_smarty_tpl->assign({$_assign}, ob_get_clean());\n";
  143. }
  144. $output .= "}\n";
  145. $output .= "}\n";
  146. $output .= "/* {/block {$_name}} */\n\n";
  147. $output .= "?>\n";
  148. $compiler->parser->current_buffer->append_subtree($compiler->parser,
  149. new Smarty_Internal_ParseTree_Tag($compiler->parser,
  150. $output));
  151. $compiler->blockOrFunctionCode .= $compiler->parser->current_buffer->to_smarty_php($compiler->parser);
  152. $compiler->parser->current_buffer = new Smarty_Internal_ParseTree_Template();
  153. // restore old status
  154. $compiler->template->compiled->has_nocache_code = $_has_nocache_code;
  155. $compiler->tag_nocache = $compiler->nocache;
  156. $compiler->nocache = $_nocache;
  157. $compiler->parser->current_buffer = $_buffer;
  158. $output = "<?php \n";
  159. if ($compiler->_cache[ 'blockNesting' ] === 1) {
  160. $output .= "\$_smarty_tpl->inheritance->instanceBlock(\$_smarty_tpl, '$_className', $_name);\n";
  161. } else {
  162. $output .= "\$_smarty_tpl->inheritance->instanceBlock(\$_smarty_tpl, '$_className', $_name, \$this->tplIndex);\n";
  163. }
  164. $output .= "?>\n";
  165. --$compiler->_cache[ 'blockNesting' ];
  166. if ($compiler->_cache[ 'blockNesting' ] === 0) {
  167. unset($compiler->_cache[ 'blockNesting' ]);
  168. }
  169. $compiler->has_code = true;
  170. $compiler->suppressNocacheProcessing = true;
  171. return $output;
  172. }
  173. }