1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\ViewEngine; |
4
|
|
|
|
5
|
|
|
use Core\ViewEngineInterface; |
6
|
|
|
use Core\Application; |
7
|
|
|
use \Twig_Loader_Filesystem; |
8
|
|
|
use \Twig_Environment; |
9
|
|
|
use \Twig_SimpleFunction; |
10
|
|
|
|
11
|
|
|
class Twig implements ViewEngineInterface |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
private $PUBLIC_PATH = 'public/'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var Twig_Environment |
18
|
|
|
*/ |
19
|
|
|
private $twig; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Twig template file |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $templateFilename = ''; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array engine setting |
30
|
|
|
*/ |
31
|
|
|
private $settings = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* rendering template variables |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
private $var = []; |
38
|
|
|
|
39
|
|
|
public function __construct($settings = []) |
40
|
|
|
{ |
41
|
|
|
$loader = new Twig_Loader_Filesystem($settings['path']); |
42
|
|
|
$this->twig = new Twig_Environment($loader, $settings['settings']); |
43
|
|
|
|
44
|
|
|
$this->settings = $settings; |
45
|
|
|
|
46
|
|
|
$this->addCssFunction(); |
47
|
|
|
$this->addAssetFunction(); |
48
|
|
|
$this->addUrlFunction(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function set($key, $value) |
52
|
|
|
{ |
53
|
|
|
$this->var[$key] = $value; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function getVariables() |
57
|
|
|
{ |
58
|
|
|
return $this->var; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function file($fileName) |
62
|
|
|
{ |
63
|
|
|
$this->templateFilename = $fileName . $this->settings['postfix']; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function getFile() |
67
|
|
|
{ |
68
|
|
|
return $this->templateFilename; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function render() |
72
|
|
|
{ |
73
|
|
|
echo $this->twig->render($this->templateFilename, $this->var); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
private function addCssFunction() |
77
|
|
|
{ |
78
|
|
|
$this->twig->addFunction(new Twig_SimpleFunction( |
|
|
|
|
79
|
|
|
'css', |
80
|
|
|
[$this, 'cssFunction'], |
81
|
|
|
['is_safe' => ['html']] |
82
|
|
|
)); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
private function addAssetFunction() |
86
|
|
|
{ |
87
|
|
|
$this->twig->addFunction(new Twig_SimpleFunction( |
|
|
|
|
88
|
|
|
'asset', |
89
|
|
|
[$this, 'assetFunction'] |
90
|
|
|
)); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
private function addUrlFunction() |
94
|
|
|
{ |
95
|
|
|
$this->twig->addFunction(new Twig_SimpleFunction( |
|
|
|
|
96
|
|
|
'url', |
97
|
|
|
[$this, 'urlFunction'] |
98
|
|
|
)); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function cssFunction($url) |
102
|
|
|
{ |
103
|
|
|
return sprintf('<link rel="stylesheet" type="text/css" href="%s">', $url); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function assetFunction($file) |
107
|
|
|
{ |
108
|
|
|
return Application::getInstance()->url($this->PUBLIC_PATH . $file); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function urlFunction($url = '') |
112
|
|
|
{ |
113
|
|
|
return Application::getInstance()->url($url); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.