Cette traduction peut être obsolète.

Le linter Twig de Mago propose 25 règles réparties sur 8 catégories. Cliquez sur une règle pour afficher sa description, ses prérequis, sa configuration par défaut et ses exemples. Les codes des règles commencent par twig- ; les clés de configuration sous [twig.linter.rules] omettent ce préfixe.

Clarté

Règles qui rendent l'intention explicite et réduisent l'effort de lecture. Elles signalent des constructions techniquement valides mais qui obscurcissent ce que fait le code.

twig-no-nested-ternarywarning

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.


À éviter
{{ flag ? (other ? one : two) : no }}
À privilégier
{{ flag ? yes : no }}

OptionTypeDéfaut
enabledbooleanfalse
levelstring"warning"

Bonnes pratiques

Patterns Twig idiomatiques que recommandent la documentation et les conventions de codage de Twig.

twig-prefer-include-functionwarning

Prefers the include() function to the include tag.

No fix is offered: a rewrite must account for escaping, sandboxing, scope, and call order.


À éviter
{% include 'card.twig' %}
À privilégier
{{ include('card.twig') }}

OptionTypeDéfaut
enabledbooleanfalse
levelstring"warning"

Cohérence

Uniformité stylistique sur l'ensemble du code. Choisissez une façon de faire, ces règles aident tout le monde à s'y tenir.

twig-macro-argument-namewarning

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.


À éviter
{% macro input(className) %}{% endmacro %}
À privilégier
{% macro input(class_name) %}{% endmacro %}

OptionTypeDéfaut
enabledbooleanfalse
levelstring"warning"
twig-macro-namewarning

Enforces snake_case for macro names.

Non-ASCII names are left alone. No fix is offered, because renaming a macro breaks its callers.


À éviter
{% macro htmlInput() %}{% endmacro %}
À privilégier
{% macro html_input() %}{% endmacro %}

OptionTypeDéfaut
enabledbooleanfalse
levelstring"warning"
twig-named-argument-namewarning

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.


À éviter
{{ input(className: 'input') }}
À privilégier
{{ input(class_name: 'input') }}

OptionTypeDéfaut
enabledbooleanfalse
levelstring"warning"
twig-variable-namewarning

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.


À éviter
{% set firstName = user.name %}
À privilégier
{% set first_name = user.name %}

OptionTypeDéfaut
enabledbooleanfalse
levelstring"warning"
twig-end-block-namehelp

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.


À éviter
{% block title %}Title{% endblock %}
À privilégier
{% block title %}Title{% endblock title %}

OptionTypeDéfaut
enabledbooleanfalse
levelstring"help"
twig-named-argument-separatorhelp

Requires : between a named argument and its value, as the Twig coding standard recommends, instead of =.

Macro parameter defaults keep =.

Cette règle requiert une version minimale de Twig, [twig.linter] version, égale ou supérieure à 3.12.

À éviter
{{ range(low = 1, high = 3) }}
À privilégier
{{ range(low: 1, high: 3) }}

OptionTypeDéfaut
enabledbooleantrue
levelstring"help"
twig-single-quoteshelp

Prefers single quotes for plain strings: strings without escapes, apostrophes, #, or line breaks.

Other strings stay unchanged, because new quotes would change their meaning.


À éviter
{{ "title" }}
À privilégier
{{ 'title' }}

OptionTypeDéfaut
enabledbooleanfalse
levelstring"help"

Obsolescence

Fonctionnalités Twig obsolètes en amont, qu'une version ultérieure de Twig supprime. Migrez avant qu'elles ne cassent.

twig-deprecated-attributewarning

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.

Cette règle requiert une version minimale de Twig, [twig.linter] version, égale ou supérieure à 3.15.

À éviter
{{ attribute(object, name) }}
À privilégier
{{ object.(name) }}

OptionTypeDéfaut
enabledbooleantrue
levelstring"warning"
twig-deprecated-spacelesswarning

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.

Cette règle requiert une version minimale de Twig, [twig.linter] version, égale ou supérieure à 3.12.

À éviter
{{ text|spaceless }}
À privilégier
{{ text }}

OptionTypeDéfaut
enabledbooleantrue
levelstring"warning"
twig-duplicate-macrowarning

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.

Cette règle requiert une version minimale de Twig, [twig.linter] version, égale ou supérieure à 3.29.

À éviter
{% macro title() %}A{% endmacro %}
{% macro title() %}B{% endmacro %}
À privilégier
{% macro title() %}Title{% endmacro %}

OptionTypeDéfaut
enabledbooleantrue
levelstring"warning"

Maintenabilité

Du code difficile à maintenir dans la durée, trop complexe, trop emmêlé, trop fragile. Ces règles font remonter le coût tôt.

twig-explicit-include-contextwarning

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.


À éviter
{% include 'card.twig' %}
À privilégier
{% include 'card.twig' with {title: title} only %}

OptionTypeDéfaut
enabledbooleanfalse
levelstring"warning"
twig-no-include-ignore-missingwarning

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.


À éviter
{% include 'card.twig' ignore missing %}
À privilégier
{% include 'card.twig' %}

OptionTypeDéfaut
enabledbooleanfalse
levelstring"warning"

Redondance

Code mort, valeurs inutilisées, constructions sans effet observable. Les retirer garde le code honnête.

twig-constant-conditionwarning

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.


À éviter
{% if true %}yes{% endif %}
À privilégier
{% if flag %}yes{% endif %}

OptionTypeDéfaut
enabledbooleanfalse
levelstring"warning"

Sécurité

Sorties qui contournent l'échappement, sorties de débogage qui exposent le contexte du template, et fonctions, filtres ou tags interdits par votre projet.

twig-disallowed-filterswarning

Reports the filters listed in names, including in apply tags.

Names match exactly. An empty list reports nothing.


À éviter
{{ title|raw }}
À privilégier
{{ title }}

OptionTypeDéfaut
enabledbooleanfalse
levelstring"warning"
namesarray[]
twig-disallowed-functionswarning

Reports calls to the functions listed in names.

Names match exactly, including macros imported under those names. An empty list reports nothing.


À éviter
{{ source('secret.txt') }}
À privilégier
{{ title }}

OptionTypeDéfaut
enabledbooleanfalse
levelstring"warning"
namesarray[]
twig-disallowed-tagswarning

Reports the tags listed in names, including custom tags.

Names match exactly. An empty list reports nothing.


À éviter
{% flush %}
À privilégier
{{ title }}

OptionTypeDéfaut
enabledbooleanfalse
levelstring"warning"
namesarray[]
twig-no-autoescape-falsewarning

Disallows autoescape false blocks, which turn off escaping for their whole body.

No fix is offered, because Mago cannot tell which output needs escaping.


À éviter
{% autoescape false %}{{ value }}{% endautoescape %}
À privilégier
{% autoescape 'html' %}{{ value }}{% endautoescape %}

OptionTypeDéfaut
enabledbooleanfalse
levelstring"warning"
twig-no-dumpwarning

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.


À éviter
{{ dump(user) }}
À privilégier
{{ user.name }}

OptionTypeDéfaut
enabledbooleantrue
levelstring"warning"
twig-no-rawwarning

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.


À éviter
{{ user.name|raw }}
À privilégier
{{ user.name }}

OptionTypeDéfaut
enabledbooleanfalse
levelstring"warning"

Correction

Bugs et erreurs de logique. Les règles de cette catégorie attrapent du code qui fait probablement autre chose que ce que l'auteur voulait.

twig-duplicate-blockerror

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.


À éviter
{% block title %}A{% endblock %}
{% block title %}B{% endblock %}
À privilégier
{% block title %}Title{% endblock %}

OptionTypeDéfaut
enabledbooleantrue
levelstring"error"
twig-duplicate-parametererror

Reports macro parameter names that repeat an earlier parameter, including variadic parameters. Twig rejects such macros.


À éviter
{% macro input(name, name) %}{% endmacro %}
À privilégier
{% macro input(name, value) %}{% endmacro %}

OptionTypeDéfaut
enabledbooleantrue
levelstring"error"
twig-duplicate-argumentwarning

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.


À éviter
{{ range(low: 1, low: 3) }}
À privilégier
{{ range(low: 1, high: 3) }}

OptionTypeDéfaut
enabledbooleantrue
levelstring"warning"
twig-duplicate-keywarning

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.


À éviter
{{ {title: title, title: body} }}
À privilégier
{{ {title: title, body: body} }}

OptionTypeDéfaut
enabledbooleantrue
levelstring"warning"

↳ Modifier cette page →