Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
TemplateEngine.php
Go to the documentation of this file.
1<?php
2
4
11
21abstract class TemplateEngine {
22
23 protected Framework $fw;
24
32 abstract public function render(string $viewName, ?array $pData = [], bool $asString = false);
33
39 abstract public function getBlockNames(string $templateName): array;
40
46 abstract public function getCode(string $templateName): string;
47
54 abstract public function addFunction(string $name, $callback, array $options = []): void;
55
62 abstract protected function addFilter(string $name, $callback, array $options = []): void;
63
64 abstract protected function addExtension($extension): void;
65
72 abstract public function exists(string $name): bool;
73
74 protected function hasThemeResource(&$resource): bool {
75 $resource = \str_replace('@activeTheme/', '', $resource, $count);
76 return $count > 0;
77 }
78
87 public function setTheme(string $theme, string $themeFolder = ThemesManager::THEMES_FOLDER): string {
88 $root = DDDManager::getActiveViewFolder();
89 $path = $root . $themeFolder . \DS . $theme;
90 if ($theme == '') {
91 $path = $root;
92 }
93 if (!\file_exists($path)) {
94 throw new ThemesException (sprintf('The path `%s` does not exists!', $path));
95 }
96 return $path;
97 }
98
99 protected function addFunctions(): void {
100 $safe = ['is_safe' => ['html']];
101 $this->fw = new Framework();
102 $this->addFunction('path', function ($name, $params = [], $absolute = false) {
103 return Router::path($name, $params, $absolute);
104 });
105
106 $this->addFunction('url', function ($name, $params = []) {
107 return Router::url($name, $params);
108 });
109
110 if (\class_exists('\\Ubiquity\\security\\csrf\\UCsrfHttp')) {
111 $this->addFunction('csrfMeta', function ($name) {
112 return \Ubiquity\security\csrf\UCsrfHttp::getTokenMeta($name);
113 }, $safe);
114 $this->addFunction('csrf', function ($name) {
115 return \Ubiquity\security\csrf\UCsrfHttp::getTokenField($name);
116 }, $safe);
117 }
118
119 if (\class_exists('\\Ubiquity\security\\acl\\AclManager')) {
120 $this->addFunction('isAllowedRoute', function ($role, $routeName) {
121 return \Ubiquity\security\acl\AclManager::isAllowedRoute($role, $routeName);
122 }, []);
123 }
124
125 $this->addFunction('css', function ($resource, $parameters = [], $absolute = false) {
126 if ($this->hasThemeResource($resource)) {
127 return $this->safeString(AssetsManager::css_($resource, $parameters, $absolute));
128 }
129 return $this->safeString(AssetsManager::css($resource, $parameters, $absolute));
130 }, $safe);
131
132 $this->addFunction('js', function ($resource, $parameters = [], $absolute = false) {
133 if ($this->hasThemeResource($resource)) {
134 return $this->safeString(AssetsManager::js_($resource, $parameters, $absolute));
135 }
136 return $this->safeString(AssetsManager::js($resource, $parameters, $absolute));
137 }, $safe);
138
139 $this->addFunction('img', function ($resource, $parameters = [], $absolute = false) {
140 if ($this->hasThemeResource($resource)) {
141 return $this->safeString(AssetsManager::img_($resource, $parameters, $absolute));
142 }
143 return $this->safeString(AssetsManager::img($resource, $parameters, $absolute));
144 }, $safe);
145 }
146
147 protected function safeString(string $str) {
148 return $str;
149 }
150
151 abstract public function getGenerator(): ?TemplateGenerator;
152
158 public function generateTemplateSourceFromFile(string $templateName): string {
159 $result = $this->getCode($templateName);
160 return $this->generateTemplateSource($result);
161 }
162
163 public function generateTemplateSource(string $source): string {
164 $gen = $this->getGenerator();
165 if ($gen != null) {
166 return $gen->parseFromTwig($source);
167 }
168 return $source;
169 }
170}
Assets manager for css and js inclusions in templates.
Manager for a Domain Driven Design approach.
Exceptions for ThemesManager.
setTheme(string $theme, string $themeFolder=ThemesManager::THEMES_FOLDER)
Defines the activeTheme.
getBlockNames(string $templateName)
Returns the defined block names.
addFilter(string $name, $callback, array $options=[])
getCode(string $templateName)
Returns the source code of the template.
addFunction(string $name, $callback, array $options=[])
render(string $viewName, ?array $pData=[], bool $asString=false)
Renders a view.
generateTemplateSourceFromFile(string $templateName)
Generates the source for this engine from a twig model template.
exists(string $name)
Checks if we have the source code of a template, given its name.
Ubiquity abstract TemplateGenerator class.