1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Nip\View\Template; |
6
|
|
|
|
7
|
|
|
use Nip\View\View; |
8
|
|
|
use function array_key_exists; |
9
|
|
|
use function is_array; |
10
|
|
|
use function is_string; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class Template. |
14
|
|
|
* |
15
|
|
|
* @property View $engine |
16
|
|
|
*/ |
17
|
|
|
class Template extends \League\Plates\Template\Template |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* {@inheritDoc} |
21
|
|
|
*/ |
22
|
|
|
public function render($data = []): ?string |
23
|
|
|
{ |
24
|
|
|
if (!is_string($data)) { |
25
|
|
|
return parent::render($data); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
if (isset($this->sections[$data])) { |
29
|
|
|
return $this->section($data); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
return $this->fetch($data); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Determine if a piece of data is bound. |
37
|
|
|
* |
38
|
|
|
* @param string $key |
39
|
|
|
* |
40
|
|
|
* @return bool |
41
|
|
|
*/ |
42
|
|
|
public function offsetExists($key) |
43
|
|
|
{ |
44
|
|
|
return array_key_exists($key, $this->data); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Get a piece of bound data to the view. |
49
|
|
|
* |
50
|
|
|
* @param string $key |
51
|
|
|
* |
52
|
|
|
* @return mixed |
53
|
|
|
*/ |
54
|
|
|
public function offsetGet($key) |
55
|
|
|
{ |
56
|
|
|
return $this->data[$key]; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Set a piece of data on the view. |
61
|
|
|
* |
62
|
|
|
* @param string $key |
63
|
|
|
* @param mixed $value |
64
|
|
|
* |
65
|
|
|
* @return void |
66
|
|
|
*/ |
67
|
|
|
public function offsetSet($key, $value) |
68
|
|
|
{ |
69
|
|
|
$this->with($key, $value); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Unset a piece of data from the view. |
74
|
|
|
* |
75
|
|
|
* @param string $key |
76
|
|
|
* |
77
|
|
|
* @return void |
78
|
|
|
*/ |
79
|
|
|
public function offsetUnset($key) |
80
|
|
|
{ |
81
|
|
|
unset($this->data[$key]); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return mixed|null |
86
|
|
|
*/ |
87
|
|
|
public function &__get($key) |
88
|
|
|
{ |
89
|
|
|
$var = $this->get($key); |
90
|
|
|
|
91
|
|
|
return $var; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return $this |
96
|
|
|
*/ |
97
|
|
|
public function __set($name, $value) |
98
|
|
|
{ |
99
|
|
|
return $this->set($name, $value); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string $name |
104
|
|
|
* @param null $default |
|
|
|
|
105
|
|
|
* |
106
|
|
|
* @return mixed|null |
107
|
|
|
*/ |
108
|
|
|
public function get($name, $default = null) |
109
|
|
|
{ |
110
|
|
|
if (isset($this->data[$name])) { |
111
|
|
|
return $this->data[$name]; |
112
|
|
|
} else { |
113
|
|
|
return $default; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return bool |
119
|
|
|
*/ |
120
|
|
|
public function has(string $name) |
121
|
|
|
{ |
122
|
|
|
return isset($this->data[$name]); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param null $value |
|
|
|
|
127
|
|
|
* |
128
|
|
|
* @return $this |
129
|
|
|
*/ |
130
|
|
|
public function with($key, $value = null) |
131
|
|
|
{ |
132
|
|
|
$data = (is_array($key)) ? $key : [$key => $value]; |
133
|
|
|
$this->data = array_merge($this->data, $data); |
134
|
|
|
|
135
|
|
|
return $this; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param string|array $key |
140
|
|
|
* @param mixed $value |
141
|
|
|
* |
142
|
|
|
* @return $this |
143
|
|
|
*/ |
144
|
|
|
public function set($key, $value) |
145
|
|
|
{ |
146
|
|
|
$this->data[$key] = $value; |
147
|
|
|
|
148
|
|
|
return $this; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @return bool |
153
|
|
|
*/ |
154
|
|
|
public function __isset($name) |
155
|
|
|
{ |
156
|
|
|
return isset($this->data[$name]); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function __unset($name) |
160
|
|
|
{ |
161
|
|
|
unset($this->data[$name]); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param string $name |
166
|
|
|
* @param string $appended |
167
|
|
|
* |
168
|
|
|
* @return $this |
169
|
|
|
*/ |
170
|
|
|
public function append($name, $appended) |
171
|
|
|
{ |
172
|
|
|
$value = $this->has($name) ? $this->get($name) : ''; |
173
|
|
|
$value .= $appended; |
174
|
|
|
|
175
|
|
|
return $this->set($name, $value); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Assigns variables in bulk in the current scope. |
180
|
|
|
* |
181
|
|
|
* @param array $array |
182
|
|
|
* |
183
|
|
|
* @return $this |
184
|
|
|
*/ |
185
|
|
|
public function assign($array = []) |
186
|
|
|
{ |
187
|
|
|
foreach ($array as $key => $value) { |
188
|
|
|
if (is_string($key)) { |
189
|
|
|
$this->set($key, $value); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
return $this; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @return View |
198
|
|
|
*/ |
199
|
|
|
public function getEngine(): View |
200
|
|
|
{ |
201
|
|
|
return $this->engine; |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|