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
|
|
|
class Template |
18
|
|
|
{ |
19
|
|
|
protected $aNamespaces; |
20
|
|
|
|
21
|
|
|
public function __construct($sTemplateDir) |
22
|
|
|
{ |
23
|
|
|
$sTemplateDir = rtrim(trim($sTemplateDir), "/\\"); |
24
|
|
|
$this->aNamespaces = [ |
25
|
|
|
'jaxon' => [ |
26
|
|
|
'directory' => $sTemplateDir . DIRECTORY_SEPARATOR, |
27
|
|
|
'extension' => '.php', |
28
|
|
|
], |
29
|
|
|
'pagination' => [ |
30
|
|
|
'directory' => $sTemplateDir . DIRECTORY_SEPARATOR . 'pagination' . DIRECTORY_SEPARATOR, |
31
|
|
|
'extension' => '.php', |
32
|
|
|
], |
33
|
|
|
]; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Add a namespace to the template system |
38
|
|
|
* |
39
|
|
|
* @param string $sNamespace The namespace name |
40
|
|
|
* @param string $sDirectory The namespace directory |
41
|
|
|
* @param string $sExtension The extension to append to template names |
42
|
|
|
* |
43
|
|
|
* @return void |
44
|
|
|
*/ |
45
|
|
|
public function addNamespace($sNamespace, $sDirectory, $sExtension = '') |
46
|
|
|
{ |
47
|
|
|
// The 'jaxon' key cannot be overriden |
48
|
|
|
if($sNamespace == 'jaxon') |
49
|
|
|
{ |
50
|
|
|
return; |
51
|
|
|
} |
52
|
|
|
// Save the namespace |
53
|
|
|
$this->aNamespaces[$sNamespace] = [ |
54
|
|
|
'directory' => rtrim(trim($sDirectory), "/\\") . DIRECTORY_SEPARATOR, |
55
|
|
|
'extension' => $sExtension, |
56
|
|
|
]; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Set a new directory for pagination templates |
61
|
|
|
* |
62
|
|
|
* @param string $sDirectory The directory path |
63
|
|
|
* |
64
|
|
|
* @return void |
65
|
|
|
*/ |
66
|
|
|
public function setPaginationDir($sDirectory) |
67
|
|
|
{ |
68
|
|
|
$this->addNamespace('pagination', $sDirectory, '.php'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Set a cache directory for the template engine |
73
|
|
|
* |
74
|
|
|
* @param string $sCacheDir The cache directory |
75
|
|
|
* |
76
|
|
|
* @return void |
77
|
|
|
*/ |
78
|
|
|
public function setCacheDir($sCacheDir) |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
// Nothing to do |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Render a template |
85
|
|
|
* |
86
|
|
|
* @param string $sTemplate The name of template to be rendered |
87
|
|
|
* @param string $aVars The template vars |
88
|
|
|
* |
89
|
|
|
* @return string The template content |
90
|
|
|
*/ |
91
|
|
|
public function render($sTemplate, array $aVars = array()) |
92
|
|
|
{ |
93
|
|
|
$sTemplate = trim($sTemplate); |
94
|
|
|
// Get the namespace name |
95
|
|
|
$sNamespace = ''; |
96
|
|
|
$iSeparatorPosition = strrpos($sTemplate, '::'); |
97
|
|
|
if($iSeparatorPosition !== false) |
98
|
|
|
{ |
99
|
|
|
$sNamespace = substr($sTemplate, 0, $iSeparatorPosition); |
100
|
|
|
$sTemplate = substr($sTemplate, $iSeparatorPosition + 2); |
101
|
|
|
} |
102
|
|
|
// The default namespace is 'jaxon' |
103
|
|
|
if(!($sNamespace = trim($sNamespace))) |
104
|
|
|
{ |
105
|
|
|
$sNamespace = 'jaxon'; |
106
|
|
|
} |
107
|
|
|
// Check if the namespace is defined |
108
|
|
|
if(!key_exists($sNamespace, $this->aNamespaces)) |
109
|
|
|
{ |
110
|
|
|
return false; |
111
|
|
|
} |
112
|
|
|
$aNamespace = $this->aNamespaces[$sNamespace]; |
113
|
|
|
// Get the template path |
114
|
|
|
$sTemplatePath = $aNamespace['directory'] . $sTemplate . $aNamespace['extension']; |
115
|
|
|
// Render the template |
116
|
|
|
$xRenderer = new Renderer(); |
117
|
|
|
return $xRenderer->render($sTemplatePath, $aVars); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.