Completed
Push — master ( 88763f...220402 )
by Jonathan
02:06
created

Template::render()   C

↳ Parent: Template

Complexity

Conditions 7
Paths 31

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 41
Code Lines 25

Code Coverage

Tests 27
CRAP Score 7.0022

Importance

Changes 0
Metric Value
dl 0
loc 41
ccs 27
cts 28
cp 0.9643
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 25
nc 31
nop 1
crap 7.0022
1
<?php
2
3
namespace League\Plates\Template;
4
5
use Exception;
6
use League\Plates\Engine;
7
use LogicException;
8
use Throwable;
9
10
/**
11
 * Container which holds template data and provides access to template functions.
12
 */
13
class Template
14
{
15
    /**
16
     * Instance of the template engine.
17
     * @var Engine
18
     */
19
    protected $engine;
20
21
    /**
22
     * The name of the template.
23
     * @var Name
24
     */
25
    protected $name;
26
27
    /**
28
     * The data assigned to the template.
29
     * @var array
30
     */
31
    protected $data = array();
32
33
    /**
34
     * An array of section content.
35
     * @var array
36
     */
37
    protected $sections = array();
38
39
    /**
40
     * The name of the template layout.
41
     * @var string
42
     */
43
    protected $layoutName;
44
45
    /**
46
     * The data assigned to the template layout.
47
     * @var array
48
     */
49
    protected $layoutData;
50
51
    /**
52
     * Create new Template instance.
53
     * @param Engine $engine
54
     * @param string $name
55
     */
56 108
    public function __construct(Engine $engine, $name)
57
    {
58 108
        $this->engine = $engine;
59 108
        $this->name = new Name($engine, $name);
60
61 108
        $this->data($this->engine->getData($name));
62 108
    }
63
64
    /**
65
     * Magic method used to call extension functions.
66
     * @param  string $name
67
     * @param  array  $arguments
68
     * @return mixed
69
     */
70 8
    public function __call($name, $arguments)
71
    {
72 8
        return $this->engine->getFunction($name)->call($this, $arguments);
73
    }
74
75
    /**
76
     * Alias for render() method.
77
     * @throws \Throwable
78
     * @throws \Exception
79
     * @return string
80
     */
81 4
    public function __toString()
82
    {
83 4
        return $this->render();
84
    }
85
86
    /**
87
     * Assign or get template data.
88
     * @param  array $data
89
     * @return mixed
90
     */
91 108
    public function data(array $data = null)
92
    {
93 108
        if (is_null($data)) {
94 4
            return $this->data;
95
        }
96
97 108
        $this->data = array_merge($this->data, $data);
98 108
    }
99
100
    /**
101
     * Check if the template exists.
102
     * @return boolean
103
     */
104 92
    public function exists()
105
    {
106 92
        return $this->name->doesPathExist();
107
    }
108
109
    /**
110
     * Get the template path.
111
     * @return string
112
     */
113 88
    public function path()
114
    {
115 88
        return $this->name->getPath();
116
    }
117
118
    /**
119
     * Render the template and layout.
120
     * @param  array  $data
121
     * @throws \Throwable
122
     * @throws \Exception
123
     * @return string
124
     */
125 84
    public function render(array $data = array())
126
    {
127 84
        $this->data($data);
128 84
        unset($data);
129 84
        extract($this->data);
130
131 84
        if (!$this->exists()) {
132 4
            throw new LogicException(
133 4
                'The template "' . $this->name->getName() . '" could not be found at "' . $this->path() . '".'
134 3
            );
135
        }
136
137
        try {
138 80
            $level = ob_get_level();
139 80
            ob_start();
140
141 80
            include $this->path();
142
143 64
            $content = ob_get_clean();
144
145 64
            if (isset($this->layoutName)) {
146 12
                $layout = $this->engine->make($this->layoutName);
147 12
                $layout->sections = array_merge($this->sections, array('content' => $content));
148 12
                $content = $layout->render($this->layoutData);
149 9
            }
150
151 64
            return $content;
152 16
        } catch (Throwable $e) {
0 ignored issues
show
Bug introduced by Jonathan Reinink
The class Throwable does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
153 4
            while (ob_get_level() > $level) {
154 4
                ob_end_clean();
155
            }
156
157 4
            throw $e;
158 12
        } catch (Exception $e) {
159 12
            while (ob_get_level() > $level) {
160 12
                ob_end_clean();
161 12
            }
162
163 12
            throw $e;
164
        }
165
    }
166
167
    /**
168
     * Set the template's layout.
169
     * @param  string $name
170
     * @param  array  $data
171
     * @return null
172
     */
173 12
    public function layout($name, array $data = array())
174
    {
175 12
        $this->layoutName = $name;
176 12
        $this->layoutData = $data;
177 12
    }
178
179
    /**
180
     * Start a new section block.
181
     * @param  string $name
182
     * @return null
183
     */
184 8
    public function start($name)
185
    {
186 8
        if ($name === 'content') {
187 4
            throw new LogicException(
188 1
                'The section name "content" is reserved.'
189 3
            );
190
        }
191
192 4
        $this->sections[$name] = '';
193
194 4
        ob_start();
195 4
    }
196
197
    /**
198
     * Stop the current section block.
199
     * @return null
200
     */
201 8
    public function stop()
202
    {
203 8
        if (empty($this->sections)) {
204 4
            throw new LogicException(
205 1
                'You must start a section before you can stop it.'
206 3
            );
207
        }
208
209 4
        end($this->sections);
210
211 4
        $this->sections[key($this->sections)] = ob_get_clean();
212 4
    }
213
214
    /**
215
     * Returns the content for a section block.
216
     * @param  string      $name    Section name
217
     * @param  string      $default Default section content
218
     * @return string|null
219
     */
220 12
    public function section($name, $default = null)
221
    {
222 12
        if (!isset($this->sections[$name])) {
223 8
            return $default;
224
        }
225
226 4
        return $this->sections[$name];
227
    }
228
229
    /**
230
     * Fetch a rendered template.
231
     * @param  string $name
232
     * @param  array  $data
233
     * @return string
234
     */
235 4
    public function fetch($name, array $data = array())
236
    {
237 4
        return $this->engine->render($name, $data);
238
    }
239
240
    /**
241
     * Output a rendered template.
242
     * @param  string $name
243
     * @param  array  $data
244
     * @return null
245
     */
246 4
    public function insert($name, array $data = array())
247
    {
248 4
        echo $this->engine->render($name, $data);
249 4
    }
250
251
    /**
252
     * Apply multiple functions to variable.
253
     * @param  mixed  $var
254
     * @param  string $functions
255
     * @return mixed
256
     */
257 12
    public function batch($var, $functions)
258
    {
259 12
        foreach (explode('|', $functions) as $function) {
260 12
            if ($this->engine->doesFunctionExist($function)) {
261 4
                $var = call_user_func(array($this, $function), $var);
262 12
            } elseif (is_callable($function)) {
263 8
                $var = call_user_func($function, $var);
264 6
            } else {
265 4
                throw new LogicException(
266 6
                    'The batch function could not find the "' . $function . '" function.'
267 3
                );
268
            }
269 6
        }
270
271 8
        return $var;
272
    }
273
274
    /**
275
     * Escape string.
276
     * @param  string      $string
277
     * @param  null|string $functions
278
     * @return string
279
     */
280 12
    public function escape($string, $functions = null)
281
    {
282 12
        static $flags;
283
284 12
        if (!isset($flags)) {
285 4
            $flags = ENT_QUOTES | (defined('ENT_SUBSTITUTE') ? ENT_SUBSTITUTE : 0);
286 3
        }
287
288 12
        if ($functions) {
289 4
            $string = $this->batch($string, $functions);
290 3
        }
291
292 12
        return htmlspecialchars($string, $flags, 'UTF-8');
293
    }
294
295
    /**
296
     * Alias to escape function.
297
     * @param  string      $string
298
     * @param  null|string $functions
299
     * @return string
300
     */
301 4
    public function e($string, $functions = null)
302
    {
303 4
        return $this->escape($string, $functions);
304
    }
305
}
306