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_capture.php 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Compile Capture
  4. * Compiles the {capture} tag
  5. *
  6. * @package Smarty
  7. * @subpackage Compiler
  8. * @author Uwe Tews
  9. */
  10. /**
  11. * Smarty Internal Plugin Compile Capture Class
  12. *
  13. * @package Smarty
  14. * @subpackage Compiler
  15. */
  16. class Smarty_Internal_Compile_Capture extends Smarty_Internal_CompileBase
  17. {
  18. /**
  19. * Attribute definition: Overwrites base class.
  20. *
  21. * @var array
  22. * @see Smarty_Internal_CompileBase
  23. */
  24. public $shorttag_order = array('name');
  25. /**
  26. * Attribute definition: Overwrites base class.
  27. *
  28. * @var array
  29. * @see Smarty_Internal_CompileBase
  30. */
  31. public $optional_attributes = array('name', 'assign', 'append');
  32. /**
  33. * Compiles code for the {capture} tag
  34. *
  35. * @param array $args array with attributes from parser
  36. * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
  37. *
  38. * @return string compiled code
  39. */
  40. public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler)
  41. {
  42. // check and get attributes
  43. $_attr = $this->getAttributes($compiler, $args);
  44. $buffer = isset($_attr['name']) ? $_attr['name'] : "'default'";
  45. $assign = isset($_attr['assign']) ? $_attr['assign'] : 'null';
  46. $append = isset($_attr['append']) ? $_attr['append'] : 'null';
  47. $compiler->_capture_stack[0][] = array($buffer, $assign, $append, $compiler->nocache);
  48. // maybe nocache because of nocache variables
  49. $compiler->nocache = $compiler->nocache | $compiler->tag_nocache;
  50. $_output = "<?php \$_smarty_tpl->_cache['capture_stack'][] = array($buffer, $assign, $append); ob_start(); ?>";
  51. return $_output;
  52. }
  53. /**
  54. * Compiles code for the {$smarty.capture.xxx}
  55. *
  56. * @param array $args array with attributes from parser
  57. * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
  58. * @param array $parameter array with compilation parameter
  59. *
  60. * @return string compiled code
  61. * @throws \SmartyCompilerException
  62. */
  63. public static function compileSpecialVariable($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
  64. {
  65. $tag = strtolower(trim($parameter[ 0 ], '"\''));
  66. $name = isset($parameter[ 1 ]) ? $compiler->getId($parameter[ 1 ]) : false;
  67. if (!$name) {
  68. $compiler->trigger_template_error("missing or illegal \$smarty.{$tag} name attribute", null, true);
  69. }
  70. return "(isset(\$_smarty_tpl->_cache['__smarty_capture']['{$name}']) ? \$_smarty_tpl->_cache['__smarty_capture']['{$name}'] : null)";
  71. }
  72. }
  73. /**
  74. * Smarty Internal Plugin Compile Captureclose Class
  75. *
  76. * @package Smarty
  77. * @subpackage Compiler
  78. */
  79. class Smarty_Internal_Compile_CaptureClose extends Smarty_Internal_CompileBase
  80. {
  81. /**
  82. * Compiles code for the {/capture} tag
  83. *
  84. * @param array $args array with attributes from parser
  85. * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
  86. *
  87. * @return string compiled code
  88. */
  89. public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler)
  90. {
  91. // check and get attributes
  92. $_attr = $this->getAttributes($compiler, $args);
  93. // must endblock be nocache?
  94. if ($compiler->nocache) {
  95. $compiler->tag_nocache = true;
  96. }
  97. list($buffer, $assign, $append, $compiler->nocache) = array_pop($compiler->_capture_stack[0]);
  98. $_output = "<?php list(\$_capture_buffer, \$_capture_assign, \$_capture_append) = array_pop(\$_smarty_tpl->_cache['capture_stack']);\n";
  99. $_output .= "if (!empty(\$_capture_buffer)) {\n";
  100. $_output .= " if (isset(\$_capture_assign)) \$_smarty_tpl->assign(\$_capture_assign, ob_get_contents());\n";
  101. $_output .= " if (isset( \$_capture_append)) \$_smarty_tpl->append( \$_capture_append, ob_get_contents());\n";
  102. $_output .= "\$_smarty_tpl->_cache['__smarty_capture'][\$_capture_buffer]=ob_get_clean();\n";
  103. $_output .= "} else \$_smarty_tpl->capture_error();?>";
  104. return $_output;
  105. }
  106. }