1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Charcoal\View\Twig; |
4
|
|
|
|
5
|
|
|
// 3rd-party libraries (`twigphp/twig`) dependencies |
6
|
|
|
use Twig_Environment; |
7
|
|
|
|
8
|
|
|
// Intra-module (`charcoal-view`) depentencies |
9
|
|
|
use Charcoal\View\AbstractEngine; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* |
13
|
|
|
*/ |
14
|
|
|
class TwigEngine extends AbstractEngine |
15
|
|
|
{ |
16
|
|
|
const DEFAULT_CACHE_PATH = '../cache/twig'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var Twig_Environment $twig |
20
|
|
|
*/ |
21
|
|
|
private $twig; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @return string |
25
|
|
|
*/ |
26
|
|
|
public function type() |
27
|
|
|
{ |
28
|
|
|
return 'twig'; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @return Twig_Environment |
33
|
|
|
*/ |
34
|
|
|
public function twig() |
35
|
|
|
{ |
36
|
|
|
if ($this->twig === null) { |
37
|
|
|
$this->twig = $this->createTwig(); |
38
|
|
|
} |
39
|
|
|
return $this->twig; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return Twig_Environment |
44
|
|
|
*/ |
45
|
|
|
protected function createTwig() |
46
|
|
|
{ |
47
|
|
|
$twig = new Twig_Environment($this->loader(), [ |
|
|
|
|
48
|
|
|
'cache' => $this->cache(), |
49
|
|
|
'charset' => 'utf-8', |
50
|
|
|
'debug' => false |
51
|
|
|
]); |
52
|
|
|
|
53
|
|
|
return $twig; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Set the engine's cache implementation. |
58
|
|
|
* |
59
|
|
|
* @param mixed $cache A Twig cache option. |
60
|
|
|
* @return void |
61
|
|
|
*/ |
62
|
|
|
protected function setCache($cache) |
63
|
|
|
{ |
64
|
|
|
/** |
65
|
|
|
* If NULL is specified, the value is converted to FALSE |
66
|
|
|
* because Twig internally requires FALSE to disable the cache. |
67
|
|
|
*/ |
68
|
|
|
if ($cache === null) { |
69
|
|
|
$cache = false; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
parent::setCache($cache); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string $templateIdent The template identifier to load and render. |
77
|
|
|
* @param mixed $context The rendering context. |
78
|
|
|
* @return string The rendered template string. |
79
|
|
|
*/ |
80
|
|
|
public function render($templateIdent, $context) |
81
|
|
|
{ |
82
|
|
|
$arrayContext = json_decode(json_encode($context), true); |
83
|
|
|
return $this->twig()->render($templateIdent, $arrayContext); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $templateString The template string to render. |
88
|
|
|
* @param mixed $context The rendering context. |
89
|
|
|
* @return string The rendered template string. |
90
|
|
|
*/ |
91
|
|
|
public function renderTemplate($templateString, $context) |
92
|
|
|
{ |
93
|
|
|
$template = $this->twig()->createTemplate($templateString); |
94
|
|
|
$arrayContext = json_decode(json_encode($context), true); |
95
|
|
|
return $template->render($arrayContext); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: