Base for a static organization website

ViewBlock.php 5.9KB

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