1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Brendt\Stitcher\Template\Twig; |
4
|
|
|
|
5
|
|
|
use Brendt\Stitcher\Lib\Browser; |
6
|
|
|
use Brendt\Stitcher\Template\TemplateEngine; |
7
|
|
|
use Brendt\Stitcher\Template\TemplatePlugin; |
8
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
9
|
|
|
use Twig_Environment; |
10
|
|
|
use Twig_Loader_Filesystem; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* The Twig template engine. |
14
|
|
|
*/ |
15
|
|
|
class TwigEngine extends Twig_Environment implements TemplateEngine |
16
|
|
|
{ |
17
|
|
|
private $variables = []; |
18
|
|
|
|
19
|
|
|
public function __construct(Browser $browser, TemplatePlugin $templatePlugin) { |
20
|
|
|
$loader = new Twig_Loader_Filesystem($browser->getTemplateDir()); |
21
|
|
|
|
22
|
|
|
parent::__construct($loader, [ |
23
|
|
|
'cache' => false, |
24
|
|
|
]); |
25
|
|
|
|
26
|
|
|
$this->addFunction(new \Twig_SimpleFunction('meta', [$templatePlugin, 'meta'], ['is_safe' => ['html'],])); |
27
|
|
|
$this->addFunction(new \Twig_SimpleFunction('css', [$templatePlugin, 'css'], ['is_safe' => ['html'],])); |
28
|
|
|
$this->addFunction(new \Twig_SimpleFunction('js', [$templatePlugin, 'js'], ['is_safe' => ['html'],])); |
29
|
|
|
$this->addFunction(new \Twig_SimpleFunction('image', [$templatePlugin, 'image'])); |
30
|
|
|
$this->addFunction(new \Twig_SimpleFunction('file', [$templatePlugin, 'file'])); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function renderTemplate(SplFileInfo $template) { |
34
|
|
|
return $this->render($template->getRelativePathname(), $this->variables); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function addTemplateVariables(array $variables) { |
38
|
|
|
$this->variables += $variables; |
39
|
|
|
|
40
|
|
|
return $this; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function hasTemplateVariable(string $name) : bool { |
44
|
|
|
return isset($this->variables[$name]); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function clearTemplateVariables() { |
48
|
|
|
$this->variables = []; |
49
|
|
|
|
50
|
|
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function addTemplateVariable($name, $value) { |
54
|
|
|
$this->variables[$name] = $value; |
55
|
|
|
|
56
|
|
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function clearTemplateVariable($variable) { |
60
|
|
|
unset($this->variables[$variable]); |
61
|
|
|
|
62
|
|
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function getTemplateExtensions(): array { |
66
|
|
|
return ['html', 'twig']; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|