ViewBlock.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. /**
  3. *
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @since CakePHP(tm) v2.1
  15. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  16. */
  17. /**
  18. * ViewBlock implements the concept of Blocks or Slots in the View layer.
  19. * Slots or blocks are combined with extending views and layouts to afford slots
  20. * of content that are present in a layout or parent view, but are defined by the child
  21. * view or elements used in the view.
  22. *
  23. * @package Cake.View
  24. */
  25. class ViewBlock {
  26. /**
  27. * Append content
  28. *
  29. * @var string
  30. */
  31. const APPEND = 'append';
  32. /**
  33. * Prepend content
  34. *
  35. * @var string
  36. */
  37. const PREPEND = 'prepend';
  38. /**
  39. * Block content. An array of blocks indexed by name.
  40. *
  41. * @var array
  42. */
  43. protected $_blocks = array();
  44. /**
  45. * The active blocks being captured.
  46. *
  47. * @var array
  48. */
  49. protected $_active = array();
  50. /**
  51. * Should the currently captured content be discarded on ViewBlock::end()
  52. *
  53. * @var boolean
  54. * @see ViewBlock::end()
  55. * @see ViewBlock::startIfEmpty()
  56. */
  57. protected $_discardActiveBufferOnEnd = false;
  58. /**
  59. * Start capturing output for a 'block'
  60. *
  61. * Blocks allow you to create slots or blocks of dynamic content in the layout.
  62. * view files can implement some or all of a layout's slots.
  63. *
  64. * You can end capturing blocks using View::end(). Blocks can be output
  65. * using View::get();
  66. *
  67. * @param string $name The name of the block to capture for.
  68. * @throws CakeException When starting a block twice
  69. * @return void
  70. */
  71. public function start($name) {
  72. if (in_array($name, $this->_active)) {
  73. throw new CakeException(__("A view block with the name '%s' is already/still open.", $name));
  74. }
  75. $this->_active[] = $name;
  76. ob_start();
  77. }
  78. /**
  79. * Start capturing output for a 'block' if it is empty
  80. *
  81. * Blocks allow you to create slots or blocks of dynamic content in the layout.
  82. * view files can implement some or all of a layout's slots.
  83. *
  84. * You can end capturing blocks using View::end(). Blocks can be output
  85. * using View::get();
  86. *
  87. * @param string $name The name of the block to capture for.
  88. * @return void
  89. */
  90. public function startIfEmpty($name) {
  91. if (empty($this->_blocks[$name])) {
  92. return $this->start($name);
  93. }
  94. $this->_discardActiveBufferOnEnd = true;
  95. ob_start();
  96. }
  97. /**
  98. * End a capturing block. The compliment to ViewBlock::start()
  99. *
  100. * @return void
  101. * @see ViewBlock::start()
  102. */
  103. public function end() {
  104. if ($this->_discardActiveBufferOnEnd) {
  105. $this->_discardActiveBufferOnEnd = false;
  106. ob_end_clean();
  107. return;
  108. }
  109. if (!empty($this->_active)) {
  110. $active = end($this->_active);
  111. $content = ob_get_clean();
  112. if (!isset($this->_blocks[$active])) {
  113. $this->_blocks[$active] = '';
  114. }
  115. $this->_blocks[$active] .= $content;
  116. array_pop($this->_active);
  117. }
  118. }
  119. /**
  120. * Concat content to an existing or new block.
  121. * Concating to a new block will create the block.
  122. *
  123. * Calling concat() without a value will create a new capturing
  124. * block that needs to be finished with View::end(). The content
  125. * of the new capturing context will be added to the existing block context.
  126. *
  127. * @param string $name Name of the block
  128. * @param mixed $value The content for the block
  129. * @param string $mode If ViewBlock::APPEND content will be appended to existing content.
  130. * If ViewBlock::PREPEND it will be prepended.
  131. * @return void
  132. */
  133. public function concat($name, $value = null, $mode = ViewBlock::APPEND) {
  134. if (isset($value)) {
  135. if (!isset($this->_blocks[$name])) {
  136. $this->_blocks[$name] = '';
  137. }
  138. if ($mode === ViewBlock::PREPEND) {
  139. $this->_blocks[$name] = $value . $this->_blocks[$name];
  140. } else {
  141. $this->_blocks[$name] .= $value;
  142. }
  143. } else {
  144. $this->start($name);
  145. }
  146. }
  147. /**
  148. * Append to an existing or new block. Appending to a new
  149. * block will create the block.
  150. *
  151. * Calling append() without a value will create a new capturing
  152. * block that needs to be finished with View::end(). The content
  153. * of the new capturing context will be added to the existing block context.
  154. *
  155. * @param string $name Name of the block
  156. * @param string $value The content for the block.
  157. * @return void
  158. * @deprecated As of 2.3 use ViewBlock::concat() instead.
  159. */
  160. public function append($name, $value = null) {
  161. $this->concat($name, $value);
  162. }
  163. /**
  164. * Set the content for a block. This will overwrite any
  165. * existing content.
  166. *
  167. * @param string $name Name of the block
  168. * @param mixed $value The content for the block.
  169. * @return void
  170. */
  171. public function set($name, $value) {
  172. $this->_blocks[$name] = (string)$value;
  173. }
  174. /**
  175. * Get the content for a block.
  176. *
  177. * @param string $name Name of the block
  178. * @param string $default Default string
  179. * @return string The block content or $default if the block does not exist.
  180. */
  181. public function get($name, $default = '') {
  182. if (!isset($this->_blocks[$name])) {
  183. return $default;
  184. }
  185. return $this->_blocks[$name];
  186. }
  187. /**
  188. * Get the names of all the existing blocks.
  189. *
  190. * @return array An array containing the blocks.
  191. */
  192. public function keys() {
  193. return array_keys($this->_blocks);
  194. }
  195. /**
  196. * Get the name of the currently open block.
  197. *
  198. * @return mixed Either null or the name of the last open block.
  199. */
  200. public function active() {
  201. return end($this->_active);
  202. }
  203. /**
  204. * Get the names of the unclosed/active blocks.
  205. *
  206. * @return array An array of unclosed blocks.
  207. */
  208. public function unclosed() {
  209. return $this->_active;
  210. }
  211. }