Skip to content

Commit

Permalink
Implemented a validate function to check the syntax of twig template …
Browse files Browse the repository at this point in the history
…file
  • Loading branch information
LionelLaffineur committed Feb 29, 2024
1 parent d8f7829 commit 26599d9
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions lib/Skeleton/Template/Twig/Twig.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,46 @@ public function render($template) {

return $this->twig->render($template, $this->variables);
}

/**
* Validate
*
* @access public
* @param string $template
* @param string &$error
* @return bool
*/
public function validate($template, &$error): bool {
$chain_loader = new \Twig\Loader\ChainLoader([
new \Twig\Loader\FilesystemLoader()
]);

$environment = new \Twig\Environment($chain_loader, [
'cache' => false,
'debug' => false,
'autoescape' => 'html',
'auto_reload' => false,
'strict_variables' => false,
]);

if (class_exists('\\Skeleton\\I18n\\Translation')) {
$environment->addExtension(new \Skeleton\I18n\Template\Twig\Extension\Tigron());
}
$environment->addExtension(new \Twig\Extension\StringLoaderExtension());
$environment->addExtension(new \Twig\Extra\Markdown\MarkdownExtension());
$environment->addExtension(new \Twig\Extra\Cache\CacheExtension());
$environment->addExtension(new \Twig\Extra\String\StringExtension());
$environment->addExtension(new \Skeleton\Template\Twig\Extension\Common());
foreach (\Skeleton\Template\Twig\Config::get_extensions() as $extension) {
$environment->addExtension(new $extension());
}

try {
$environment->createTemplate(file_get_contents($template), $template);
} catch (\Exception $e) {
$error = $e->getMessage();
return false;
}
return true;
}
}

0 comments on commit 26599d9

Please sign in to comment.