Skip to content

Commit 14b552f

Browse files
committed
Merge branch 'release/4.0.5' into v4
2 parents 098822b + ca1bcfd commit 14b552f

File tree

4 files changed

+38
-1
lines changed

4 files changed

+38
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
All notable changes to this project will be documented in this file.
44

5+
## 4.0.5 - 2028.08.18
6+
### Added
7+
* Provide a mechanism for adding Twig Extensions in bulk to the `SandboxView` ([#1632](https://github.com/nystudio107/craft-seomatic/issues/1632))
8+
59
## 4.0.4 - 2025.06.10
610
### Fixed
711
* Remove errant dependency on SEOmatic in the `SecurityPolicy` helper class

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,28 @@ This will cause it to create the sandbox from the `seomatic-sandbox.php` file in
260260

261261
Craft automatically creates a namespaced alias for each plugin.
262262

263+
### Adding TwigExtensions
264+
265+
By default, the Twig `SandboxView` will only have the Twig extensions in it that Craft itself registers. Any Twig extensions that are added by plugins or modules will not be present.
266+
267+
This is because there is no "Register Twig Extensions" event sent by Craft that plugins are modules can listen for, so the `SandboxView` has no way to tell each plugin or module to register their Twig extensions in the Twig `SandboxView`.
268+
269+
Instead, if you have Twig extensions that you want added to your Twig `SandboxView`, you can either do it manually:
270+
271+
```php
272+
$sandboxView = new SandboxView();
273+
$sandboxView->registerTwigExtension(new MyTwigExtension());
274+
```
275+
276+
...or you can pass in an array of class names to the constructor, and have the Twig `SandboxView` instantiate the Twig extensions for you:
277+
278+
```php
279+
$sandboxView = new SandboxView(['twigExtensionClasses' => [
280+
MyTwigExtension::class,
281+
AnotherTwigExtension::class,
282+
]]);
283+
```
284+
263285
### Custom SecurityPolicy
264286

265287
You can also create your own custom `SecurityPolicy` to use, it just needs to conform to the Twig [`SecurityPolicyInterface`](https://github.com/twigphp/Twig/blob/3.x/src/Sandbox/SecurityPolicyInterface.php):

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "nystudio107/craft-twig-sandbox",
33
"description": "Allows you to easily create a sandboxed Twig environment where you can control what tags, filters, functions, and object methods/properties are allowed",
4-
"version": "4.0.4",
4+
"version": "4.0.5",
55
"keywords": [
66
"craft",
77
"cms",

src/web/SandboxView.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ class SandboxView extends View
2525
*/
2626
public WebSandboxErrorHandler|ConsoleSandboxErrorHandler|null $sandboxErrorHandler = null;
2727

28+
/**
29+
* @var class-string[] Array of TwigExtension classes to instantiate and add to the SandboxView
30+
*/
31+
public array $twigExtensionClasses = [];
32+
2833
// Public Methods
2934
// =========================================================================
3035

@@ -39,5 +44,11 @@ public function init(): void
3944
$this->securityPolicy = $this->securityPolicy ?? new BlacklistSecurityPolicy();
4045
// Add the SandboxExtension with our SecurityPolicy lazily via ::registerTwigExtension()
4146
$this->registerTwigExtension(new SandboxExtension($this->securityPolicy, true));
47+
// Register the additional TwigExtension classes
48+
foreach ($this->twigExtensionClasses as $className) {
49+
if (class_exists($className)) {
50+
$this->registerTwigExtension(new $className());
51+
}
52+
}
4253
}
4354
}

0 commit comments

Comments
 (0)