SlotsHelper.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Templating\Helper;
  11. /**
  12. * SlotsHelper manages template slots.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. *
  16. * @api
  17. */
  18. class SlotsHelper extends Helper
  19. {
  20. protected $slots = array();
  21. protected $openSlots = array();
  22. /**
  23. * Starts a new slot.
  24. *
  25. * This method starts an output buffer that will be
  26. * closed when the stop() method is called.
  27. *
  28. * @param string $name The slot name
  29. *
  30. * @throws \InvalidArgumentException if a slot with the same name is already started
  31. *
  32. * @api
  33. */
  34. public function start($name)
  35. {
  36. if (in_array($name, $this->openSlots)) {
  37. throw new \InvalidArgumentException(sprintf('A slot named "%s" is already started.', $name));
  38. }
  39. $this->openSlots[] = $name;
  40. $this->slots[$name] = '';
  41. ob_start();
  42. ob_implicit_flush(0);
  43. }
  44. /**
  45. * Stops a slot.
  46. *
  47. * @throws \LogicException if no slot has been started
  48. *
  49. * @api
  50. */
  51. public function stop()
  52. {
  53. if (!$this->openSlots) {
  54. throw new \LogicException('No slot started.');
  55. }
  56. $name = array_pop($this->openSlots);
  57. $this->slots[$name] = ob_get_clean();
  58. }
  59. /**
  60. * Returns true if the slot exists.
  61. *
  62. * @param string $name The slot name
  63. *
  64. * @return Boolean
  65. *
  66. * @api
  67. */
  68. public function has($name)
  69. {
  70. return isset($this->slots[$name]);
  71. }
  72. /**
  73. * Gets the slot value.
  74. *
  75. * @param string $name The slot name
  76. * @param Boolean|string $default The default slot content
  77. *
  78. * @return string The slot content
  79. *
  80. * @api
  81. */
  82. public function get($name, $default = false)
  83. {
  84. return isset($this->slots[$name]) ? $this->slots[$name] : $default;
  85. }
  86. /**
  87. * Sets a slot value.
  88. *
  89. * @param string $name The slot name
  90. * @param string $content The slot content
  91. *
  92. * @api
  93. */
  94. public function set($name, $content)
  95. {
  96. $this->slots[$name] = $content;
  97. }
  98. /**
  99. * Outputs a slot.
  100. *
  101. * @param string $name The slot name
  102. * @param Boolean|string $default The default slot content
  103. *
  104. * @return Boolean true if the slot is defined or if a default content has been provided, false otherwise
  105. *
  106. * @api
  107. */
  108. public function output($name, $default = false)
  109. {
  110. if (!isset($this->slots[$name])) {
  111. if (false !== $default) {
  112. echo $default;
  113. return true;
  114. }
  115. return false;
  116. }
  117. echo $this->slots[$name];
  118. return true;
  119. }
  120. /**
  121. * Returns the canonical name of this helper.
  122. *
  123. * @return string The canonical name
  124. *
  125. * @api
  126. */
  127. public function getName()
  128. {
  129. return 'slots';
  130. }
  131. }