1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Brendt\Stitcher\Template\Smarty; |
4
|
|
|
|
5
|
|
|
use \Smarty; |
6
|
|
|
use Brendt\Stitcher\Template\TemplateEngine; |
7
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* The Smarty template engine. |
11
|
|
|
*/ |
12
|
|
|
class SmartyEngine extends Smarty implements TemplateEngine |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Create the Smarty engine, set the template- and cache directory; and add the plugin directory. |
16
|
|
|
* |
17
|
|
|
* @param string $templateDir |
18
|
|
|
* @param string $cacheDir |
19
|
|
|
*/ |
20
|
|
|
public function __construct(?string $templateDir = './src', ?string $cacheDir = './.cache') { |
21
|
|
|
parent::__construct(); |
22
|
|
|
|
23
|
|
|
$this->addTemplateDir($templateDir); |
24
|
|
|
$this->setCompileDir($cacheDir); |
25
|
|
|
$this->addPluginsDir([__DIR__]); |
26
|
|
|
|
27
|
|
|
$this->caching = false; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
|
|
public function renderTemplate(SplFileInfo $template) { |
34
|
|
|
return $this->fetch($template->getRealPath()); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
|
|
public function addTemplateVariables(array $variables) { |
41
|
|
|
foreach ($variables as $name => $variable) { |
42
|
|
|
$this->assign($name, $variable); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
|
|
public function clearTemplateVariables() { |
52
|
|
|
$this->clearAllAssign(); |
53
|
|
|
|
54
|
|
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
|
|
public function addTemplateVariable($name, $value) { |
61
|
|
|
$this->assign($name, $value); |
62
|
|
|
|
63
|
|
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Check whether a template variable is set |
68
|
|
|
* |
69
|
|
|
* @param string $name |
70
|
|
|
* |
71
|
|
|
* @return bool |
72
|
|
|
*/ |
73
|
|
|
public function hasTemplateVariable(string $name) : bool { |
74
|
|
|
return $this->getTemplateVars($name) != null; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritdoc} |
79
|
|
|
*/ |
80
|
|
|
public function clearTemplateVariable($variable) { |
81
|
|
|
$this->clearAssign($variable); |
82
|
|
|
|
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* {@inheritdoc} |
88
|
|
|
*/ |
89
|
|
|
public function getTemplateExtension() { |
90
|
|
|
return 'tpl'; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|