smarty_internal_compile_capture.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 object $compiler compiler object
  37. *
  38. * @return string compiled code
  39. */
  40. public function compile($args, $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->_capture_stack[0][] = array($buffer, $assign, $append); ob_start(); ?>";
  51. return $_output;
  52. }
  53. }
  54. /**
  55. * Smarty Internal Plugin Compile Captureclose Class
  56. *
  57. * @package Smarty
  58. * @subpackage Compiler
  59. */
  60. class Smarty_Internal_Compile_CaptureClose extends Smarty_Internal_CompileBase
  61. {
  62. /**
  63. * Compiles code for the {/capture} tag
  64. *
  65. * @param array $args array with attributes from parser
  66. * @param object $compiler compiler object
  67. *
  68. * @return string compiled code
  69. */
  70. public function compile($args, $compiler)
  71. {
  72. // check and get attributes
  73. $_attr = $this->getAttributes($compiler, $args);
  74. // must endblock be nocache?
  75. if ($compiler->nocache) {
  76. $compiler->tag_nocache = true;
  77. }
  78. list($buffer, $assign, $append, $compiler->nocache) = array_pop($compiler->_capture_stack[0]);
  79. $_output = "<?php list(\$_capture_buffer, \$_capture_assign, \$_capture_append) = array_pop(\$_smarty_tpl->_capture_stack[0]);\n";
  80. $_output .= "if (!empty(\$_capture_buffer)) {\n";
  81. $_output .= " if (isset(\$_capture_assign)) \$_smarty_tpl->assign(\$_capture_assign, ob_get_contents());\n";
  82. $_output .= " if (isset( \$_capture_append)) \$_smarty_tpl->append( \$_capture_append, ob_get_contents());\n";
  83. $_output .= " Smarty::\$_smarty_vars['capture'][\$_capture_buffer]=ob_get_clean();\n";
  84. $_output .= "} else \$_smarty_tpl->capture_error();?>";
  85. return $_output;
  86. }
  87. }