1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Cecil/Cecil package. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) Arnaud Ligny <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Cecil\Renderer; |
12
|
|
|
|
13
|
|
|
use Cecil\Builder; |
14
|
|
|
use Cecil\Renderer\Twig\Extension as TwigExtension; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class Twig. |
18
|
|
|
*/ |
19
|
|
|
class Twig implements RendererInterface |
20
|
|
|
{ |
21
|
|
|
/** @var \Twig\Environment */ |
22
|
|
|
protected $twig; |
23
|
|
|
/** @var string */ |
24
|
|
|
protected $templatesDir; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* {@inheritdoc} |
28
|
|
|
*/ |
29
|
|
|
public function __construct($templatesPath, Builder $builder) |
30
|
|
|
{ |
31
|
|
|
// load layouts |
32
|
|
|
$loader = new \Twig\Loader\FilesystemLoader($templatesPath); |
33
|
|
|
// Twig |
34
|
|
|
$this->twig = new \Twig\Environment($loader, [ |
35
|
|
|
'debug' => true, |
36
|
|
|
'strict_variables' => true, |
37
|
|
|
'autoescape' => false, |
38
|
|
|
'cache' => false, |
39
|
|
|
'auto_reload' => true, |
40
|
|
|
]); |
41
|
|
|
// adds extensions |
42
|
|
|
$this->twig->addExtension(new \Twig\Extension\DebugExtension()); |
43
|
|
|
$this->twig->addExtension(new TwigExtension($builder)); |
44
|
|
|
$this->twig->addExtension(new \Twig\Extension\StringLoaderExtension()); |
45
|
|
|
// set date format & timezone |
46
|
|
|
$this->twig->getExtension(\Twig\Extension\CoreExtension::class) |
47
|
|
|
->setDateFormat($builder->getConfig()->get('date.format')); |
|
|
|
|
48
|
|
|
$this->twig->getExtension(\Twig\Extension\CoreExtension::class) |
49
|
|
|
->setTimezone($builder->getConfig()->get('date.timezone')); |
|
|
|
|
50
|
|
|
// internationalisation |
51
|
|
|
if (extension_loaded('intl')) { |
52
|
|
|
$this->twig->addExtension(new \Twig_Extensions_Extension_Intl()); |
53
|
|
|
} |
54
|
|
|
if (extension_loaded('gettext')) { |
55
|
|
|
$this->twig->addExtension(new \Twig_Extensions_Extension_I18n()); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
|
|
public function addGlobal($name, $value) |
63
|
|
|
{ |
64
|
|
|
$this->twig->addGlobal($name, $value); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
|
|
public function render($template, $variables) |
71
|
|
|
{ |
72
|
|
|
return $this->twig->render($template, $variables); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|