twigphp/Twig · error · SyntaxError

Expected endmacro for macro

Error message

Expected endmacro for macro "%s" (but "%s" given).

What it means

Thrown by MacroTokenParser::parse when a {% macro %} is closed with {% endmacro name %} whose name does not match the opening macro's name. Like endblock, Twig validates the optional label after endmacro and raises a SyntaxError on mismatch, pointing at the current line.

Solutions

  1. Make the endmacro name match the macro name.
  2. Drop the name entirely and just use {% endmacro %}.
  3. Verify nesting — the mismatched endmacro may belong to a different macro scope.

Example fix

{% macro input(name) %}...{% endmacro textbox %}

{# after #}
{% macro input(name) %}...{% endmacro %}
Defensive patterns

Strategy: validation

Try / catch

try {
    $twig->parse($twig->tokenize(new \Twig\Source($code, $name)));
} catch (\Twig\Error\SyntaxError $e) {
    if (str_contains($e->getMessage(), 'Expected endmacro')) {
        // align endmacro label with the macro name
    }
    throw $e;
}

Prevention

When it happens

Trigger: Writing {% macro input(field) %}...{% endmacro textbox %} — the NAME token after endmacro differs from the macro's declared name (loose != comparison).

Common situations: Copy-pasting macros and forgetting to rename endmacro; renaming the macro declaration but not the closing tag; nesting confusion inside {% import %}-heavy template libraries.

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/9a4963ac73331eaf. Report an issue: GitHub.

Appendix: source

Thrown at src/TokenParser/MacroTokenParser.php:53

 * @internal
 */
final class MacroTokenParser extends AbstractTokenParser
{
    public function parse(Token $token): Node
    {
        $lineno = $token->getLine();
        $stream = $this->parser->getStream();
        $name = $stream->expect(Token::NAME_TYPE)->getValue();
        [$arguments, $variadicName] = $this->parseDefinition($name);

        $stream->expect(Token::BLOCK_END_TYPE);
        $this->parser->pushLocalScope();
        $body = $this->parser->subparse([$this, 'decideBlockEnd'], true);
        if ($token = $stream->nextIf(Token::NAME_TYPE)) {
            $value = $token->getValue();

            if ($value != $name) {
                throw new SyntaxError(\sprintf('Expected endmacro for macro "%s" (but "%s" given).', $name, $value), $stream->getCurrent()->getLine(), $stream->getSourceContext());
            }
        }
        $this->parser->popLocalScope();
        $stream->expect(Token::BLOCK_END_TYPE);

        $this->parser->setMacro($name, $macro = new MacroNode($name, new BodyNode([$body]), $arguments, $lineno, $variadicName));
        $this->parser->setDocumentationTarget($macro);

        return new MacroDeclarationNode($name, $lineno);
    }

    public function decideBlockEnd(Token $token): bool
    {
        return $token->test('endmacro');
    }

    public function getTag(): string
    {

View on GitHub (pinned to a414c3a491)