1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ubiquity\views\engine\micro; |
4
|
|
|
|
5
|
|
|
use Ubiquity\utils\base\UFileSystem; |
6
|
|
|
use Ubiquity\views\engine\TemplateEngine; |
7
|
|
|
use Ubiquity\views\engine\TemplateGenerator; |
8
|
|
|
|
9
|
|
|
class MicroTemplateEngine extends TemplateEngine { |
10
|
|
|
private string $viewsFolder; |
11
|
|
|
private array $parsers = []; |
12
|
|
|
|
13
|
|
|
private function getTemplateParser(string $viewName): TemplateParser { |
14
|
|
|
if (!isset ($this->parsers [$viewName])) { |
15
|
|
|
$this->parsers [$viewName] = new TemplateParser ($this->viewsFolder . $viewName); |
16
|
|
|
} |
17
|
|
|
return $this->parsers [$viewName]; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function __construct() { |
21
|
|
|
$this->viewsFolder = \ROOT . \DS . 'views' . \DS; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/* |
25
|
|
|
* (non-PHPdoc) |
26
|
|
|
* @see TemplateEngine::render() |
27
|
|
|
*/ |
28
|
|
|
public function render(string $viewName, ?array $pData = [], bool $asString = false) { |
29
|
|
|
if (\is_array($pData)) { |
|
|
|
|
30
|
|
|
\extract($pData); |
31
|
|
|
} |
32
|
|
|
$content = eval ('?>' . $this->getTemplateParser($viewName)->__toString()); |
33
|
|
|
if ($asString) { |
34
|
|
|
return $content; |
35
|
|
|
} |
36
|
|
|
echo $content; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function getBlockNames(string $templateName): array { |
40
|
|
|
return []; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function getCode(string $templateName): string { |
44
|
|
|
$fileName = $this->viewsFolder . $templateName; |
45
|
|
|
return UFileSystem::load($fileName); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function exists(string $name): bool { |
49
|
|
|
$filename = $this->viewsFolder . $name; |
50
|
|
|
return \file_exists($filename); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function addFunction(string $name, $callback, array $options = []): void { |
54
|
|
|
throw new \BadMethodCallException('addFunction method has no sense with MicroTemplateEngine'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
protected function addFilter(string $name, $callback, array $options = []): void { |
58
|
|
|
throw new \BadMethodCallException('addFilter method has no sense with MicroTemplateEngine'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
protected function addExtension($extension): void { |
62
|
|
|
throw new \BadMethodCallException('addExtension method has no sense with MicroTemplateEngine'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function getGenerator(): ?TemplateGenerator { |
66
|
|
|
return null; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|