-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathTwigEngine.php
More file actions
90 lines (75 loc) · 2.16 KB
/
Copy pathTwigEngine.php
File metadata and controls
90 lines (75 loc) · 2.16 KB
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
<?php
declare(strict_types=1);
namespace Bow\View\Engine;
use Bow\Application\Exception\ApplicationException;
use Bow\Configuration\Loader as ConfigurationLoader;
use Bow\View\EngineAbstract;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;
use Twig\TwigFunction;
class TwigEngine extends EngineAbstract
{
/**
* The engine name
*
* @var string
*/
protected string $name = 'twig';
/**
* The template engine instance
*
* @var Environment
*/
private Environment $template;
/**
* TwigEngine constructor.
*
* @param array $config
* @return Environment
* @throws ApplicationException
*/
public function __construct(array $config)
{
$this->config = $config;
$loader = new FilesystemLoader($config['path']);
$additional_options = $config['additional_options'] ?? [];
$env = [
'auto_reload' => true,
'debug' => true,
'cache' => $config['cache']
];
if (is_array($additional_options)) {
foreach ($additional_options as $key => $additional_option) {
$env[$key] = $additional_option;
}
}
$this->template = new Environment($loader, $env);
// Add variable in global scope in the Twig use case
$configuration_loader = ConfigurationLoader::getInstance();
$this->template->addGlobal('_public', $configuration_loader['app.static']);
$this->template->addGlobal('_root', $configuration_loader['app.root']);
// Add function in global scope in Twig use case
foreach (EngineAbstract::HELPERS as $helper) {
$this->template->addFunction(
new TwigFunction($helper, $helper)
);
}
}
/**
* @inheritDoc
*/
public function render($filename, array $data = []): string
{
$filename = $this->checkParseFile($filename);
return $this->template->render($filename, $data);
}
/**
* The get engine instance
*
* @return Environment
*/
public function getEngine(): Environment
{
return $this->template;
}
}