/**
* Twig initialization that sets the twig loader chain, then the environment, then extensions
* and also the base set of twig vars
*/
public function init()
{
if (!isset($this->twig)) {
/** @var Config $config */
$config = $this->grav['config'];
/** @var UniformResourceLocator $locator */
$locator = $this->grav['locator'];
$debugger = $this->grav['debugger'];
$this->twig_paths = $locator->findResources('theme://templates');
$this->grav->fireEvent('onTwigTemplatePaths');
$this->loader = new \Twig_Loader_Filesystem($this->twig_paths);
$this->loaderArray = new \Twig_Loader_Array(array());
$loader_chain = new \Twig_Loader_Chain(array($this->loaderArray, $this->loader));
$params = $config->get('system.twig');
if (!empty($params['cache'])) {
$params['cache'] = $locator->findResource('cache://twig', true, true);
}
$this->twig = new \Twig_Environment($loader_chain, $params);
if ($debugger->enabled() && $config->get('system.debugger.twig')) {
$this->twig = new \DebugBar\Bridge\Twig\TraceableTwigEnvironment($this->twig);
$collector = new \DebugBar\Bridge\Twig\TwigCollector($this->twig);
$debugger->addCollector($collector);
}
if ($config->get('system.twig.undefined_functions')) {
$this->twig->registerUndefinedFunctionCallback(function ($name) {
if (function_exists($name)) {
return new \Twig_Function_Function($name);
}
return new \Twig_Function_Function(function () {
});
});
}
if ($config->get('system.twig.undefined_filters')) {
$this->twig->registerUndefinedFilterCallback(function ($name) {
if (function_exists($name)) {
return new \Twig_Filter_Function($name);
}
return new \Twig_Filter_Function(function () {
});
});
}
$this->grav->fireEvent('onTwigInitialized');
// set default date format if set in config
if ($config->get('system.pages.dateformat.long')) {
$this->twig->getExtension('core')->setDateFormat($config->get('system.pages.dateformat.long'));
}
// enable the debug extension if required
if ($config->get('system.twig.debug')) {
$this->twig->addExtension(new \Twig_Extension_Debug());
}
$this->twig->addExtension(new TwigExtension());
$this->grav->fireEvent('onTwigExtensions');
// Set some standard variables for twig
$this->twig_vars = array('grav' => $this->grav, 'config' => $config, 'uri' => $this->grav['uri'], 'base_dir' => rtrim(ROOT_DIR, '/'), 'base_url' => $this->grav['base_url'], 'base_url_absolute' => $this->grav['base_url_absolute'], 'base_url_relative' => $this->grav['base_url_relative'], 'theme_dir' => $locator->findResource('theme://'), 'theme_url' => $this->grav['base_url'] . '/' . $locator->findResource('theme://', false), 'site' => $config->get('site'), 'assets' => $this->grav['assets'], 'taxonomy' => $this->grav['taxonomy'], 'browser' => $this->grav['browser']);
}
}