ViewBlock.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. * PHP 5
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright 2005-2011, 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-2011, 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. * Block content. An array of blocks indexed by name.
  27. *
  28. * @var array
  29. */
  30. protected $_blocks = array();
  31. /**
  32. * The active blocks being captured.
  33. *
  34. * @var array
  35. */
  36. protected $_active = array();
  37. /**
  38. * Start capturing output for a 'block'
  39. *
  40. * Blocks allow you to create slots or blocks of dynamic content in the layout.
  41. * view files can implement some or all of a layout's slots.
  42. *
  43. * You can end capturing blocks using View::end(). Blocks can be output
  44. * using View::get();
  45. *
  46. * @param string $name The name of the block to capture for.
  47. * @return void
  48. */
  49. public function start($name) {
  50. $this->_active[] = $name;
  51. ob_start();
  52. }
  53. /**
  54. * End a capturing block. The compliment to ViewBlock::start()
  55. *
  56. * @return void
  57. * @see ViewBlock::start()
  58. */
  59. public function end() {
  60. if (!empty($this->_active)) {
  61. $active = end($this->_active);
  62. $content = ob_get_clean();
  63. if (!isset($this->_blocks[$active])) {
  64. $this->_blocks[$active] = '';
  65. }
  66. $this->_blocks[$active] .= $content;
  67. array_pop($this->_active);
  68. }
  69. }
  70. /**
  71. * Append to an existing or new block. Appending to a new
  72. * block will create the block.
  73. *
  74. * Calling append() without a value will create a new capturing
  75. * block that needs to be finished with View::end(). The content
  76. * of the new capturing context will be added to the existing block context.
  77. *
  78. * @param string $name Name of the block
  79. * @param string $value The content for the block.
  80. * @return void
  81. * @throws CakeException when you use non-string values.
  82. */
  83. public function append($name, $value = null) {
  84. if (isset($value)) {
  85. if (!is_string($value)) {
  86. throw new CakeException(__d('cake_dev', '$value must be a string.'));
  87. }
  88. if (!isset($this->_blocks[$name])) {
  89. $this->_blocks[$name] = '';
  90. }
  91. $this->_blocks[$name] .= $value;
  92. } else {
  93. $this->start($name);
  94. }
  95. }
  96. /**
  97. * Set the content for a block. This will overwrite any
  98. * existing content.
  99. *
  100. * @param string $name Name of the block
  101. * @param string $value The content for the block.
  102. * @return void
  103. * @throws CakeException when you use non-string values.
  104. */
  105. public function set($name, $value) {
  106. if (!is_string($value)) {
  107. throw new CakeException(__d('cake_dev', 'Blocks can only contain strings.'));
  108. }
  109. $this->_blocks[$name] = $value;
  110. }
  111. /**
  112. * Get the content for a block.
  113. *
  114. * @param string $name Name of the block
  115. * @return The block content or '' if the block does not exist.
  116. */
  117. public function get($name) {
  118. if (!isset($this->_blocks[$name])) {
  119. return '';
  120. }
  121. return $this->_blocks[$name];
  122. }
  123. /**
  124. * Get the names of all the existing blocks.
  125. *
  126. * @return array An array containing the blocks.
  127. */
  128. public function keys() {
  129. return array_keys($this->_blocks);
  130. }
  131. /**
  132. * Get the name of the currently open block.
  133. *
  134. * @return mixed Either null or the name of the last open block.
  135. */
  136. public function active() {
  137. return end($this->_active);
  138. }
  139. }