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