此翻译可能已过时。

Mago 的 Twig linter 共提供 25 条规则,分布在 8 个类别中。点击任意规则可展开其描述、要求、默认配置和示例。规则代码以 twig- 开头;[twig.linter.rules] 下的配置键省略该前缀。

清晰度

让意图更明确、减少阅读负担的规则。它们指出在技术上合法但会模糊代码意图的写法。

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.


避免
{{ flag ? (other ? one : two) : no }}
推荐
{{ flag ? yes : no }}

选项类型默认值
enabledbooleanfalse
levelstring"warning"

最佳实践

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.


避免
{% include 'card.twig' %}
推荐
{{ include('card.twig') }}

选项类型默认值
enabledbooleanfalse
levelstring"warning"

一致性

保持整套代码风格一致。选定一种写法,这些规则帮助所有人坚持下去。

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.


避免
{% macro input(className) %}{% endmacro %}
推荐
{% macro input(class_name) %}{% endmacro %}

选项类型默认值
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.


避免
{% macro htmlInput() %}{% endmacro %}
推荐
{% macro html_input() %}{% endmacro %}

选项类型默认值
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.


避免
{{ input(className: 'input') }}
推荐
{{ input(class_name: 'input') }}

选项类型默认值
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.


避免
{% set firstName = user.name %}
推荐
{% set first_name = user.name %}

选项类型默认值
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.


避免
{% block title %}Title{% endblock %}
推荐
{% block title %}Title{% endblock title %}

选项类型默认值
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 =.

此规则要求最低 Twig 版本 [twig.linter] version 为 3.12 或更高。

避免
{{ range(low = 1, high = 3) }}
推荐
{{ range(low: 1, high: 3) }}

选项类型默认值
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.


避免
{{ "title" }}
推荐
{{ 'title' }}

选项类型默认值
enabledbooleanfalse
levelstring"help"

弃用

上游已弃用、后续 Twig 版本会移除的 Twig 特性。请在它们失效前迁移。

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.

此规则要求最低 Twig 版本 [twig.linter] version 为 3.15 或更高。

避免
{{ attribute(object, name) }}
推荐
{{ object.(name) }}

选项类型默认值
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.

此规则要求最低 Twig 版本 [twig.linter] version 为 3.12 或更高。

避免
{{ text|spaceless }}
推荐
{{ text }}

选项类型默认值
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.

此规则要求最低 Twig 版本 [twig.linter] version 为 3.29 或更高。

避免
{% macro title() %}A{% endmacro %}
{% macro title() %}B{% endmacro %}
推荐
{% macro title() %}Title{% endmacro %}

选项类型默认值
enabledbooleantrue
levelstring"warning"

可维护性

难以长期维护的代码,过于复杂、纠缠或脆弱。这些规则提前暴露成本。

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.


避免
{% include 'card.twig' %}
推荐
{% include 'card.twig' with {title: title} only %}

选项类型默认值
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.


避免
{% include 'card.twig' ignore missing %}
推荐
{% include 'card.twig' %}

选项类型默认值
enabledbooleanfalse
levelstring"warning"

冗余

无用代码、无效值,以及没有可观察效果的写法。清理它们让代码更诚实。

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.


避免
{% if true %}yes{% endif %}
推荐
{% if flag %}yes{% endif %}

选项类型默认值
enabledbooleanfalse
levelstring"warning"

安全

绕过转义的输出、暴露模板上下文的调试输出,以及项目禁用的函数、过滤器或标签。

twig-disallowed-filterswarning

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

Names match exactly. An empty list reports nothing.


避免
{{ title|raw }}
推荐
{{ title }}

选项类型默认值
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.


避免
{{ source('secret.txt') }}
推荐
{{ title }}

选项类型默认值
enabledbooleanfalse
levelstring"warning"
namesarray[]
twig-disallowed-tagswarning

Reports the tags listed in names, including custom tags.

Names match exactly. An empty list reports nothing.


避免
{% flush %}
推荐
{{ title }}

选项类型默认值
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.


避免
{% autoescape false %}{{ value }}{% endautoescape %}
推荐
{% autoescape 'html' %}{{ value }}{% endautoescape %}

选项类型默认值
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.


避免
{{ dump(user) }}
推荐
{{ user.name }}

选项类型默认值
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.


避免
{{ user.name|raw }}
推荐
{{ user.name }}

选项类型默认值
enabledbooleanfalse
levelstring"warning"

正确性

缺陷与逻辑错误。本类规则负责发现作者本意之外的行为。

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.


避免
{% block title %}A{% endblock %}
{% block title %}B{% endblock %}
推荐
{% block title %}Title{% endblock %}

选项类型默认值
enabledbooleantrue
levelstring"error"
twig-duplicate-parametererror

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 %}

选项类型默认值
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.


避免
{{ range(low: 1, low: 3) }}
推荐
{{ range(low: 1, high: 3) }}

选项类型默认值
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.


避免
{{ {title: title, title: body} }}
推荐
{{ {title: title, body: body} }}

选项类型默认值
enabledbooleantrue
levelstring"warning"

↳ 编辑此页 →