section.php 2.58 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<?php namespace Laravel;

class Section {

	/**
	 * All of the captured sections.
	 *
	 * @var array
	 */
	public static $sections = array();

	/**
	 * The last section on which injection was started.
	 *
Han Lin Yap committed
15
	 * @var array
16
	 */
17
	public static $last = array();
18 19 20 21 22 23 24 25

	/**
	 * Start injecting content into a section.
	 *
	 * <code>
	 *		// Start injecting into the "header" section
	 *		Section::start('header');
	 *
26
	 *		// Inject a raw string into the "header" section without buffering
27 28 29
	 *		Section::start('header', '<title>Laravel</title>');
	 * </code>
	 *
30 31
	 * @param  string          $section
	 * @param  string|Closure  $content
32 33 34 35
	 * @return void
	 */
	public static function start($section, $content = '')
	{
36 37 38 39 40 41
		if ($content === '')
		{
			ob_start() and static::$last[] = $section;
		}
		else
		{
42
			static::extend($section, $content);
43
		}
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
	}

	/**
	 * Inject inline content into a section.
	 *
	 * This is helpful for injecting simple strings such as page titles.
	 *
	 * <code>
	 *		// Inject inline content into the "header" section
	 *		Section::inject('header', '<title>Laravel</title>');
	 * </code>
	 *
	 * @param  string  $section
	 * @param  string  $content
	 * @return void
	 */
	public static function inject($section, $content)
	{
		static::start($section, $content);
	}

	/**
66 67 68 69 70 71 72 73 74 75
	 * Stop injecting content into a section and return its contents.
	 *
	 * @return string
	 */
	public static function yield_section()
	{
		return static::yield(static::stop());
	}

	/**
76 77
	 * Stop injecting content into a section.
	 *
78
	 * @return string
79 80 81
	 */
	public static function stop()
	{
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 91 92 93
	 *
	 * @param  string  $section
	 * @param  string  $content
	 * @return void
	 */
94
	protected static function extend($section, $content)
95 96 97
	{
		if (isset(static::$sections[$section]))
		{
98 99 100 101 102
			static::$sections[$section] = str_replace('@parent', $content, static::$sections[$section]);
		}
		else
		{
			static::$sections[$section] = $content;
103 104 105 106
		}
	}

	/**
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
	 * Append content to a given section.
	 *
	 * @param  string  $section
	 * @param  string  $content
	 * @return void
	 */
	public static function append($section, $content)
	{
		if (isset(static::$sections[$section]))
		{
			static::$sections[$section] .= $content;
		}
		else
		{
			static::$sections[$section] = $content;
		}
	}

	/**
126 127 128 129 130 131 132 133 134 135 136
	 * Get the string contents of a section.
	 *
	 * @param  string  $section
	 * @return string
	 */
	public static function yield($section)
	{
		return (isset(static::$sections[$section])) ? static::$sections[$section] : '';
	}

}