.lex file. Use it whenever you render templates that were written or modified by end users — for example, email newsletter editors, report builders, or any CMS feature that lets users author their own markup.
Without sandbox mode, a template author can call arbitrary PHP functions, use raw echo, or embed #php blocks. Sandbox mode removes those capabilities so you can safely compile and render user-supplied content.
Enabling sandbox mode
CallenableSandbox() on your Lexer instance. You can optionally pass a SandboxConfig object to control exactly what is restricted.
SandboxConfig:
Configuration presets
SandboxConfig ships with two factory methods covering the most common needs.
- SandboxConfig::permissive()
- SandboxConfig::secure()
Starts with no restrictions except the always-blocked functions (see below). Raw echo is allowed, custom directives are allowed, and all PHP functions are callable.Use this preset when you want to track the always-blocked list automatically without adding any additional restrictions yourself.
| Setting | Value |
|---|---|
Raw echo {!! !!} | Allowed |
| PHP function calls | All allowed (except always-blocked) |
| Custom directives | Allowed |
#php blocks | Allowed |
Fluent modifiers
SandboxConfig is immutable. Each modifier returns a new instance.
withAllowedFunctions(array $fns): static
Provides an explicit whitelist of PHP functions that template expressions may call. Any function not in this list throws a TemplateSyntaxException at compile time.
null means all functions are allowed (subject to the always-blocked list). Passing an empty array [] means no function calls are permitted at all.
withRawEcho(bool $allow): static
Controls whether {!! $expr !!} raw output is permitted in the template. In SandboxConfig::secure() this defaults to false.
withAllowedDirectives(array $directives): static
Whitelists specific custom directive names when allowCustomDirectives is false. This lets you allow selected trusted directives while still blocking all others.
withCustomDirectives(bool $allow): static
Toggles whether any custom directive registered via $lexer->directive() may be used in the sandboxed template.
Always-blocked functions
The following functions are always forbidden, regardless of anySandboxConfig setting or whitelist. They are blocked at the ExpressionValidator level and cannot be re-enabled:
`) is also always blocked.
The always-blocked list is enforced whenever sandbox mode is active.
SandboxConfig::permissive() still blocks these functions — it only removes the additional restrictions on raw echo, custom directives, and function whitelisting. The always-blocked list cannot be overridden by any sandbox preset.What sandbox mode restricts
| Feature | Permissive | Secure |
|---|---|---|
#php / #endphp blocks | Allowed | Blocked |
Raw echo {!! !!} | Allowed | Blocked |
| Arbitrary PHP function calls | Allowed | Blocked (unless whitelisted) |
new keyword in expressions | Allowed | Blocked |
| Custom directives | Allowed | Blocked |
| Always-blocked functions | Blocked | Blocked |
| Backtick operator | Blocked | Blocked |
Example: user-submitted newsletter templates
The following shows a realistic setup for a newsletter editor where users can personalise their own email templates.
A user-authored template like the following compiles and renders safely:
TemplateSyntaxException at compile time — before any output is produced: