1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Template trait |
4
|
|
|
* A consistent way to render a template for all theme/view classes |
5
|
|
|
* |
6
|
|
|
* By including this trait in your class, your object can act like a view (like magic) |
7
|
|
|
* |
8
|
|
|
* @package erdiko/core |
9
|
|
|
* @copyright 2012-2017 Arroyo Labs, Inc. http://www.arroyolabs.com |
10
|
|
|
* @author John Arroyo <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
namespace erdiko\core; |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
trait Template |
16
|
|
|
{ |
17
|
|
|
/** Template */ |
18
|
|
|
protected $_template = null; |
19
|
|
|
/** Data */ |
20
|
|
|
protected $_data = null; |
21
|
|
|
/** Default Template */ |
22
|
|
|
protected $_defaultTemplate = 'default'; |
23
|
|
|
/** Default Template Root Folder */ |
24
|
|
|
protected $_templateRootFolder = null; |
25
|
|
|
/** Template Folder */ |
26
|
|
|
protected $_templateFolder = null; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Constructor like initiation |
30
|
|
|
* @param string $template , Theme Object (Contaier) |
31
|
|
|
* @param mixed $data |
32
|
|
|
*/ |
33
|
|
|
public function initiate($template = null, $data = null, $templateRootFolder = ERDIKO_APP) |
34
|
|
|
{ |
35
|
|
|
$template = ($template === null) ? $this->getDefaultTemplate() : $template; |
36
|
|
|
$this->setTemplate($template); |
37
|
|
|
$this->setData($data); |
38
|
|
|
$this->setTemplateRootFolder($templateRootFolder); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Set template |
43
|
|
|
* |
44
|
|
|
* @param string $template |
45
|
|
|
*/ |
46
|
|
|
public function setTemplate($template) |
47
|
|
|
{ |
48
|
|
|
$this->_template = $template; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Get template |
53
|
|
|
* |
54
|
|
|
* @param string $template |
|
|
|
|
55
|
|
|
*/ |
56
|
|
|
public function getTemplate() |
57
|
|
|
{ |
58
|
|
|
return $this->_template; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get default template name |
63
|
|
|
* |
64
|
|
|
* @param string $template |
|
|
|
|
65
|
|
|
*/ |
66
|
|
|
public function getDefaultTemplate() |
67
|
|
|
{ |
68
|
|
|
return $this->_defaultTemplate; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Set default template name |
73
|
|
|
* |
74
|
|
|
* @param string $template |
75
|
|
|
*/ |
76
|
|
|
public function setDefaultTemplate($template) |
77
|
|
|
{ |
78
|
|
|
$this->_defaultTemplate = $template; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Set template root folder |
83
|
|
|
* |
84
|
|
|
* @param string $TemplateRootFolder |
|
|
|
|
85
|
|
|
*/ |
86
|
|
|
public function setTemplateRootFolder($templateRootFolder) |
87
|
|
|
{ |
88
|
|
|
$this->_templateRootFolder = $templateRootFolder; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Set data |
93
|
|
|
* |
94
|
|
|
* @param mixed $data , data injected into the container |
95
|
|
|
*/ |
96
|
|
|
public function setData($data) |
97
|
|
|
{ |
98
|
|
|
$this->_data = $data; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get data |
103
|
|
|
* |
104
|
|
|
* @return mixed $data , data injected into the container |
105
|
|
|
*/ |
106
|
|
|
public function getData() |
107
|
|
|
{ |
108
|
|
|
return $this->_data; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get Template folder |
113
|
|
|
*/ |
114
|
|
|
public function getTemplateFolder() |
115
|
|
|
{ |
116
|
|
|
return $this->_templateRootFolder.'/'.$this->_templateFolder.'/'; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Set Template folder |
121
|
|
|
*/ |
122
|
|
|
public function setTemplateFolder($templateFolder) |
123
|
|
|
{ |
124
|
|
|
$this->_templateFolder = $templateFolder; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function renderMustache($filename, $data) |
128
|
|
|
{ |
129
|
|
|
$file = file_get_contents($filename.'.html'); |
130
|
|
|
$m = new \Mustache_Engine; |
131
|
|
|
return $m->render($file, $data); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Get rendered template file |
136
|
|
|
* Accepts one of the types of template files in this order: |
137
|
|
|
* php (.php), html/mustache (.html), markdown (.md) |
138
|
|
|
* |
139
|
|
|
* @param string $filename , file without extension |
140
|
|
|
* @param array $data , associative array of data |
141
|
|
|
* @throws \Exception , template file does not exist |
142
|
|
|
*/ |
143
|
|
|
public function getTemplateFile($filename, $data) |
144
|
|
|
{ |
145
|
|
|
if (is_file($filename.'.php')) { |
146
|
|
|
ob_start(); |
147
|
|
|
include $filename.'.php'; |
148
|
|
|
return ob_get_clean(); |
149
|
|
|
|
150
|
|
|
} elseif (is_file($filename.'.html')) { |
151
|
|
|
return $this->renderMustache($filename, $data); |
152
|
|
|
|
153
|
|
|
} elseif (is_file($filename.'.md')) { |
154
|
|
|
$parsedown = new \Parsedown; |
155
|
|
|
return $parsedown->text(file_get_contents($filename.'.md')); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
throw new \Exception("Template file does not exist ({$filename})"); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function __set($key, $value) |
162
|
|
|
{ |
163
|
|
|
// Capitalize first letter of the key to preserve camel case convention naming |
164
|
|
|
$method = 'set'.ucfirst($key); |
165
|
|
|
if(is_callable(array($this, $method))) { |
166
|
|
|
$this->$method($value); // Execute the native setter function |
167
|
|
|
} else { |
168
|
|
|
$this->_data[$key] = $value; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function __get($key) |
173
|
|
|
{ |
174
|
|
|
$value = null; |
175
|
|
|
|
176
|
|
|
if(array_key_exists($key, $this->_data)) |
177
|
|
|
$value = $this->_data[$key]; |
178
|
|
|
|
179
|
|
|
return $value; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Render container to HTML |
184
|
|
|
* |
185
|
|
|
* @return string $html |
186
|
|
|
*/ |
187
|
|
|
public function toHtml() |
188
|
|
|
{ |
189
|
|
|
$filename = $this->getTemplateFolder().$this->getTemplate(); |
190
|
|
|
$data = (is_subclass_of($this->_data, 'erdiko\core\Container')) ? $this->_data->toHtml() : $this->_data; |
191
|
|
|
|
192
|
|
|
return $this->getTemplateFile($filename, $data); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* toString magic method. |
197
|
|
|
* When casting to a string use the toHtml method to determine how to render |
198
|
|
|
*/ |
199
|
|
|
public function __toString() |
200
|
|
|
{ |
201
|
|
|
return $this->toHtml(); |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.