1
|
|
|
<?php namespace Limoncello\Templates; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2015-2017 [email protected] |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
use Limoncello\Contracts\Templates\TemplatesInterface; |
20
|
|
|
use Twig_Environment; |
21
|
|
|
use Twig_Loader_Filesystem; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @package Limoncello\Templates |
25
|
|
|
*/ |
26
|
|
|
class TwigTemplates implements TemplatesInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var Twig_Environment |
30
|
|
|
*/ |
31
|
|
|
private $twig; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param string $templatesFolder |
35
|
|
|
* @param null|string $cacheFolder |
36
|
|
|
*/ |
37
|
6 |
|
public function __construct(string $templatesFolder, string $cacheFolder = null) |
38
|
|
|
{ |
39
|
|
|
// For Twig options see http://twig.sensiolabs.org/doc/api.html |
40
|
|
|
$options = [ |
41
|
6 |
|
'cache' => $cacheFolder === null ? false : $cacheFolder, |
42
|
|
|
'auto_reload' => false, |
43
|
|
|
]; |
44
|
|
|
|
45
|
6 |
|
$this->twig = new Twig_Environment(new Twig_Loader_Filesystem($templatesFolder), $options); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return Twig_Environment |
50
|
|
|
*/ |
51
|
2 |
|
public function getTwig(): Twig_Environment |
52
|
|
|
{ |
53
|
2 |
|
return $this->twig; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @inheritdoc |
58
|
|
|
*/ |
59
|
1 |
|
public function render(string $name, array $context = []): string |
60
|
|
|
{ |
61
|
1 |
|
return $this->getTwig()->render($name, $context); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|