twigphp/Twig · error · Twig\Error\SyntaxError

The " " modifier takes exactly one argument (2 given).

Error message

The "%s" modifier takes exactly one argument (2 given).

What it means

This Twig SyntaxError is thrown when a {% cache %} modifier ('ttl' or 'tags') receives more than one argument. After parsing the first expression, if the parser sees a ',' it knows a second argument was given and reports '2 given' (the tag supports exactly one).

Solutions

  1. Pass a single argument per modifier; combine values into one expression if needed.
  2. For multiple tags, pass one array: tags(['a','b']) instead of tags(['a'],['b']).
  3. Split distinct options across the two supported modifiers ttl and tags.

Example fix

// before
{% cache tags(['a'], ['b']) %}...{% endcache %}
// after
{% cache tags(['a','b']) %}...{% endcache %}
Defensive patterns

Strategy: validation

Validate before calling

if (preg_match('/\{\%\s*cache\s+(ttl|tags)\s*\([^,)]+,/', $template)) {
    throw new InvalidArgumentException('cache ttl/tags modifiers accept exactly one argument');
}

Try / catch

try {
    $twig->render($template);
} catch (\Twig\Error\SyntaxError $e) {
    if (str_contains($e->getMessage(), '(2 given)')) {
        // combine arguments into a single expression/array
    }
    throw $e;
}

Prevention

When it happens

Trigger: Template like {% cache ttl(60, 120) %} or {% cache tags(['a'], ['b']) %} — comma-separated arguments inside a modifier.

Common situations: Developers assuming ttl accepts (value, unit) or tags accepts multiple list arguments, mimicking other template engines' syntax.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


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

Appendix: source

Thrown at extra/cache-extra/TokenParser/CacheTokenParser.php:45

        $key = $this->parser->parseExpression();

        $ttl = null;
        $tags = null;
        while ($stream->test(Token::NAME_TYPE)) {
            $k = $stream->getCurrent()->getValue();
            if (!\in_array($k, ['ttl', 'tags'], true)) {
                throw new SyntaxError(\sprintf('Unknown "%s" configuration.', $k), $stream->getCurrent()->getLine(), $stream->getSourceContext());
            }

            $stream->next();
            $stream->expect(Token::OPERATOR_TYPE, '(');
            $line = $stream->getCurrent()->getLine();
            if ($stream->test(Token::PUNCTUATION_TYPE, ')')) {
                throw new SyntaxError(\sprintf('The "%s" modifier takes exactly one argument (0 given).', $k), $line, $stream->getSourceContext());
            }
            $arg = $this->parser->parseExpression();
            if ($stream->test(Token::PUNCTUATION_TYPE, ',')) {
                throw new SyntaxError(\sprintf('The "%s" modifier takes exactly one argument (2 given).', $k), $line, $stream->getSourceContext());
            }
            $stream->expect(Token::PUNCTUATION_TYPE, ')');

            if ('ttl' === $k) {
                $ttl = $arg;
            } elseif ('tags' === $k) {
                $tags = $arg;
            }
        }

        $stream->expect(Token::BLOCK_END_TYPE);
        $body = $this->parser->subparse([$this, 'decideCacheEnd'], true);
        $stream->expect(Token::BLOCK_END_TYPE);

        $body = new CacheNode($key, $ttl, $tags, $body, $token->getLine());

        return new PrintNode(new RawFilter($body), $token->getLine());
    }

View on GitHub (pinned to a414c3a491)