section.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php namespace Laravel;
  2. class Section {
  3. /**
  4. * All of the captured sections.
  5. *
  6. * @var array
  7. */
  8. public static $sections = array();
  9. /**
  10. * The last section on which injection was started.
  11. *
  12. * @var array
  13. */
  14. public static $last = array();
  15. /**
  16. * Start injecting content into a section.
  17. *
  18. * <code>
  19. * // Start injecting into the "header" section
  20. * Section::start('header');
  21. *
  22. * // Inject a raw string into the "header" section without buffering
  23. * Section::start('header', '<title>Laravel</title>');
  24. * </code>
  25. *
  26. * @param string $section
  27. * @param string|Closure $content
  28. * @return void
  29. */
  30. public static function start($section, $content = '')
  31. {
  32. if ($content === '')
  33. {
  34. ob_start() and static::$last[] = $section;
  35. }
  36. else
  37. {
  38. static::extend($section, $content);
  39. }
  40. }
  41. /**
  42. * Inject inline content into a section.
  43. *
  44. * This is helpful for injecting simple strings such as page titles.
  45. *
  46. * <code>
  47. * // Inject inline content into the "header" section
  48. * Section::inject('header', '<title>Laravel</title>');
  49. * </code>
  50. *
  51. * @param string $section
  52. * @param string $content
  53. * @return void
  54. */
  55. public static function inject($section, $content)
  56. {
  57. static::start($section, $content);
  58. }
  59. /**
  60. * Stop injecting content into a section and return its contents.
  61. *
  62. * @return string
  63. */
  64. public static function yield_section()
  65. {
  66. return static::yield(static::stop());
  67. }
  68. /**
  69. * Stop injecting content into a section.
  70. *
  71. * @return string
  72. */
  73. public static function stop()
  74. {
  75. static::extend($last = array_pop(static::$last), ob_get_clean());
  76. return $last;
  77. }
  78. /**
  79. * Extend the content in a given section.
  80. *
  81. * @param string $section
  82. * @param string $content
  83. * @return void
  84. */
  85. protected static function extend($section, $content)
  86. {
  87. if (isset(static::$sections[$section]))
  88. {
  89. static::$sections[$section] = str_replace('@parent', $content, static::$sections[$section]);
  90. }
  91. else
  92. {
  93. static::$sections[$section] = $content;
  94. }
  95. }
  96. /**
  97. * Append content to a given section.
  98. *
  99. * @param string $section
  100. * @param string $content
  101. * @return void
  102. */
  103. public static function append($section, $content)
  104. {
  105. if (isset(static::$sections[$section]))
  106. {
  107. static::$sections[$section] .= $content;
  108. }
  109. else
  110. {
  111. static::$sections[$section] = $content;
  112. }
  113. }
  114. /**
  115. * Get the string contents of a section.
  116. *
  117. * @param string $section
  118. * @return string
  119. */
  120. public static function yield($section)
  121. {
  122. return (isset(static::$sections[$section])) ? static::$sections[$section] : '';
  123. }
  124. }