smarty_internal_compile_capture.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Compile Capture
  4. *
  5. * Compiles the {capture} tag
  6. *
  7. * @package Smarty
  8. * @subpackage Compiler
  9. * @author Uwe Tews
  10. */
  11. /**
  12. * Smarty Internal Plugin Compile Capture Class
  13. *
  14. * @package Smarty
  15. * @subpackage Compiler
  16. */
  17. class Smarty_Internal_Compile_Capture extends Smarty_Internal_CompileBase {
  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. * @return string compiled code
  38. */
  39. public function compile($args, $compiler)
  40. {
  41. // check and get attributes
  42. $_attr = $this->getAttributes($compiler, $args);
  43. $buffer = isset($_attr['name']) ? $_attr['name'] : "'default'";
  44. $assign = isset($_attr['assign']) ? $_attr['assign'] : 'null';
  45. $append = isset($_attr['append']) ? $_attr['append'] : 'null';
  46. $compiler->_capture_stack[] = array($buffer, $assign, $append, $compiler->nocache);
  47. // maybe nocache because of nocache variables
  48. $compiler->nocache = $compiler->nocache | $compiler->tag_nocache;
  49. $_output = "<?php \$_smarty_tpl->_capture_stack[] = array($buffer, $assign, $append); ob_start(); ?>";
  50. return $_output;
  51. }
  52. }
  53. /**
  54. * Smarty Internal Plugin Compile Captureclose Class
  55. *
  56. * @package Smarty
  57. * @subpackage Compiler
  58. */
  59. class Smarty_Internal_Compile_CaptureClose extends Smarty_Internal_CompileBase {
  60. /**
  61. * Compiles code for the {/capture} tag
  62. *
  63. * @param array $args array with attributes from parser
  64. * @param object $compiler compiler object
  65. * @return string compiled code
  66. */
  67. public function compile($args, $compiler)
  68. {
  69. // check and get attributes
  70. $_attr = $this->getAttributes($compiler, $args);
  71. // must endblock be nocache?
  72. if ($compiler->nocache) {
  73. $compiler->tag_nocache = true;
  74. }
  75. list($buffer, $assign, $append, $compiler->nocache) = array_pop($compiler->_capture_stack);
  76. $_output = "<?php list(\$_capture_buffer, \$_capture_assign, \$_capture_append) = array_pop(\$_smarty_tpl->_capture_stack);\n";
  77. $_output .= "if (!empty(\$_capture_buffer)) {\n";
  78. $_output .= " if (isset(\$_capture_assign)) \$_smarty_tpl->assign(\$_capture_assign, ob_get_contents());\n";
  79. $_output .= " if (isset( \$_capture_append)) \$_smarty_tpl->append( \$_capture_append, ob_get_contents());\n";
  80. $_output .= " Smarty::\$_smarty_vars['capture'][\$_capture_buffer]=ob_get_clean();\n";
  81. $_output .= "} else \$_smarty_tpl->capture_error();?>";
  82. return $_output;
  83. }
  84. }
  85. ?>