PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 1.18
Independent Analytics – WordPress Analytics Plugin v1.18
2.15.5 2.15.4 2.15.3 2.15.2 2.15.1 2.15.0 2.14.10 trunk 1.1 1.10 1.10.1 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.17.1 1.17.2 1.17.3 1.17.4 1.18 1.18.1 1.19.0 1.19.1 1.2 1.20.0 1.21.0 1.22.0 1.22.1 1.23.0 1.23.1 1.24.0 1.24.1 1.25.0 1.25.1 1.26.0 1.27.0 1.28.0 1.28.1 1.28.2 1.28.3 1.29.0 1.3 1.30.0 1.30.1 1.4 1.5 1.6 1.7 1.8 1.9 2.0.0 2.0.1 2.1.4 2.1.5 2.1.6 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.10 2.11.2 2.11.3 2.11.4 2.11.5 2.11.6 2.11.7 2.11.8 2.11.9 2.12.0 2.12.1 2.12.2 2.13.1 2.13.2 2.13.5 2.13.6 2.14.0 2.14.1 2.14.2 2.14.4 2.14.6 2.14.7 2.14.8 2.14.9 2.2.0 2.2.1 2.3.1 2.3.2 2.4.2 2.4.3 2.5.0 2.5.1 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.7.0 2.7.1 2.7.2 2.7.3 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.8.9 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7
independent-analytics / vendor / league / plates / src / Template / Template.php
independent-analytics / vendor / league / plates / src / Template Last commit date
Data.php 3 years ago Directory.php 3 years ago FileExtension.php 3 years ago Folder.php 3 years ago Folders.php 3 years ago Func.php 3 years ago Functions.php 3 years ago Name.php 3 years ago Template.php 3 years ago
Template.php
327 lines
1 <?php
2
3 namespace IAWP\League\Plates\Template;
4
5 use Exception;
6 use IAWP\League\Plates\Engine;
7 use LogicException;
8 use Throwable;
9 /**
10 * Container which holds template data and provides access to template functions.
11 */
12 class Template
13 {
14 const SECTION_MODE_REWRITE = 1;
15 const SECTION_MODE_PREPEND = 2;
16 const SECTION_MODE_APPEND = 3;
17 /**
18 * Set section content mode: rewrite/append/prepend
19 * @var int
20 */
21 protected $sectionMode = self::SECTION_MODE_REWRITE;
22 /**
23 * Instance of the template engine.
24 * @var Engine
25 */
26 protected $engine;
27 /**
28 * The name of the template.
29 * @var Name
30 */
31 protected $name;
32 /**
33 * The data assigned to the template.
34 * @var array
35 */
36 protected $data = array();
37 /**
38 * An array of section content.
39 * @var array
40 */
41 protected $sections = array();
42 /**
43 * The name of the section currently being rendered.
44 * @var string
45 */
46 protected $sectionName;
47 /**
48 * Whether the section should be appended or not.
49 * @deprecated stayed for backward compatibility, use $sectionMode instead
50 * @var boolean
51 */
52 protected $appendSection;
53 /**
54 * The name of the template layout.
55 * @var string
56 */
57 protected $layoutName;
58 /**
59 * The data assigned to the template layout.
60 * @var array
61 */
62 protected $layoutData;
63 /**
64 * Create new Template instance.
65 * @param Engine $engine
66 * @param string $name
67 */
68 public function __construct(Engine $engine, $name)
69 {
70 $this->engine = $engine;
71 $this->name = new Name($engine, $name);
72 $this->data($this->engine->getData($name));
73 }
74 /**
75 * Magic method used to call extension functions.
76 * @param string $name
77 * @param array $arguments
78 * @return mixed
79 */
80 public function __call($name, $arguments)
81 {
82 return $this->engine->getFunction($name)->call($this, $arguments);
83 }
84 /**
85 * Alias for render() method.
86 * @throws \Throwable
87 * @throws \Exception
88 * @return string
89 */
90 public function __toString()
91 {
92 return $this->render();
93 }
94 /**
95 * Assign or get template data.
96 * @param array $data
97 * @return mixed
98 */
99 public function data(array $data = null)
100 {
101 if (\is_null($data)) {
102 return $this->data;
103 }
104 $this->data = \array_merge($this->data, $data);
105 }
106 /**
107 * Check if the template exists.
108 * @return boolean
109 */
110 public function exists()
111 {
112 return $this->name->doesPathExist();
113 }
114 /**
115 * Get the template path.
116 * @return string
117 */
118 public function path()
119 {
120 return $this->name->getPath();
121 }
122 /**
123 * Render the template and layout.
124 * @param array $data
125 * @throws \Throwable
126 * @throws \Exception
127 * @return string
128 */
129 public function render(array $data = array())
130 {
131 $this->data($data);
132 unset($data);
133 \extract($this->data);
134 if (!$this->exists()) {
135 throw new LogicException('The template "' . $this->name->getName() . '" could not be found at "' . $this->path() . '".');
136 }
137 try {
138 $level = \ob_get_level();
139 \ob_start();
140 include $this->path();
141 $content = \ob_get_clean();
142 if (isset($this->layoutName)) {
143 $layout = $this->engine->make($this->layoutName);
144 $layout->sections = \array_merge($this->sections, array('content' => $content));
145 $content = $layout->render($this->layoutData);
146 }
147 return $content;
148 } catch (Throwable $e) {
149 while (\ob_get_level() > $level) {
150 \ob_end_clean();
151 }
152 throw $e;
153 } catch (Exception $e) {
154 while (\ob_get_level() > $level) {
155 \ob_end_clean();
156 }
157 throw $e;
158 }
159 }
160 /**
161 * Set the template's layout.
162 * @param string $name
163 * @param array $data
164 * @return null
165 */
166 public function layout($name, array $data = array())
167 {
168 $this->layoutName = $name;
169 $this->layoutData = $data;
170 }
171 /**
172 * Start a new section block.
173 * @param string $name
174 * @return null
175 */
176 public function start($name)
177 {
178 if ($name === 'content') {
179 throw new LogicException('The section name "content" is reserved.');
180 }
181 if ($this->sectionName) {
182 throw new LogicException('You cannot nest sections within other sections.');
183 }
184 $this->sectionName = $name;
185 \ob_start();
186 }
187 /**
188 * Start a new section block in APPEND mode.
189 * @param string $name
190 * @return null
191 */
192 public function push($name)
193 {
194 $this->appendSection = \true;
195 /* for backward compatibility */
196 $this->sectionMode = self::SECTION_MODE_APPEND;
197 $this->start($name);
198 }
199 /**
200 * Start a new section block in PREPEND mode.
201 * @param string $name
202 * @return null
203 */
204 public function unshift($name)
205 {
206 $this->appendSection = \false;
207 /* for backward compatibility */
208 $this->sectionMode = self::SECTION_MODE_PREPEND;
209 $this->start($name);
210 }
211 /**
212 * Stop the current section block.
213 * @return null
214 */
215 public function stop()
216 {
217 if (\is_null($this->sectionName)) {
218 throw new LogicException('You must start a section before you can stop it.');
219 }
220 if (!isset($this->sections[$this->sectionName])) {
221 $this->sections[$this->sectionName] = '';
222 }
223 switch ($this->sectionMode) {
224 case self::SECTION_MODE_REWRITE:
225 $this->sections[$this->sectionName] = \ob_get_clean();
226 break;
227 case self::SECTION_MODE_APPEND:
228 $this->sections[$this->sectionName] .= \ob_get_clean();
229 break;
230 case self::SECTION_MODE_PREPEND:
231 $this->sections[$this->sectionName] = \ob_get_clean() . $this->sections[$this->sectionName];
232 break;
233 }
234 $this->sectionName = null;
235 $this->sectionMode = self::SECTION_MODE_REWRITE;
236 $this->appendSection = \false;
237 /* for backward compatibility */
238 }
239 /**
240 * Alias of stop().
241 * @return null
242 */
243 public function end()
244 {
245 $this->stop();
246 }
247 /**
248 * Returns the content for a section block.
249 * @param string $name Section name
250 * @param string $default Default section content
251 * @return string|null
252 */
253 public function section($name, $default = null)
254 {
255 if (!isset($this->sections[$name])) {
256 return $default;
257 }
258 return $this->sections[$name];
259 }
260 /**
261 * Fetch a rendered template.
262 * @param string $name
263 * @param array $data
264 * @return string
265 */
266 public function fetch($name, array $data = array())
267 {
268 return $this->engine->render($name, $data);
269 }
270 /**
271 * Output a rendered template.
272 * @param string $name
273 * @param array $data
274 * @return null
275 */
276 public function insert($name, array $data = array())
277 {
278 echo $this->engine->render($name, $data);
279 }
280 /**
281 * Apply multiple functions to variable.
282 * @param mixed $var
283 * @param string $functions
284 * @return mixed
285 */
286 public function batch($var, $functions)
287 {
288 foreach (\explode('|', $functions) as $function) {
289 if ($this->engine->doesFunctionExist($function)) {
290 $var = \call_user_func(array($this, $function), $var);
291 } elseif (\is_callable($function)) {
292 $var = \call_user_func($function, $var);
293 } else {
294 throw new LogicException('The batch function could not find the "' . $function . '" function.');
295 }
296 }
297 return $var;
298 }
299 /**
300 * Escape string.
301 * @param string $string
302 * @param null|string $functions
303 * @return string
304 */
305 public function escape($string, $functions = null)
306 {
307 static $flags;
308 if (!isset($flags)) {
309 $flags = \ENT_QUOTES | (\defined('ENT_SUBSTITUTE') ? \ENT_SUBSTITUTE : 0);
310 }
311 if ($functions) {
312 $string = $this->batch($string, $functions);
313 }
314 return \htmlspecialchars($string, $flags, 'UTF-8');
315 }
316 /**
317 * Alias to escape function.
318 * @param string $string
319 * @param null|string $functions
320 * @return string
321 */
322 public function e($string, $functions = null)
323 {
324 return $this->escape($string, $functions);
325 }
326 }
327