6use Twig\Loader\FilesystemLoader;
36 $loader =
new FilesystemLoader (\ROOT . \DS .
'views' . \DS);
37 $loader->addPath(Startup::getFrameworkDir() .
'/../core/views/engines/twig',
'framework');
40 if (($options [
'cache'] ??
false) ===
true) {
41 $options [
'cache'] = CacheManager::getCacheSubDirectory(
'views');
44 $this->twig =
new Environment (
$loader, $options);
46 if (isset($options[
'extensions'])) {
47 foreach ($options[
'extensions'] as $ext) {
48 $this->twig->addExtension(
new $ext());
52 if (isset ($options [
'activeTheme'])) {
53 ThemesManager::setActiveThemeFromTwig($options [
'activeTheme']);
54 $this->
setTheme($options [
'activeTheme'], ThemesManager::THEMES_FOLDER);
55 unset ($options [
'activeTheme']);
57 $this->loader->setPaths([\ROOT . \DS .
'views'],
'activeTheme');
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]);
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);
77 $test =
new TwigTest (
'instanceOf',
function ($var, $class) {
78 return $var instanceof $class;
80 $this->twig->addTest($test);
81 $this->twig->addGlobal(
'app', $this->fw);
84 public function addFunction(
string $name, $callback, array $options = []): void {
85 $this->twig->addFunction(new TwigFunction ($name, $callback, $options));
88 protected function addFilter(
string $name, $callback, array $options = []): void {
89 $this->twig->addFilter(new TwigFilter($name, $callback, $options));
93 $this->twig->addExtension($extension);
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);
119 $result = $this->twig->load($templateName)->getBlockNames();
120 }
catch (\Error $e) {
131 public function getCode(
string $templateName): string {
132 return
UFileSystem::load($this->twig->load($templateName)->getSourceContext()->getPath());
141 public function addPath(
string $path,
string $namespace) {
142 $this->loader->addPath($path, $namespace);
151 public function setPaths(array $paths,
string $namespace) {
152 $this->loader->setPaths($paths, $namespace);
161 public function setTheme(
string $theme,
string $themeFolder = ThemesManager::THEMES_FOLDER): string {
162 $path = parent::setTheme($theme, $themeFolder);
163 $this->loader->setPaths([$path],
'activeTheme');
173 public function exists(
string $name): bool {
174 return $this->twig->getLoader()->exists($name);
Manager for caches (Router, Rest, models).
Exceptions for ThemesManager.
File system utilities Ubiquity\utils\base$UFileSystem This class is part of Ubiquity.
Abstract template engine.
Ubiquity abstract TemplateGenerator class.
Ubiquity Twig template engine.
setTheme(string $theme, string $themeFolder=ThemesManager::THEMES_FOLDER)
setPaths(array $paths, string $namespace)
Sets a path in a namespace.
addPath(string $path, string $namespace)
Adds a new path in a namespace.
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.
exists(string $name)
Checks if we have the source code of a template, given its name.