1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Template engine class |
4
|
|
|
*/ |
5
|
|
|
namespace Phile\Plugin\Phile\TemplateTwig\Template; |
6
|
|
|
|
7
|
|
|
use Phile\Core\Container; |
8
|
|
|
use Phile\Core\Registry; |
9
|
|
|
use Phile\Model\Page; |
10
|
|
|
use Phile\Repository\Page as Repository; |
11
|
|
|
use Phile\ServiceLocator\TemplateInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class Twig |
15
|
|
|
* |
16
|
|
|
* @author PhileCMS |
17
|
|
|
* @link https://philecms.com |
18
|
|
|
* @license http://opensource.org/licenses/MIT |
19
|
|
|
* @package Phile\Plugin\Phile\TemplateTwig\Template |
20
|
|
|
*/ |
21
|
|
|
class Twig implements TemplateInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var array the complete phile config |
25
|
|
|
*/ |
26
|
|
|
protected $settings; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array the config for twig |
30
|
|
|
*/ |
31
|
|
|
protected $config; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var Page the page model |
35
|
|
|
*/ |
36
|
|
|
protected $page; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* the constructor |
40
|
|
|
* |
41
|
|
|
* @param array $config the configuration |
42
|
|
|
*/ |
43
|
26 |
|
public function __construct($config = []) |
44
|
|
|
{ |
45
|
26 |
|
$this->config = $config; |
46
|
26 |
|
$this->settings = Container::getInstance()->get('Phile_Config')->toArray(); |
47
|
26 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* method to set the current page |
51
|
|
|
* |
52
|
|
|
* @param Page $page the page model |
53
|
|
|
* |
54
|
|
|
* @return mixed|void |
55
|
|
|
*/ |
56
|
2 |
|
public function setCurrentPage(Page $page) |
57
|
|
|
{ |
58
|
2 |
|
$this->page = $page; |
59
|
2 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* method to render the page/template |
63
|
|
|
* |
64
|
|
|
* @return mixed|string |
65
|
|
|
*/ |
66
|
2 |
|
public function render() |
67
|
|
|
{ |
68
|
2 |
|
$engine = $this->getEngine(); |
69
|
2 |
|
$vars = $this->getTemplateVars(); |
70
|
2 |
|
Container::getInstance()->get('Phile_EventBus')->trigger( |
71
|
2 |
|
'template_engine_registered', |
72
|
2 |
|
['engine' => &$engine, 'data' => &$vars] |
73
|
|
|
); |
74
|
2 |
|
return $this->doRender($engine, $vars); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* wrapper to call the render engine |
79
|
|
|
* |
80
|
|
|
* @param $engine |
81
|
|
|
* @param $vars |
82
|
|
|
* @return mixed |
83
|
|
|
*/ |
84
|
2 |
|
protected function doRender($engine, $vars) |
85
|
|
|
{ |
86
|
|
|
try { |
87
|
2 |
|
$template = $this->getTemplateFileName(); |
88
|
|
|
} catch (\RuntimeException $e) { |
89
|
|
|
return $e->getMessage(); |
90
|
|
|
} |
91
|
2 |
|
return $engine->render($template, $vars); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* get template engine |
96
|
|
|
* |
97
|
|
|
* @return \Twig_Environment |
98
|
|
|
*/ |
99
|
2 |
|
protected function getEngine() |
100
|
|
|
{ |
101
|
2 |
|
$loader = new \Twig_Loader_Filesystem($this->getTemplatePath()); |
102
|
2 |
|
$twig = new \Twig_Environment($loader, $this->config); |
103
|
|
|
|
104
|
|
|
// load the twig debug extension if required |
105
|
2 |
|
if (!empty($this->config['debug'])) { |
106
|
|
|
$twig->addExtension(new \Twig_Extension_Debug()); |
107
|
|
|
} |
108
|
2 |
|
return $twig; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* get template file name |
113
|
|
|
* |
114
|
|
|
* @return string |
115
|
|
|
* @throws \RuntimeException |
116
|
|
|
*/ |
117
|
2 |
|
protected function getTemplateFileName() |
118
|
|
|
{ |
119
|
2 |
|
$template = $this->page->getMeta()->get('template'); |
120
|
2 |
|
if (empty($template)) { |
121
|
2 |
|
$template = 'index'; |
122
|
|
|
} |
123
|
2 |
|
if (!empty($this->config['template-extension'])) { |
124
|
2 |
|
$template .= '.' . $this->config['template-extension']; |
125
|
|
|
} |
126
|
2 |
|
$templatePath = $this->getTemplatePath($template); |
127
|
2 |
|
if (!file_exists($templatePath)) { |
128
|
|
|
throw new \RuntimeException( |
129
|
|
|
"Template file '{$templatePath}' not found.", |
130
|
|
|
1427990135 |
131
|
|
|
); |
132
|
|
|
} |
133
|
2 |
|
return $template; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* get file path to (sub-path) in theme-path |
138
|
|
|
* |
139
|
|
|
* @param string $sub |
140
|
|
|
* @return string |
141
|
|
|
*/ |
142
|
2 |
|
protected function getTemplatePath($sub = '') |
143
|
|
|
{ |
144
|
2 |
|
$themePath = THEMES_DIR . $this->settings['theme']; |
145
|
2 |
|
if (!empty($sub)) { |
146
|
2 |
|
$themePath .= '/' . ltrim($sub, DIRECTORY_SEPARATOR); |
147
|
|
|
} |
148
|
2 |
|
return $themePath; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* get template vars |
153
|
|
|
* |
154
|
|
|
* @return array|mixed |
155
|
|
|
* @throws \Exception |
156
|
|
|
*/ |
157
|
2 |
|
protected function getTemplateVars() |
158
|
|
|
{ |
159
|
|
|
$defaults = [ |
160
|
2 |
|
'content' => $this->page->getContent(), |
161
|
2 |
|
'meta' => $this->page->getMeta(), |
162
|
2 |
|
'current_page' => $this->page, |
163
|
2 |
|
'pages' => $this->page->getRepository()->findAll(), |
164
|
|
|
]; |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @var array $templateVars |
168
|
|
|
*/ |
169
|
2 |
|
$templateVars = Registry::get('templateVars'); |
170
|
2 |
|
$templateVars += $defaults; |
171
|
|
|
|
172
|
2 |
|
return $templateVars; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|