Newer
Older
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
<?php
/**
* Abstract template class representing the presentation layer of an action.
* Output can be customized by supplying attributes, which a template can
* manipulate and display.
*
* @copyright 2008 Marcus Lunzenauer <mlunzena@uos.de>
* @author Marcus Lunzenauer <mlunzena@uos.de>
* @license MIT
*/
namespace Flexi;
abstract class Template
{
/**
* Parse, render and return the presentation.
*
* @return string A string representing the rendered presentation.
*/
abstract public function _render(): string;
protected array $attributes = [];
protected Template|null $layout = null;
/**
* Constructor
*
* @param string $template the path of the template.
* @param Factory $factory the factory creating this template
* @param array $options optional array of options
*/
public function __construct(
protected string $template,
protected Factory $factory,
protected array $options = []
) {
}
/**
* __set() is a magic method run when writing data to inaccessible members.
* In this class it is used to set attributes for the template in a
* comfortable way.
*
* @param string $name the name of the member field
* @param mixed $value the value for the member field
*
* @see http://php.net/__set
*/
public function __set(string $name, mixed $value): void
{
$this->set_attribute($name, $value);
}
/**
* __get() is a magic method utilized for reading data from inaccessible
* members.
* In this class it is used to get attributes for the template in a
* comfortable way.
*
* @param string $name the name of the member field
*
* @return mixed the value for the member field
* @see http://php.net/__get
*/
public function __get(string $name): mixed
{
return $this->get_attribute($name);
}
/**
* __isset() is a magic method triggered by calling isset() or empty() on
* inaccessible members.
* In this class it is used to check for attributes for the template in a
* comfortable way.
*
* @param string $name the name of the member field
*
* @return bool TRUE if that attribute exists, FALSE otherwise
* @see http://php.net/__isset
*/
public function __isset(string $name): bool
{
return isset($this->attributes[$name]);
}
/**
* __unset() is a magic method invoked when unset() is used on inaccessible
* members.
* In this class it is used to check for attributes for the template in a
* comfortable way.
*
* @param string $name the name of the member field
*
* @see http://php.net/__set
*/
public function __unset(string $name): void
{
$this->clear_attribute($name);
}
/**
* Parse, render and return the presentation.
*
* @param array $attributes An optional associative array of attributes and
* their associated values.
* @param string|Template|null $layout A name of a layout template.
*
* @return string A string representing the rendered presentation.
* @throws TemplateNotFoundException
*/
public function render(array $attributes = [], string|Template $layout = null): string
{
if (isset($layout)) {
$this->set_layout($layout);
}
# merge attributes
$this->set_attributes($attributes);
return $this->_render();
}
/**
* Returns the value of an attribute.
*
* @param string $name An attribute name.
* @return mixed An attribute value.
*/
public function get_attribute(string $name)
{
return $this->attributes[$name] ?? null;
}
/**
* Set an array of attributes.
*
* @return array An associative array of attributes and their associated
* values.
*/
public function get_attributes(): array
{
return $this->attributes;
}
/**
* Set an attribute.
*
* @param string $name An attribute name.
* @param mixed $value An attribute value.
*/
public function set_attribute(string $name, mixed $value): void
{
$this->attributes[$name] = $value;
}
/**
* Set an array of attributes.
*
* @param array $attributes An associative array of attributes and their
* associated values.
*/
public function set_attributes(array $attributes): void
{
$this->attributes = $attributes + $this->attributes;
}
/**
* Clear all attributes associated with this template.
*/
public function clear_attributes(): void
{
$this->attributes = [];
}
/**
* Clear an attribute associated with this template.
*
* @param string $name The name of the attribute to be cleared.
*/
public function clear_attribute(string $name): void
{
unset($this->attributes[$name]);
}
/**
* Set the template's layout.
*
* @param Template|string|null $layout A name of a layout template or a
* layout template.
* @throws TemplateNotFoundException
*/
public function set_layout(Template|string|null $layout): void
{
$this->layout = $layout ? $this->factory->open($layout) : null;