Skip to content

Commit

Permalink
registerCpTwigExtension(), registerSiteTwigExtension()
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonkelly committed Sep 23, 2024
1 parent f49ec16 commit 5ffd69f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
51 changes: 45 additions & 6 deletions src/web/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,16 @@ class View extends \yii\web\View
private array $_twigOptions;

/**
* @var ExtensionInterface[] List of Twig extensions registered with [[registerTwigExtension()]]
* @var array<class-string<ExtensionInterface>,ExtensionInterface>
* @see registerCpTwigExtension()
*/
private array $_twigExtensions = [];
private array $_cpTwigExtensions = [];

/**
* @var array<class-string<ExtensionInterface>,ExtensionInterface>
* @see registerSiteTwigExtension()
*/
private array $_siteTwigExtensions = [];

/**
* @var string[]
Expand Down Expand Up @@ -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);
}

Expand All @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit 5ffd69f

Please sign in to comment.