ViewBlock.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 block being captured.
  33. *
  34. * @var string
  35. */
  36. protected $_active = null;
  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. $content = ob_get_clean();
  62. if (!isset($this->_blocks[$this->_active])) {
  63. $this->_blocks[$this->_active] = '';
  64. }
  65. $this->_blocks[$this->_active] .= $content;
  66. }
  67. $this->_active = null;
  68. }
  69. /**
  70. * Append to an existing or new block. Appending to a new
  71. * block will create the block.
  72. *
  73. * Calling append() without a value will create a new capturing
  74. * block that needs to be finished with View::end(). The content
  75. * of the new capturing context will be added to the existing block context.
  76. *
  77. * @param string $name Name of the block
  78. * @param string $value The content for the block.
  79. * @return void
  80. * @throws CakeException when you use non-string values.
  81. */
  82. public function append($name, $value = null) {
  83. if (isset($value)) {
  84. if (!is_string($value)) {
  85. throw new CakeException(__d('cake_dev', '$value must be a string.'));
  86. }
  87. if (!isset($this->_blocks[$name])) {
  88. $this->_blocks[$name] = '';
  89. }
  90. $this->_blocks[$name] .= $value;
  91. } else {
  92. $this->start($name);
  93. }
  94. }
  95. /**
  96. * Set the content for a block. This will overwrite any
  97. * existing content.
  98. *
  99. * @param string $name Name of the block
  100. * @param string $value The content for the block.
  101. * @return void
  102. * @throws CakeException when you use non-string values.
  103. */
  104. public function set($name, $value) {
  105. if (!is_string($value)) {
  106. throw new CakeException(__d('cake_dev', 'Blocks can only contain strings.'));
  107. }
  108. $this->_blocks[$name] = $value;
  109. }
  110. /**
  111. * Get the content for a block.
  112. *
  113. * @param string $name Name of the block
  114. * @return The block content or '' if the block does not exist.
  115. */
  116. public function get($name) {
  117. if (!isset($this->_blocks[$name])) {
  118. return '';
  119. }
  120. return $this->_blocks[$name];
  121. }
  122. /**
  123. * Get the names of all the existing blocks.
  124. *
  125. * @return array An array containing the blocks.
  126. */
  127. public function keys() {
  128. return array_keys($this->_blocks);
  129. }
  130. /**
  131. * Get the name of the currently open block.
  132. *
  133. * @return mixed Either null or the name of the open block.
  134. */
  135. public function active() {
  136. return $this->_active;
  137. }
  138. }