diff --git a/CHANGELOG-WIP.md b/CHANGELOG-WIP.md index eee31bd9007..f9d471ef269 100644 --- a/CHANGELOG-WIP.md +++ b/CHANGELOG-WIP.md @@ -5,4 +5,6 @@ - Added `craft\filters\BasicHttpAuthLogin`. ([#15720](https://github.com/craftcms/cms/pull/15720)) - Added `craft\filters\BasicHttpAuthStatic`. ([#15720](https://github.com/craftcms/cms/pull/15720)) - Added `craft\filters\SiteFilterTrait::$enabled`. ([#15720](https://github.com/craftcms/cms/pull/15720)) +- Added `craft\web\View::registerCpTwigExtension()`. +- Added `craft\web\View::registerSiteTwigExtension()`. - Deprecated the `enableBasicHttpAuth` config setting. `craft\filters\BasicHttpAuthLogin` should be used instead. ([#15720](https://github.com/craftcms/cms/pull/15720)) diff --git a/src/web/View.php b/src/web/View.php index 85110fe5f4a..610f46f0feb 100644 --- a/src/web/View.php +++ b/src/web/View.php @@ -155,9 +155,16 @@ class View extends \yii\web\View private array $_twigOptions; /** - * @var ExtensionInterface[] List of Twig extensions registered with [[registerTwigExtension()]] + * @var array,ExtensionInterface> + * @see registerCpTwigExtension() */ - private array $_twigExtensions = []; + private array $_cpTwigExtensions = []; + + /** + * @var array,ExtensionInterface> + * @see registerSiteTwigExtension() + */ + private array $_siteTwigExtensions = []; /** * @var string[] @@ -398,7 +405,10 @@ public function createTwig(): Environment } // Add plugin-supplied extensions - foreach ($this->_twigExtensions as $extension) { + $registeredExtensions = $this->_templateMode === self::TEMPLATE_MODE_CP + ? $this->_cpTwigExtensions + : $this->_siteTwigExtensions; + foreach ($registeredExtensions as $extension) { $twig->addExtension($extension); } @@ -419,19 +429,31 @@ public function createTwig(): Environment } /** - * Registers a new Twig extension, which will be added on existing environments and queued up for future environments. + * Registers a new Twig extension both CP and site templates. * * @param ExtensionInterface $extension */ public function registerTwigExtension(ExtensionInterface $extension): void + { + $this->registerCpTwigExtension($extension); + $this->registerSiteTwigExtension($extension); + } + + /** + * Registers a new Twig extension for CP templates. + * + * @param ExtensionInterface $extension + * @since 4.13.0 + */ + public function registerCpTwigExtension(ExtensionInterface $extension): void { // Make sure this extension isn't already registered $class = get_class($extension); - if (isset($this->_twigExtensions[$class])) { + if (isset($this->_cpTwigExtensions[$class])) { return; } - $this->_twigExtensions[$class] = $extension; + $this->_cpTwigExtensions[$class] = $extension; if (isset($this->_cpTwig)) { try { @@ -440,6 +462,23 @@ public function registerTwigExtension(ExtensionInterface $extension): void $this->_cpTwig = null; } } + } + + /** + * Registers a new Twig extension for site templates. + * + * @param ExtensionInterface $extension + * @since 4.13.0 + */ + public function registerSiteTwigExtension(ExtensionInterface $extension): void + { + // Make sure this extension isn't already registered + $class = get_class($extension); + if (isset($this->_siteTwigExtensions[$class])) { + return; + } + + $this->_siteTwigExtensions[$class] = $extension; if (isset($this->_siteTwig)) { try {