-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTemplate.php
More file actions
executable file
·217 lines (190 loc) · 7.27 KB
/
Template.php
File metadata and controls
executable file
·217 lines (190 loc) · 7.27 KB
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
namespace TestPlugin {
/**
* Class Template - a very simple PHP class for rendering PHP templates
*
* Fetched from: https://www.daggerhart.com/simple-php-template-class/
* Original Author: Jonathan Daggerhart
*
*/
class Template {
private static $isDebug = false;
public static function enableDebug() {Template::$isDebug = true;}
public static function disableDebug() {Template::$isDebug = false;}
public static function isDebug():bool {return Template::$isDebug;}
/**
* Root location of templates
*
* @var string
*/
public $rootFolder;
/**
* Root location of page templates
*
* @var string
*/
public $pagesTemplateFolder;
/**
* Extra data to provide to all templates
*
* @var array
*/
public $extraData;
/**
* Template constructor.
*
* @param string $rootFolder
* @param string $pagesFolder
* @param array $extraData
*/
function __construct(string $rootFolder = "", string $pagesFolder = "", array $extraData = []) {
if(!empty($rootFolder)) {
$this->set_root_folder($rootFolder);
}
if(!empty($pagesFolder)) {
$this->set_page_folder($pagesFolder);
}
if(!empty($extraData)) {
$this->extraData = $extraData;
}
}
/**
* Simple method for updating the root folder where templates are located.
*
* @param string $folder
*/
function set_root_folder($folder) {
// normalize the internal folder value by removing any final slashes
$this->rootFolder = rtrim($folder, '/');
}
/**
* Simple method for updating the root folder where templates for pages are located.
*
* @param string $pagesFolder
*/
function set_page_folder($pagesFolder) {
// normalize the internal folder value by removing any final slashes
$this->pagesTemplateFolder = rtrim($pagesFolder, '/');
}
/**
* Find and attempt to render a template with variables. If no name is passed, but a page is, the page name will be used as the template name.
*
* @param string[]|string $names The "suggested" template to render. This will be used to lookup the first template that matches.
* @param mixed[] $variables Any variables that the template should have
* @param string $pageName The name of the page to use to look up the template
*
* @return string The output string of the template being rendered
*/
function render($names = "", $variables = array(), $pageName = ""):string {
$template = $this->find_template($names, $pageName);
if(Template::$isDebug) {
error_log('render template:' . $template);
}
if(!empty($template)) {
$fileinfo = pathinfo($template);
$fetcherFilename = $fileinfo['dirname'].DIRECTORY_SEPARATOR.$fileinfo['filename'].'-data.php';
$variables = $this->executeDataFetcher($fetcherFilename, $variables);
}
$output = '';
if ($template) {
$output = $this->render_template($template, $variables);
}
return $output;
}
/**
* Executes a PHP file that loads data for a given page template
*
* @param string $filename The absolute path, with filename, to the data fetcher to execute
* @param array $data The data array passed to the data fetcher, and combined with it's result
*
* @return array The resulting dta array from the data fetcher (or empty)
*/
function executeDataFetcher(string $filename, array $data):array {
if (!empty($filename) && file_exists($filename)) {
$fetchedData = (include($filename));//ensures any "result" is enclosed as an expression
if(($data===TRUE) || empty($data)) {
$data = [];
}
$data = array_merge($data, $fetchedData);
}
return $data;
}
/**
* Look for the first template suggestion
*
* @param string[]|string $names The names of the template to find
* @param string $pageName The page name to consider when looking up the template
*
* @return bool|string
*/
function find_template($names = "", $pageName = "") {
$namesArray = array();
if (!is_array($names)) {
$namesArray = array($names);
} else {
$namesArray = $names;
}
$namesArray = array_reverse($namesArray);
$found = false;
foreach ($namesArray as $name) {
if(!empty($pageName)) {
if(empty($name)) {
$name = $pageName;
}
$file = implode(DIRECTORY_SEPARATOR, [
$this->pagesTemplateFolder,
$pageName,
$name.".php"
]);
if(Template::$isDebug) {
error_log('template path in pages folder to check:' . $file);
}
if (file_exists($file)) {
$found = $file;
break;
}
}
$file = implode(DIRECTORY_SEPARATOR, [
$this->rootFolder,
$name.".php"
]);
if(Template::$isDebug) {
error_log('template path in root folder to check:' . $file);
}
if (file_exists($file)) {
$found = $file;
break;
}
}
return $found;
}
/**
* Execute the template by extracting the variables into scope, and including
* the template file.
*
* @internal param $template
* @internal param $variables
*
* @return string
*/
//variables kept unnamed to avoid scoping them to the template
function render_template( /*$template, $variables*/) {
ob_start();
$model = array(
//add any special values here
"template" => func_get_args()[0]
);
$model = array_merge($model, $this->extraData);
//put any functions directly in scope
$expectInModel = function ($name) use (&$model) {
if(isset($name) && !array_key_exists($name, $model)) {
throw new \Exception("The variable '".$name."' was not provided to the template '".$model['template']."'.");
}
};
$model = array_merge($model, func_get_args()[1]);//combine with variables passed in arg1
//include the template from arg0
include func_get_args()[0];
return ob_get_clean();
}
}
}