Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
Twig.php
Go to the documentation of this file.
1<?php
2
4
5use Twig\Environment;
6use Twig\Loader\FilesystemLoader;
7use Twig\TwigFilter;
8use Twig\TwigFunction;
9use Twig\TwigTest;
20
31class Twig extends TemplateEngine {
32 private $twig;
33 private $loader;
34
35 public function __construct($options = []) {
36 $loader = new FilesystemLoader (\ROOT . \DS . 'views' . \DS);
37 $loader->addPath(Startup::getFrameworkDir() . '/../core/views/engines/twig', 'framework');
38 $this->loader = $loader;
39
40 if (($options ['cache'] ?? false) === true) {
41 $options ['cache'] = CacheManager::getCacheSubDirectory('views');
42 }
43
44 $this->twig = new Environment ($loader, $options);
45
46 if (isset($options['extensions'])) {
47 foreach ($options['extensions'] as $ext) {
48 $this->twig->addExtension(new $ext());
49 }
50 }
51
52 if (isset ($options ['activeTheme'])) {
53 ThemesManager::setActiveThemeFromTwig($options ['activeTheme']);
54 $this->setTheme($options ['activeTheme'], ThemesManager::THEMES_FOLDER);
55 unset ($options ['activeTheme']);
56 } else {
57 $this->loader->setPaths([\ROOT . \DS . 'views'], 'activeTheme');
58 }
59
60 $this->addFunctions();
61 }
62
63 protected function addFunctions(): void {
64 parent::addFunctions();
65 $t = new TwigFunction ('t', function ($context, $id, array $parameters = array(), $domain = null, $locale = null) {
66 $trans = TranslatorManager::trans($id, $parameters, $domain, $locale);
67 return $this->twig->createTemplate($trans)->render($context);
68 }, ['needs_context' => true]);
69
70 $tc = new TwigFunction ('tc', function ($context, $id, array $choice, array $parameters = array(), $domain = null, $locale = null) {
71 $trans = TranslatorManager::transChoice($id, $choice, $parameters, $domain, $locale);
72 return $this->twig->createTemplate($trans)->render($context);
73 }, ['needs_context' => true]);
74 $this->twig->addFunction($t);
75 $this->twig->addFunction($tc);
76
77 $test = new TwigTest ('instanceOf', function ($var, $class) {
78 return $var instanceof $class;
79 });
80 $this->twig->addTest($test);
81 $this->twig->addGlobal('app', $this->fw);
82 }
83
84 public function addFunction(string $name, $callback, array $options = []): void {
85 $this->twig->addFunction(new TwigFunction ($name, $callback, $options));
86 }
87
88 protected function addFilter(string $name, $callback, array $options = []): void {
89 $this->twig->addFilter(new TwigFilter($name, $callback, $options));
90 }
91
92 protected function addExtension($extension): void {
93 $this->twig->addExtension($extension);
94 }
95
96 /*
97 * (non-PHPdoc)
98 * @see TemplateEngine::render()
99 */
100 public function render(string $viewName, ?array $pData = [], bool $asString = false) {
101 $pData ['config'] = Startup::getConfig();
102 EventsManager::trigger(ViewEvents::BEFORE_RENDER, $viewName, $pData);
103 $render = $this->twig->render($viewName, $pData);
104 EventsManager::trigger(ViewEvents::AFTER_RENDER, $render, $viewName, $pData);
105 if ($asString) {
106 return $render;
107 } else {
108 echo $render;
109 }
110 }
111
117 public function getBlockNames(string $templateName): array {
118 try {
119 $result = $this->twig->load($templateName)->getBlockNames();
120 } catch (\Error $e) {
121 $result = [];
122 }
123 return $result;
124 }
125
131 public function getCode(string $templateName): string {
132 return UFileSystem::load($this->twig->load($templateName)->getSourceContext()->getPath());
133 }
134
141 public function addPath(string $path, string $namespace) {
142 $this->loader->addPath($path, $namespace);
143 }
144
151 public function setPaths(array $paths, string $namespace) {
152 $this->loader->setPaths($paths, $namespace);
153 }
154
161 public function setTheme(string $theme, string $themeFolder = ThemesManager::THEMES_FOLDER): string {
162 $path = parent::setTheme($theme, $themeFolder);
163 $this->loader->setPaths([$path], 'activeTheme');
164 return $path;
165 }
166
173 public function exists(string $name): bool {
174 return $this->twig->getLoader()->exists($name);
175 }
176
177 public function getGenerator(): ?TemplateGenerator {
178 return null;
179 }
180
181}
Manager for caches (Router, Rest, models).
Starts the framework.
Definition Startup.php:19
View events constants.
Exceptions for ThemesManager.
File system utilities Ubiquity\utils\base$UFileSystem This class is part of Ubiquity.
Ubiquity abstract TemplateGenerator class.
Ubiquity Twig template engine.
Definition Twig.php:31
setTheme(string $theme, string $themeFolder=ThemesManager::THEMES_FOLDER)
Definition Twig.php:161
setPaths(array $paths, string $namespace)
Sets a path in a namespace.
Definition Twig.php:151
addPath(string $path, string $namespace)
Adds a new path in a namespace.
Definition Twig.php:141
getBlockNames(string $templateName)
{Returns the defined block names.}
Definition Twig.php:117
addFilter(string $name, $callback, array $options=[])
Definition Twig.php:88
getCode(string $templateName)
{Returns the source code of the template.}
Definition Twig.php:131
addFunction(string $name, $callback, array $options=[])
Definition Twig.php:84
render(string $viewName, ?array $pData=[], bool $asString=false)
Renders a view.
Definition Twig.php:100
exists(string $name)
Checks if we have the source code of a template, given its name.
Definition Twig.php:173