forked from Block8/Octo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Template.php
161 lines (127 loc) · 3.23 KB
/
Template.php
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
<?php
namespace Octo;
use b8\Config;
use Octo\Event;
use Twig_Environment;
use Twig_Loader_Filesystem;
class Template
{
/**
* @var bool
*/
protected static $initialised = false;
/**
* @var \Twig_Loader_Filesystem
*/
protected static $loader;
/**
* @var \Twig_Environment
*/
protected static $twig;
/**
* @var string|bool
*/
protected static $cache;
/**
* @var \Twig_TemplateInterface
*/
protected $template;
/**
* @var string
*/
protected $templateName;
/**
* @var array
*/
protected $variables = [];
/**
* Sets up the twig template loader.
*/
public static function init()
{
self::$loader = new Twig_Loader_Filesystem();
$config = Config::getInstance();
foreach (array_reverse($config->get('Octo.paths.templates', [])) as $path) {
self::$loader->addPath($path);
}
foreach (array_reverse($config->get('Octo.paths.admin_templates', [])) as $path) {
self::$loader->addPath($path, 'admin');
}
if (OCTO_CACHE_ENABLED) {
self::$cache = OCTO_CACHE_PATH;
} else {
self::$cache = false;
}
self::$twig = new Twig_Environment(self::$loader, [
'charset' => 'UTF-8',
'cache' => self::$cache,
'auto_reload' => true,
'strict_variables' => false,
]);
$functions = [];
Event::trigger('TemplateInit', $functions);
foreach ($functions as $name => $callable) {
self::addFunction($name, $callable);
}
self::$initialised = true;
}
public static function addFunction($functionName, callable $function)
{
self::$twig->addFunction(new \Twig_SimpleFunction($functionName, $function));
}
/**
* Load a template.
* @param string $template
* @param string $namespace
*/
public function __construct($template, $namespace = null)
{
if (!self::$initialised) {
self::init();
}
$template = $template . '.twig';
if (!is_null($namespace)) {
$template = '@' . $namespace . '/' . $template;
}
$this->templateName = $template;
$this->template = self::$twig->loadTemplate($template);
Event::trigger('PublicTemplateLoaded', $this);
}
public function render()
{
$output = $this->template->render($this->variables);
Event::trigger('OnTemplateRender', $output);
return $output;
}
public function get($key)
{
if (array_key_exists($key, $this->variables)) {
return $this->variables[$key];
}
return null;
}
public function set($key, $value)
{
$this->variables[$key] = $value;
}
public function getContext()
{
return $this->variables;
}
public function setContext($context)
{
$this->variables = $context;
}
public function __get($key)
{
return $this->get($key);
}
public function __set($key, $value)
{
$this->set($key, $value);
}
public function __toString()
{
return $this->render();
}
}