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_private_block_plugin.php 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Compile Block Plugin
  4. * Compiles code for the execution of block plugin
  5. *
  6. * @package Smarty
  7. * @subpackage Compiler
  8. * @author Uwe Tews
  9. */
  10. /**
  11. * Smarty Internal Plugin Compile Block Plugin Class
  12. *
  13. * @package Smarty
  14. * @subpackage Compiler
  15. */
  16. class Smarty_Internal_Compile_Private_Block_Plugin extends Smarty_Internal_CompileBase
  17. {
  18. /**
  19. * Attribute definition: Overwrites base class.
  20. *
  21. * @var array
  22. * @see Smarty_Internal_CompileBase
  23. */
  24. public $optional_attributes = array('_any');
  25. /**
  26. * nesting level
  27. *
  28. * @var int
  29. */
  30. public $nesting = 0;
  31. /**
  32. * Compiles code for the execution of block plugin
  33. *
  34. * @param array $args array with attributes from parser
  35. * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
  36. * @param array $parameter array with compilation parameter
  37. * @param string $tag name of block plugin
  38. * @param string $function PHP function name
  39. *
  40. * @return string compiled code
  41. * @throws \SmartyCompilerException
  42. * @throws \SmartyException
  43. */
  44. public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter, $tag, $function = null)
  45. {
  46. if (!isset($tag[ 5 ]) || substr($tag, - 5) !== 'close') {
  47. // opening tag of block plugin
  48. // check and get attributes
  49. $_attr = $this->getAttributes($compiler, $args);
  50. $this->nesting ++;
  51. unset($_attr[ 'nocache' ]);
  52. list($callback, $_paramsArray, $callable) = $this->setup($compiler, $_attr, $tag, $function);
  53. $_params = 'array(' . implode(',', $_paramsArray) . ')';
  54. // compile code
  55. $output = "<?php ";
  56. if (is_array($callback)) {
  57. $output .= "\$_block_plugin{$this->nesting} = isset({$callback[0]}) ? {$callback[0]} : null;\n";
  58. $callback = "\$_block_plugin{$this->nesting}{$callback[1]}";
  59. }
  60. if (isset($callable)) {
  61. $output .= "if (!is_callable({$callable})) {\nthrow new SmartyException('block tag \'{$tag}\' not callable or registered');\n}\n";
  62. }
  63. $output .= "\$_smarty_tpl->smarty->_cache['_tag_stack'][] = array('{$tag}', {$_params});\n";
  64. $output .= "\$_block_repeat=true;\necho {$callback}({$_params}, null, \$_smarty_tpl, \$_block_repeat);\nwhile (\$_block_repeat) {\nob_start();?>";
  65. $this->openTag($compiler, $tag, array($_params, $compiler->nocache, $callback));
  66. // maybe nocache because of nocache variables or nocache plugin
  67. $compiler->nocache = $compiler->nocache | $compiler->tag_nocache;
  68. } else {
  69. // must endblock be nocache?
  70. if ($compiler->nocache) {
  71. $compiler->tag_nocache = true;
  72. }
  73. // closing tag of block plugin, restore nocache
  74. list($_params, $compiler->nocache, $callback) = $this->closeTag($compiler, substr($tag, 0, - 5));
  75. // compile code
  76. if (!isset($parameter[ 'modifier_list' ])) {
  77. $mod_pre = $mod_post = $mod_content = '';
  78. $mod_content2 = 'ob_get_clean()';
  79. } else {
  80. $mod_content2 = "\$_block_content{$this->nesting}";
  81. $mod_content = "\$_block_content{$this->nesting} = ob_get_clean();\n";
  82. $mod_pre = "ob_start();\n";
  83. $mod_post = 'echo ' . $compiler->compileTag('private_modifier', array(),
  84. array('modifierlist' => $parameter[ 'modifier_list' ],
  85. 'value' => 'ob_get_clean()')) . ";\n";
  86. }
  87. $output = "<?php {$mod_content}\$_block_repeat=false;\n{$mod_pre}echo {$callback}({$_params}, {$mod_content2}, \$_smarty_tpl, \$_block_repeat);\n{$mod_post}}\n";
  88. $output .= 'array_pop($_smarty_tpl->smarty->_cache[\'_tag_stack\']);?>';
  89. }
  90. return $output;
  91. }
  92. /**
  93. * Setup callback and parameter array
  94. *
  95. * @param \Smarty_Internal_TemplateCompilerBase $compiler
  96. * @param array $_attr attributes
  97. * @param string $tag
  98. * @param string $function
  99. *
  100. * @return array
  101. */
  102. public function setup(Smarty_Internal_TemplateCompilerBase $compiler, $_attr, $tag, $function)
  103. {
  104. $_paramsArray = array();
  105. foreach ($_attr as $_key => $_value) {
  106. if (is_int($_key)) {
  107. $_paramsArray[] = "$_key=>$_value";
  108. } else {
  109. $_paramsArray[] = "'$_key'=>$_value";
  110. }
  111. }
  112. return array($function, $_paramsArray, null);
  113. }
  114. }