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