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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. * Runtime Extension Capture
  4. *
  5. * @package Smarty
  6. * @subpackage PluginsInternal
  7. * @author Uwe Tews
  8. */
  9. class Smarty_Internal_Runtime_Capture
  10. {
  11. /**
  12. * Flag that this instance will not be cached
  13. *
  14. * @var bool
  15. */
  16. public $isPrivateExtension = true;
  17. /**
  18. * Stack of capture parameter
  19. *
  20. * @var array
  21. */
  22. private $captureStack = array();
  23. /**
  24. * Current open capture sections
  25. *
  26. * @var int
  27. */
  28. private $captureCount = 0;
  29. /**
  30. * Count stack
  31. *
  32. * @var int[]
  33. */
  34. private $countStack = array();
  35. /**
  36. * Named buffer
  37. *
  38. * @var string[]
  39. */
  40. private $namedBuffer = array();
  41. /**
  42. * Flag if callbacks are registered
  43. *
  44. * @var bool
  45. */
  46. private $isRegistered = false;
  47. /**
  48. * Open capture section
  49. *
  50. * @param \Smarty_Internal_Template $_template
  51. * @param string $buffer capture name
  52. * @param string $assign variable name
  53. * @param string $append variable name
  54. */
  55. public function open(Smarty_Internal_Template $_template, $buffer, $assign, $append)
  56. {
  57. if (!$this->isRegistered) {
  58. $this->register($_template);
  59. }
  60. $this->captureStack[] = array($buffer,
  61. $assign,
  62. $append);
  63. $this->captureCount ++;
  64. ob_start();
  65. }
  66. /**
  67. * Register callbacks in template class
  68. *
  69. * @param \Smarty_Internal_Template $_template
  70. */
  71. private function register(Smarty_Internal_Template $_template)
  72. {
  73. $_template->startRenderCallbacks[] = array($this,
  74. 'startRender');
  75. $_template->endRenderCallbacks[] = array($this,
  76. 'endRender');
  77. $this->startRender($_template);
  78. $this->isRegistered = true;
  79. }
  80. /**
  81. * Start render callback
  82. *
  83. * @param \Smarty_Internal_Template $_template
  84. */
  85. public function startRender(Smarty_Internal_Template $_template)
  86. {
  87. $this->countStack[] = $this->captureCount;
  88. $this->captureCount = 0;
  89. }
  90. /**
  91. * Close capture section
  92. *
  93. * @param \Smarty_Internal_Template $_template
  94. *
  95. * @throws \SmartyException
  96. */
  97. public function close(Smarty_Internal_Template $_template)
  98. {
  99. if ($this->captureCount) {
  100. list($buffer, $assign, $append) = array_pop($this->captureStack);
  101. $this->captureCount --;
  102. if (isset($assign)) {
  103. $_template->assign($assign, ob_get_contents());
  104. }
  105. if (isset($append)) {
  106. $_template->append($append, ob_get_contents());
  107. }
  108. $this->namedBuffer[ $buffer ] = ob_get_clean();
  109. } else {
  110. $this->error($_template);
  111. }
  112. }
  113. /**
  114. * Error exception on not matching {capture}{/capture}
  115. *
  116. * @param \Smarty_Internal_Template $_template
  117. *
  118. * @throws \SmartyException
  119. */
  120. public function error(Smarty_Internal_Template $_template)
  121. {
  122. throw new SmartyException("Not matching {capture}{/capture} in '{$_template->template_resource}'");
  123. }
  124. /**
  125. * Return content of named capture buffer by key or as array
  126. *
  127. * @param \Smarty_Internal_Template $_template
  128. * @param string|null $name
  129. *
  130. * @return string|string[]|null
  131. */
  132. public function getBuffer(Smarty_Internal_Template $_template, $name = null)
  133. {
  134. if (isset($name)) {
  135. return isset($this->namedBuffer[ $name ]) ? $this->namedBuffer[ $name ] : null;
  136. } else {
  137. return $this->namedBuffer;
  138. }
  139. }
  140. /**
  141. * End render callback
  142. *
  143. * @param \Smarty_Internal_Template $_template
  144. *
  145. * @throws \SmartyException
  146. */
  147. public function endRender(Smarty_Internal_Template $_template)
  148. {
  149. if ($this->captureCount) {
  150. $this->error($_template);
  151. } else {
  152. $this->captureCount = array_pop($this->countStack);
  153. }
  154. }
  155. }