1 | <?php |
||
16 | class TemplateHelper |
||
17 | { |
||
18 | /** |
||
19 | * An array of variables to be passed to all templates |
||
20 | * @var array |
||
21 | */ |
||
22 | private $variables = array(); |
||
23 | |||
24 | /** |
||
25 | * Escapes a string for output in an HTML document |
||
26 | * |
||
27 | * @param string $raw |
||
28 | * @return string |
||
29 | */ |
||
30 | 2 | public function escape($raw) |
|
31 | { |
||
32 | 2 | $flags = ENT_QUOTES; |
|
33 | |||
34 | // HHVM has all constants defined, but only ENT_IGNORE |
||
35 | // works at the moment |
||
36 | 2 | if (defined("ENT_SUBSTITUTE") && !defined("HHVM_VERSION")) { |
|
37 | 2 | $flags |= ENT_SUBSTITUTE; |
|
38 | 2 | } else { |
|
39 | // This is for 5.3. |
||
40 | // The documentation warns of a potential security issue, |
||
41 | // but it seems it does not apply in our case, because |
||
42 | // we do not blacklist anything anywhere. |
||
43 | $flags |= ENT_IGNORE; |
||
44 | } |
||
45 | |||
46 | 2 | return htmlspecialchars($raw, $flags, "UTF-8"); |
|
47 | } |
||
48 | |||
49 | /** |
||
50 | * Escapes a string for output in an HTML document, but preserves |
||
51 | * URIs within it, and converts them to clickable anchor elements. |
||
52 | * |
||
53 | * @param string $raw |
||
54 | * @return string |
||
55 | */ |
||
56 | 1 | public function escapeButPreserveUris($raw) |
|
64 | |||
65 | /** |
||
66 | * Format the given value into a human readable string. |
||
67 | * |
||
68 | * @param mixed $value |
||
69 | * @return string |
||
70 | */ |
||
71 | public function dump($value) |
||
102 | |||
103 | /** |
||
104 | * Convert a string to a slug version of itself |
||
105 | * |
||
106 | * @param string $original |
||
107 | * @return string |
||
108 | */ |
||
109 | 1 | public function slug($original) |
|
115 | |||
116 | /** |
||
117 | * Given a template path, render it within its own scope. This |
||
118 | * method also accepts an array of additional variables to be |
||
119 | * passed to the template. |
||
120 | * |
||
121 | * @param string $template |
||
122 | * @param array $additionalVariables |
||
123 | */ |
||
124 | 1 | public function render($template, array $additionalVariables = null) |
|
140 | |||
141 | /** |
||
142 | * Sets the variables to be passed to all templates rendered |
||
143 | * by this template helper. |
||
144 | * |
||
145 | * @param array $variables |
||
146 | */ |
||
147 | 1 | public function setVariables(array $variables) |
|
151 | |||
152 | /** |
||
153 | * Sets a single template variable, by its name: |
||
154 | * |
||
155 | * @param string $variableName |
||
156 | * @param mixd $variableValue |
||
157 | */ |
||
158 | 1 | public function setVariable($variableName, $variableValue) |
|
162 | |||
163 | /** |
||
164 | * Gets a single template variable, by its name, or |
||
165 | * $defaultValue if the variable does not exist |
||
166 | * |
||
167 | * @param string $variableName |
||
168 | * @param mixed $defaultValue |
||
169 | * @return mixed |
||
170 | */ |
||
171 | 1 | public function getVariable($variableName, $defaultValue = null) |
|
176 | |||
177 | /** |
||
178 | * Unsets a single template variable, by its name |
||
179 | * |
||
180 | * @param string $variableName |
||
181 | */ |
||
182 | 1 | public function delVariable($variableName) |
|
186 | |||
187 | /** |
||
188 | * Returns all variables for this helper |
||
189 | * |
||
190 | * @return array |
||
191 | */ |
||
192 | 1 | public function getVariables() |
|
196 | } |
||
197 |