/lib/Cake/View/ViewBlock.php

https://bitbucket.org/ManiAdil/jardinorient · PHP · 231 lines · 76 code · 16 blank · 139 comment · 12 complexity · 4281beb8e863b577dd13a32ca7cb59c0 MD5 · raw file

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