|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Template.php - Template engine |
|
5
|
|
|
* |
|
6
|
|
|
* Generate templates with template vars. |
|
7
|
|
|
* |
|
8
|
|
|
* @package jaxon-core |
|
|
|
|
|
|
9
|
|
|
* @author Thierry Feuzeu <[email protected]> |
|
10
|
|
|
* @copyright 2016 Thierry Feuzeu <[email protected]> |
|
11
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License |
|
12
|
|
|
* @link https://github.com/jaxon-php/jaxon-core |
|
13
|
|
|
*/ |
|
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
namespace Jaxon\Utils\Template; |
|
16
|
|
|
|
|
17
|
|
|
use stdClass; |
|
18
|
|
|
|
|
19
|
|
|
class Engine |
|
|
|
|
|
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* The namespaces |
|
23
|
|
|
* |
|
24
|
|
|
* @var array $aNamespaces |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $aNamespaces; |
|
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* The constructor |
|
30
|
|
|
* |
|
31
|
|
|
* @param string $sTemplateDir The template directory |
|
|
|
|
|
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct($sTemplateDir) |
|
|
|
|
|
|
34
|
|
|
{ |
|
35
|
|
|
$sTemplateDir = rtrim(trim($sTemplateDir), '/\\'); |
|
|
|
|
|
|
36
|
|
|
$this->aNamespaces = [ |
|
37
|
|
|
'jaxon' => [ |
|
38
|
|
|
'directory' => $sTemplateDir . DIRECTORY_SEPARATOR, |
|
39
|
|
|
'extension' => '.php', |
|
40
|
|
|
], |
|
41
|
|
|
'pagination' => [ |
|
42
|
|
|
'directory' => $sTemplateDir . DIRECTORY_SEPARATOR . 'pagination' . DIRECTORY_SEPARATOR, |
|
43
|
|
|
'extension' => '.php', |
|
44
|
|
|
], |
|
45
|
|
|
]; |
|
46
|
|
|
} |
|
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Add a namespace to the template system |
|
50
|
|
|
* |
|
51
|
|
|
* @param string $sNamespace The namespace name |
|
|
|
|
|
|
52
|
|
|
* @param string $sDirectory The namespace directory |
|
|
|
|
|
|
53
|
|
|
* @param string $sExtension The extension to append to template names |
|
|
|
|
|
|
54
|
|
|
* |
|
55
|
|
|
* @return void |
|
56
|
|
|
*/ |
|
57
|
|
|
public function addNamespace($sNamespace, $sDirectory, $sExtension = '') |
|
58
|
|
|
{ |
|
59
|
|
|
// The 'jaxon' key cannot be overriden |
|
60
|
|
|
if($sNamespace == 'jaxon' || $sNamespace == 'pagination') |
|
61
|
|
|
{ |
|
62
|
|
|
return; |
|
63
|
|
|
} |
|
64
|
|
|
// Save the namespace |
|
65
|
|
|
$this->aNamespaces[$sNamespace] = [ |
|
66
|
|
|
'directory' => rtrim(trim($sDirectory), "/\\") . DIRECTORY_SEPARATOR, |
|
67
|
|
|
'extension' => $sExtension, |
|
68
|
|
|
]; |
|
69
|
|
|
} |
|
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Set a new directory for pagination templates |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $sDirectory The directory path |
|
|
|
|
|
|
75
|
|
|
* |
|
76
|
|
|
* @return void |
|
77
|
|
|
*/ |
|
78
|
|
|
public function pagination($sDirectory) |
|
79
|
|
|
{ |
|
80
|
|
|
$this->aNamespaces['pagination']['directory'] = $sDirectory; |
|
81
|
|
|
} |
|
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Render a template |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $sPath The path to the template |
|
|
|
|
|
|
87
|
|
|
* @param array $aVars The template vars |
|
|
|
|
|
|
88
|
|
|
* |
|
89
|
|
|
* @return string |
|
90
|
|
|
*/ |
|
91
|
|
|
private function _render($sPath, array $aVars) |
|
92
|
|
|
{ |
|
93
|
|
|
// Make the template vars available as attributes |
|
94
|
|
|
$xData = new stdClass(); |
|
95
|
|
|
foreach($aVars as $sName => $xValue) |
|
96
|
|
|
{ |
|
97
|
|
|
$sName = (string)$sName; |
|
|
|
|
|
|
98
|
|
|
$xData->$sName = $xValue; |
|
99
|
|
|
} |
|
100
|
|
|
// Render the template |
|
101
|
|
|
$cRenderer = function() use($sPath) { |
|
102
|
|
|
ob_start(); |
|
103
|
|
|
include($sPath); |
|
104
|
|
|
return ob_get_clean(); |
|
105
|
|
|
}; |
|
106
|
|
|
// Call the closure in the context of the $xData object. |
|
107
|
|
|
// So the keyworg '$this' in the template will refer to the $xData object. |
|
108
|
|
|
return \call_user_func($cRenderer->bindTo($xData)); |
|
109
|
|
|
} |
|
|
|
|
|
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Render a template |
|
113
|
|
|
* |
|
114
|
|
|
* @param string $sTemplate The name of template to be rendered |
|
|
|
|
|
|
115
|
|
|
* @param array $aVars The template vars |
|
|
|
|
|
|
116
|
|
|
* |
|
117
|
|
|
* @return string |
|
118
|
|
|
*/ |
|
119
|
|
|
public function render($sTemplate, array $aVars = []) |
|
120
|
|
|
{ |
|
121
|
|
|
$sTemplate = trim($sTemplate); |
|
|
|
|
|
|
122
|
|
|
// Get the namespace name |
|
123
|
|
|
$sNamespace = ''; |
|
|
|
|
|
|
124
|
|
|
$iSeparatorPosition = strrpos($sTemplate, '::'); |
|
125
|
|
|
if($iSeparatorPosition !== false) |
|
126
|
|
|
{ |
|
127
|
|
|
$sNamespace = substr($sTemplate, 0, $iSeparatorPosition); |
|
128
|
|
|
$sTemplate = substr($sTemplate, $iSeparatorPosition + 2); |
|
|
|
|
|
|
129
|
|
|
} |
|
130
|
|
|
// The default namespace is 'jaxon' |
|
131
|
|
|
if(!($sNamespace = trim($sNamespace))) |
|
|
|
|
|
|
132
|
|
|
{ |
|
133
|
|
|
$sNamespace = 'jaxon'; |
|
134
|
|
|
} |
|
135
|
|
|
// Check if the namespace is defined |
|
136
|
|
|
if(!key_exists($sNamespace, $this->aNamespaces)) |
|
137
|
|
|
{ |
|
138
|
|
|
return ''; |
|
139
|
|
|
} |
|
140
|
|
|
$aNamespace = $this->aNamespaces[$sNamespace]; |
|
141
|
|
|
// Get the template path |
|
142
|
|
|
$sTemplatePath = $aNamespace['directory'] . $sTemplate . $aNamespace['extension']; |
|
143
|
|
|
// Render the template |
|
144
|
|
|
return $this->_render($sTemplatePath, $aVars); |
|
145
|
|
|
} |
|
|
|
|
|
|
146
|
|
|
} |
|
147
|
|
|
|