twig.php
974 Bytes
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
<?php
namespace Template;
final class Twig {
private $data = array();
public function set($key, $value) {
$this->data[$key] = $value;
}
public function render($filename, $code = '') {
if (!$code) {
$file = DIR_TEMPLATE . $filename . '.twig';
if (is_file($file)) {
$code = file_get_contents($file);
} else {
throw new \Exception('Error: Could not load template ' . $file . '!');
exit();
}
}
// initialize Twig environment
$config = array(
'autoescape' => false,
'debug' => false,
'auto_reload' => true,
'cache' => DIR_CACHE . 'template/'
);
try {
$loader = new \Twig\Loader\ArrayLoader(array($filename . '.twig' => $code));
$twig = new \Twig\Environment($loader, $config);
return $twig->render($filename . '.twig', $this->data);
} catch (\Exception $e) {
trigger_error('Error: Could not load template ' . $filename . '!');
exit();
}
}
}