twigphp/Twig · error · SyntaxError

The "output_strategy" argument of the "render_sandboxed"…

Error message

The "output_strategy" argument of the "render_sandboxed" function must be a non-empty literal string other than "all".

What it means

Twig's render_sandboxed() function compiles its output_strategy argument at compile time, so it must be a literal string constant known when the template is compiled. Twig rejects non-constant expressions and any string that is empty or 'all' (meaningless here because render_sandboxed renders a single included template, not all).

Solutions

  1. Pass a literal string strategy such as 'js' or an allowed strategy name directly in the template
  2. Remove output_strategy entirely if you want default behavior
  3. Check allowed strategy names in the sandbox extension and use one verbatim

Example fix

// before
{% render_sandboxed with { output_strategy: strategyVar } %}
// after
{% render_sandboxed with { output_strategy: 'js' } %}
Defensive patterns

Strategy: validation

Validate before calling

$allowed = ['js','css','html'];
if (!is_string($strategy) || '' === $strategy || 'all' === $strategy || !in_array($strategy, $allowed, true)) {
    throw new InvalidArgumentException('output_strategy must be a literal non-empty strategy name');
}

Type guard

function isValidOutputStrategy(mixed $s): bool {
    return is_string($s) && '' !== $s && 'all' !== $s;
}

Try / catch

try {
    $twig->load('page.html');
} catch (\Twig\Error\SyntaxError $e) {
    // inspect $e->getMessage() for output_strategy problems
}

Prevention

When it happens

Trigger: Calling {% render_sandboxed with {output_strategy: someVar} %} where the value is a variable/expression, an empty string, or the literal 'all'.

Common situations: Passing the strategy from config or a template variable instead of a literal; copying code that used the old 'all' default from an older Twig sandboxing example.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of twigphp/Twig@a414c3a491 (2026-09-13). Data as JSON: /api/errors/67cf17dbad1c2b9a. Report an issue: GitHub.

Appendix: source

Thrown at src/Node/Expression/FunctionNode/RenderSandboxedFunction.php:35

use Twig\Node\Expression\FunctionExpression;
use Twig\Node\Node;

/**
 * @internal
 */
final class RenderSandboxedFunction extends FunctionExpression
{
    public function compile(Compiler $compiler): void
    {
        $arguments = $this->getNode('arguments');
        $outputStrategyName = self::getOutputStrategyName($arguments);
        if (null === $outputStrategyName) {
            throw new SyntaxError('Value for argument "output_strategy" is required for function "render_sandboxed".', $this->getTemplateLine(), $this->getSourceContext());
        }

        $outputStrategy = $arguments->getNode($outputStrategyName);
        if (!$outputStrategy instanceof ConstantExpression) {
            throw new SyntaxError('The "output_strategy" argument of the "render_sandboxed" function must be a non-empty literal string other than "all".', $outputStrategy->getTemplateLine(), $outputStrategy->getSourceContext());
        }

        $strategy = $outputStrategy->getAttribute('value');
        if (!\is_string($strategy) || '' === $strategy || 'all' === $strategy) {
            throw new SyntaxError('The "output_strategy" argument of the "render_sandboxed" function must be a non-empty literal string other than "all".', $outputStrategy->getTemplateLine(), $outputStrategy->getSourceContext());
        }

        $runtimeArguments = clone $arguments;
        $runtimeArguments->removeNode($outputStrategyName);
        $this->setNode('arguments', $runtimeArguments);
        try {
            parent::compile($compiler);
        } finally {
            $this->setNode('arguments', $arguments);
        }
    }

    /**

View on GitHub (pinned to a414c3a491)