/** * 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']); } }
/** * @param array $values */ public function __construct($values = array()) { $defaults['twig.options'] = array(); $defaults['twig.directories'] = array(); $defaults['twig.loader'] = function ($meadow) { // this needs to be lazy or theme switchers and alike explode it $stylesheet_dir = get_stylesheet_directory(); $template_dir = get_template_directory(); $calculated_dirs = array($stylesheet_dir, $template_dir, plugin_dir_path(__DIR__) . 'twig'); // enables explicit inheritance from parent theme in child if ($stylesheet_dir !== $template_dir) { $calculated_dirs[] = dirname($template_dir); } $directories = array_unique(array_merge($calculated_dirs, $meadow['twig.directories'])); return new \Twig_Loader_Filesystem($directories); }; $defaults['twig.undefined_function'] = array(__CLASS__, 'undefined_function'); $defaults['twig.undefined_filter'] = array(__CLASS__, 'undefined_filter'); $defaults['twig.environment'] = function ($meadow) { $environment = new \Twig_Environment($meadow['twig.loader'], $meadow['twig.options']); $meadow_extension = new Extension(); $environment->addExtension($meadow_extension); $environment->registerUndefinedFunctionCallback($meadow['twig.undefined_function']); $environment->registerUndefinedFilterCallback($meadow['twig.undefined_filter']); if (defined('WP_DEBUG') && WP_DEBUG) { $debug_extension = new \Twig_Extension_Debug(); $environment->addExtension($debug_extension); $environment->enableDebug(); } return $environment; }; $defaults['hierarchy'] = function () { return new Template_Hierarchy(); }; parent::__construct(array_merge($defaults, $values)); }
/** * 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']; /** @var Language $language */ $language = $this->grav['language']; $active_language = $language->getActive(); $language_append = ''; if ($language->getDefault() != $active_language || $config->get('system.languages.include_default_lang') === true) { $language_append = $active_language ? '/' . $active_language : ''; } // handle language templates if available if ($language->enabled()) { $lang_templates = $locator->findResource('theme://templates/' . ($active_language ? $active_language : $language->getDefault())); if ($lang_templates) { $this->twig_paths[] = $lang_templates; } } $this->twig_paths = array_merge($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([]); $loader_chain = new \Twig_Loader_Chain([$this->loaderArray, $this->loader]); $params = $config->get('system.twig'); if (!empty($params['cache'])) { $cachePath = $locator->findResource('cache://twig', true, true); $params['cache'] = new \Twig_Cache_Filesystem($cachePath, \Twig_Cache_Filesystem::FORCE_BYTECODE_INVALIDATION); } if (!empty($this->autoescape)) { $params['autoescape'] = $this->autoescape; } $this->twig = new TwigEnvironment($loader_chain, $params); 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 = $this->twig_vars + ['config' => $config, 'uri' => $this->grav['uri'], 'base_dir' => rtrim(ROOT_DIR, '/'), 'base_url' => $this->grav['base_url'] . $language_append, 'base_url_simple' => $this->grav['base_url'], 'base_url_absolute' => $this->grav['base_url_absolute'] . $language_append, 'base_url_relative' => $this->grav['base_url_relative'] . $language_append, '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']]; } }