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