1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
<?php /** * Whoops - php errors for cool kids * @author Filipe Dobreira <http://github.com/filp> */
namespace Whoops\Util;
use Symfony\Component\VarDumper\Cloner\VarCloner; use Symfony\Component\VarDumper\Dumper\CliDumper; use Symfony\Component\VarDumper\Dumper\HtmlDumper;
/** * Exposes useful tools for working with/in templates */ class TemplateHelper { /** * An array of variables to be passed to all templates * @var array */ private $variables = array();
/** * Escapes a string for output in an HTML document * * @param string $raw * @return string */ public function escape($raw) { $flags = ENT_QUOTES;
// HHVM has all constants defined, but only ENT_IGNORE // works at the moment if (defined("ENT_SUBSTITUTE") && !defined("HHVM_VERSION")) { $flags |= ENT_SUBSTITUTE; } else { // This is for 5.3. // The documentation warns of a potential security issue, // but it seems it does not apply in our case, because // we do not blacklist anything anywhere. $flags |= ENT_IGNORE; }
return htmlspecialchars($raw, $flags, "UTF-8"); }
/** * Escapes a string for output in an HTML document, but preserves * URIs within it, and converts them to clickable anchor elements. * * @param string $raw * @return string */ public function escapeButPreserveUris($raw) { $escaped = $this->escape($raw); return preg_replace( "@([A-z]+?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@", "<a href=\"$1\" target=\"_blank\">$1</a>", $escaped ); }
/** * Format the given value into a human readable string. * * @param mixed $value * @return string */ public function dump($value) { if (class_exists('Symfony\Component\VarDumper\Cloner\VarCloner')) { static $dumper = null;
// re-use the same var-dumper instance, so it won't re-render the global styles/scripts on each dump. if (!$dumper) { $dumper = new HtmlDumper();
$styles = array( 'default' => '', 'num' => '', 'const' => '', 'str' => '', 'note' => '', 'ref' => '', 'public' => '', 'protected' => '', 'private' => '', 'meta' => '', 'key' => '', 'index' => '', ); $dumper->setStyles($styles); }
$cloner = new VarCloner(); return $dumper->dump($cloner->cloneVar($value)); } return print_r($value, true); }
/** * Convert a string to a slug version of itself * * @param string $original * @return string */ public function slug($original) { $slug = str_replace(" ", "-", $original); $slug = preg_replace('/[^\w\d\-\_]/i', '', $slug); return strtolower($slug); }
/** * Given a template path, render it within its own scope. This * method also accepts an array of additional variables to be * passed to the template. * * @param string $template * @param array $additionalVariables */ public function render($template, array $additionalVariables = null) { $variables = $this->getVariables();
// Pass the helper to the template: $variables["tpl"] = $this;
if ($additionalVariables !== null) { $variables = array_replace($variables, $additionalVariables); }
call_user_func(function () { extract(func_get_arg(1)); require func_get_arg(0); }, $template, $variables); }
/** * Sets the variables to be passed to all templates rendered * by this template helper. * * @param array $variables */ public function setVariables(array $variables) { $this->variables = $variables; }
/** * Sets a single template variable, by its name: * * @param string $variableName * @param mixd $variableValue */ public function setVariable($variableName, $variableValue) { $this->variables[$variableName] = $variableValue; }
/** * Gets a single template variable, by its name, or * $defaultValue if the variable does not exist * * @param string $variableName * @param mixed $defaultValue * @return mixed */ public function getVariable($variableName, $defaultValue = null) { return isset($this->variables[$variableName]) ? $this->variables[$variableName] : $defaultValue; }
/** * Unsets a single template variable, by its name * * @param string $variableName */ public function delVariable($variableName) { unset($this->variables[$variableName]); }
/** * Returns all variables for this helper * * @return array */ public function getVariables() { return $this->variables; } }
|