Mago's Twig linter ships 25 rules across 8 categories. Click any rule to expand its description, requirements, default configuration, and examples. Rule codes start with twig-; the configuration keys under [twig.linter.rules] leave that prefix out.
Clarity
Rules that make intent explicit and reduce reader effort. They flag patterns that are technically valid but obscure what the code is doing.
twig-no-nested-ternary¶warning
Reports conditional expressions nested inside other conditional expressions. Split them into named values or branches.
Parentheses do not hide a nested conditional. A conditional in an arrow function body is not nested in the conditional around the arrow function.
{{ flag ? (other ? one : two) : no }}
{{ flag ? yes : no }}
| Option | Type | Default |
|---|---|---|
enabled | boolean | false |
level | string | "warning" |
Best practices
Idiomatic Twig patterns that the Twig documentation and coding standard recommend.
twig-prefer-include-function¶warning
Prefers the include() function to the include tag.
No fix is offered: a rewrite must account for escaping, sandboxing, scope, and call order.
{% include 'card.twig' %}
{{ include('card.twig') }}
| Option | Type | Default |
|---|---|---|
enabled | boolean | false |
level | string | "warning" |
Consistency
Stylistic uniformity across a codebase. Pick one way to do a thing, these rules help everyone stick to it.
twig-macro-argument-name¶warning
Enforces snake_case for macro parameter names.
Non-ASCII names are left alone. No fix is offered, because renaming a parameter breaks calls that pass it by name.
{% macro input(className) %}{% endmacro %}
{% macro input(class_name) %}{% endmacro %}
| Option | Type | Default |
|---|---|---|
enabled | boolean | false |
level | string | "warning" |
twig-macro-name¶warning
Enforces snake_case for macro names.
Non-ASCII names are left alone. No fix is offered, because renaming a macro breaks its callers.
{% macro htmlInput() %}{% endmacro %}
{% macro html_input() %}{% endmacro %}
| Option | Type | Default |
|---|---|---|
enabled | boolean | false |
level | string | "warning" |
twig-named-argument-name¶warning
Enforces snake_case for named arguments.
Enable this only when the functions, filters, and macros you call follow the Twig coding standard. Non-ASCII names are left alone.
{{ input(className: 'input') }}
{{ input(class_name: 'input') }}
| Option | Type | Default |
|---|---|---|
enabled | boolean | false |
level | string | "warning" |
twig-variable-name¶warning
Enforces snake_case for the names that set, for, import, from, arrow functions,
assignments, and destructuring bind.
Variables from the application, mapping keys, attribute names, and non-ASCII names are left alone.
{% set firstName = user.name %}
{% set first_name = user.name %}
| Option | Type | Default |
|---|---|---|
enabled | boolean | false |
level | string | "warning" |
twig-end-block-name¶help
Requires the block or macro name on endblock and endmacro tags.
Short blocks need no closing tag. The fix adds the name and leaves whitespace control intact.
{% block title %}Title{% endblock %}
{% block title %}Title{% endblock title %}
| Option | Type | Default |
|---|---|---|
enabled | boolean | false |
level | string | "help" |
twig-named-argument-separator¶help
Requires : between a named argument and its value, as the Twig coding standard recommends,
instead of =.
Macro parameter defaults keep =.
This rule needs the minimum Twig version,[twig.linter] version, to be3.12or newer.
{{ range(low = 1, high = 3) }}
{{ range(low: 1, high: 3) }}
| Option | Type | Default |
|---|---|---|
enabled | boolean | true |
level | string | "help" |
twig-single-quotes¶help
Prefers single quotes for plain strings: strings without escapes, apostrophes, #, or line
breaks.
Other strings stay unchanged, because new quotes would change their meaning.
{{ "title" }}
{{ 'title' }}
| Option | Type | Default |
|---|---|---|
enabled | boolean | false |
level | string | "help" |
Deprecation
Twig features deprecated upstream that a later Twig release removes. Migrate before they break.
twig-deprecated-attribute¶warning
Reports calls to the built-in attribute() function, deprecated since Twig 3.15.
Use dynamic attribute access, object.(name), after checking its call and lookup behavior. A
macro imported as attribute is left alone.
This rule needs the minimum Twig version,[twig.linter] version, to be3.15or newer.
{{ attribute(object, name) }}
{{ object.(name) }}
| Option | Type | Default |
|---|---|---|
enabled | boolean | true |
level | string | "warning" |
twig-deprecated-spaceless¶warning
Reports the spaceless filter, deprecated since Twig 3.12, including in apply tags.
No fix is offered: check the rendered output before you stop stripping whitespace.
This rule needs the minimum Twig version,[twig.linter] version, to be3.12or newer.
{{ text|spaceless }}
{{ text }}
| Option | Type | Default |
|---|---|---|
enabled | boolean | true |
level | string | "warning" |
twig-duplicate-macro¶warning
Reports macro names repeated in one template. Names are case-sensitive.
Twig 3 uses the last definition; Twig 3.29 deprecates this and Twig 4 rejects it. An embed body
has its own macros.
This rule needs the minimum Twig version,[twig.linter] version, to be3.29or newer.
{% macro title() %}A{% endmacro %}
{% macro title() %}B{% endmacro %}
{% macro title() %}Title{% endmacro %}
| Option | Type | Default |
|---|---|---|
enabled | boolean | true |
level | string | "warning" |
Maintainability
Code that is hard to keep alive over time, too complex, too tangled, too fragile. These rules surface the cost early.
twig-explicit-include-context¶warning
Requires only on include and embed tags, and a literal with_context: false in calls to
the built-in include() function, so templates do not share the whole context by accident.
A macro imported as include is left alone. No fix is offered, because it would change the
variables the included template can see.
{% include 'card.twig' %}
{% include 'card.twig' with {title: title} only %}
| Option | Type | Default |
|---|---|---|
enabled | boolean | false |
level | string | "warning" |
twig-no-include-ignore-missing¶warning
Disallows ignore missing on include and embed tags, and a literal ignore_missing: true
in calls to the built-in include() function.
Dynamic values are left alone.
{% include 'card.twig' ignore missing %}
{% include 'card.twig' %}
| Option | Type | Default |
|---|---|---|
enabled | boolean | false |
level | string | "warning" |
Redundancy
Dead code, unused values, and constructs that have no observable effect. Trimming them keeps the codebase honest.
twig-constant-condition¶warning
Reports literal boolean conditions in if branches and conditional expressions.
The rule does not guess the truthiness of other values. No fix is offered, because removing a branch removes rendered text.
{% if true %}yes{% endif %}
{% if flag %}yes{% endif %}
| Option | Type | Default |
|---|---|---|
enabled | boolean | false |
level | string | "warning" |
Security
Output that bypasses escaping, debug output that exposes the template context, and functions, filters, or tags your project bans.
twig-disallowed-filters¶warning
Reports the filters listed in names, including in apply tags.
Names match exactly. An empty list reports nothing.
{{ title|raw }}
{{ title }}
| Option | Type | Default |
|---|---|---|
enabled | boolean | false |
level | string | "warning" |
names | array | [] |
twig-disallowed-functions¶warning
Reports calls to the functions listed in names.
Names match exactly, including macros imported under those names. An empty list reports nothing.
{{ source('secret.txt') }}
{{ title }}
| Option | Type | Default |
|---|---|---|
enabled | boolean | false |
level | string | "warning" |
names | array | [] |
twig-disallowed-tags¶warning
Reports the tags listed in names, including custom tags.
Names match exactly. An empty list reports nothing.
{% flush %}
{{ title }}
| Option | Type | Default |
|---|---|---|
enabled | boolean | false |
level | string | "warning" |
names | array | [] |
twig-no-autoescape-false¶warning
Disallows autoescape false blocks, which turn off escaping for their whole body.
No fix is offered, because Mago cannot tell which output needs escaping.
{% autoescape false %}{{ value }}{% endautoescape %}
{% autoescape 'html' %}{{ value }}{% endautoescape %}
| Option | Type | Default |
|---|---|---|
enabled | boolean | false |
level | string | "warning" |
twig-no-dump¶warning
Reports calls to the built-in dump() function. Debug output can expose the template context.
A macro imported as dump is left alone. No fix is offered: the calls and their arguments stay
in place.
{{ dump(user) }}
{{ user.name }}
| Option | Type | Default |
|---|---|---|
enabled | boolean | true |
level | string | "warning" |
twig-no-raw¶warning
Disallows the raw filter, including in apply tags. Raw output can be deliberate, so this rule
is off by default.
No fix is offered, because removing the filter can change escaping or token boundaries.
{{ user.name|raw }}
{{ user.name }}
| Option | Type | Default |
|---|---|---|
enabled | boolean | false |
level | string | "warning" |
Correctness
Bugs and logic errors. Rules in this category catch code that does something the author probably did not intend.
twig-duplicate-block¶error
Reports block names repeated in one template. Twig rejects such templates.
An embed body has its own blocks, so it may reuse a name from the enclosing template.
{% block title %}A{% endblock %}
{% block title %}B{% endblock %}
{% block title %}Title{% endblock %}
| Option | Type | Default |
|---|---|---|
enabled | boolean | true |
level | string | "error" |
twig-duplicate-parameter¶error
Reports macro parameter names that repeat an earlier parameter, including variadic parameters. Twig rejects such macros.
{% macro input(name, name) %}{% endmacro %}
{% macro input(name, value) %}{% endmacro %}
| Option | Type | Default |
|---|---|---|
enabled | boolean | true |
level | string | "error" |
twig-duplicate-argument¶warning
Reports named arguments that repeat a name in one call, filter, or test.
Twig ignores underscores and case in the argument names of its own functions, filters, and
tests, so first_name and firstName name the same argument there. Calls to macros and
methods compare names exactly.
{{ range(low: 1, low: 3) }}
{{ range(low: 1, high: 3) }}
| Option | Type | Default |
|---|---|---|
enabled | boolean | true |
level | string | "warning" |
twig-duplicate-key¶warning
Reports literal mapping keys that repeat an earlier key after PHP's array-key conversion, so
'1' and 1 are the same key. The last value replaces the first.
Dynamic keys are left alone. No fix is offered, because both values can have side effects.
{{ {title: title, title: body} }}
{{ {title: title, body: body} }}
| Option | Type | Default |
|---|---|---|
enabled | boolean | true |
level | string | "warning" |