diff --git a/CHANGELOG.md b/CHANGELOG.md index 3aa0d4eb4..5efa8e180 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ 1. [](#new) * Added a new `cache_control` system and page level property [#1591](https://github.com/getgrav/grav/issues/1591) * Added a new `clear_images_by_default` system property to stop cache clear events from removing processed images [#1481](https://github.com/getgrav/grav/pull/1481) + * Added new `onTwigLoader()` event to enable utilization of loader methods + * Added new `Twig::addPath()` and `Twig::prependPath()` methods to wrap loader methods and support namespacing [#1604](https://github.com/getgrav/grav/issues/1604) 1. [](#bugfix) * Allow `session.timetout` field to be set to `0` via blueprints [#1598](https://github.com/getgrav/grav/issues/1598) diff --git a/system/src/Grav/Common/Twig/Twig.php b/system/src/Grav/Common/Twig/Twig.php index 60f4e4770..fff6b6130 100644 --- a/system/src/Grav/Common/Twig/Twig.php +++ b/system/src/Grav/Common/Twig/Twig.php @@ -98,6 +98,9 @@ class Twig $this->grav->fireEvent('onTwigTemplatePaths'); $this->loader = new \Twig_Loader_Filesystem($this->twig_paths); + + $this->grav->fireEvent('onTwigLoader'); + $this->loaderArray = new \Twig_Loader_Array([]); $loader_chain = new \Twig_Loader_Chain([$this->loaderArray, $this->loader]); @@ -359,6 +362,26 @@ class Twig return $output; } + /** + * Wraps the Twig_Loader_Filesystem addPath method (should be used only in `onTwigLoader()` event + * @param $template_path + * @param null $namespace + */ + public function addPath($template_path, $namespace = null) + { + $this->loader->addPath($template_path, $namespace); + } + + /** + * Wraps the Twig_Loader_Filesystem prependPath method (should be used only in `onTwigLoader()` event + * @param $template_path + * @param null $namespace + */ + public function prependPath($template_path, $namespace = null) + { + $this->loader->prependPath($template_path, $namespace); + } + /** * Simple helper method to get the twig template if it has already been set, else return * the one being passed in