smarty_internal_compile_private_block_plugin.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. */
  42. public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter, $tag, $function = null)
  43. {
  44. if (!isset($tag[ 5 ]) || substr($tag, - 5) != 'close') {
  45. // opening tag of block plugin
  46. // check and get attributes
  47. $_attr = $this->getAttributes($compiler, $args);
  48. $this->nesting ++;
  49. unset($_attr[ 'nocache' ]);
  50. list($callback, $_paramsArray, $callable) = $this->setup($compiler, $_attr, $tag, $function);
  51. $_params = 'array(' . implode(",", $_paramsArray) . ')';
  52. // compile code
  53. $output = "<?php ";
  54. if (is_array($callback)) {
  55. $output .= "\$_block_plugin{$this->nesting} = isset({$callback[0]}) ? {$callback[0]} : null;\n";
  56. $callback = "\$_block_plugin{$this->nesting}{$callback[1]}";
  57. }
  58. if (isset($callable)) {
  59. $output .= "if (!is_callable({$callable})) {\nthrow new SmartyException('block tag \'{$tag}\' not callable or registered');\n}\n";
  60. }
  61. $output .= "\$_smarty_tpl->smarty->_cache['_tag_stack'][] = array('{$tag}', {$_params});\n";
  62. $output .= "\$_block_repeat{$this->nesting}=true;\necho {$callback}({$_params}, null, \$_smarty_tpl, \$_block_repeat{$this->nesting});\nwhile (\$_block_repeat{$this->nesting}) {\nob_start();\n?>";
  63. $this->openTag($compiler, $tag, array($_params, $compiler->nocache, $callback));
  64. // maybe nocache because of nocache variables or nocache plugin
  65. $compiler->nocache = $compiler->nocache | $compiler->tag_nocache;
  66. } else {
  67. // must endblock be nocache?
  68. if ($compiler->nocache) {
  69. $compiler->tag_nocache = true;
  70. }
  71. // closing tag of block plugin, restore nocache
  72. list($_params, $compiler->nocache, $callback) = $this->closeTag($compiler, substr($tag, 0, - 5));
  73. //Does tag create output
  74. $compiler->has_output = isset($_params[ 'assign' ]) ? false : true;
  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{$this->nesting}=false;\n" . $mod_pre .
  88. "echo {$callback}({$_params}, " . $mod_content2 .
  89. ", \$_smarty_tpl, \$_block_repeat{$this->nesting});\n" . $mod_post . "}\n";
  90. $output .= "array_pop(\$_smarty_tpl->smarty->_cache['_tag_stack']);";
  91. $output .= "?>";
  92. $this->nesting --;
  93. }
  94. return $output . "\n";
  95. }
  96. /**
  97. * Setup callback and parameter array
  98. *
  99. * @param \Smarty_Internal_TemplateCompilerBase $compiler
  100. * @param array $_attr attributes
  101. * @param string $tag
  102. * @param string $function
  103. *
  104. * @return array
  105. */
  106. public function setup(Smarty_Internal_TemplateCompilerBase $compiler, $_attr, $tag, $function)
  107. {
  108. $_paramsArray = array();
  109. foreach ($_attr as $_key => $_value) {
  110. if (is_int($_key)) {
  111. $_paramsArray[] = "$_key=>$_value";
  112. } else {
  113. $_paramsArray[] = "'$_key'=>$_value";
  114. }
  115. }
  116. return array($function, $_paramsArray, null);
  117. }
  118. }