agentleFS
michtiomichtio/craftcms-claude-skills/skills/craft-twig-guidelines/SKILL.md

craft-twig-guidelines

Skill90 starsChanged 4 months agoMITView on GitHub

Twig coding standards and conventions for Craft CMS 5 templates. ALWAYS load this skill when writing, editing, or reviewing any .twig file in a Craft CMS project, even for small edits. Covers: variable naming (camelCase, no abbreviations), null handling (?? operator, ??? with empty-coalesce plugin), whitespace control ({%- trimming, NOT {%- minify -%}), include isolation (always 'only'), Craft Twig helpers ({% tag %}, tag(), attr(), |attr, |parseAttr, |append, svg()), collect() for props and class collections, .implode(), ========= comment headers on component files, and pitfalls (snake_case, macros as components, hardcoded colors).

The file

We split this skill into the rules it gives an agent

7 rules. About 0 are common, 0 are distinctive, and 7 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈0

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 7

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Use WebFetch on specific doc pages when something isn't covered here.

    line 33
  2. Very specific:

    Every {% include %} uses only: No exceptions: without it the component silently depends on parent variables.

    line 48
  3. Very specific:

    Polymorphic elements: use {% tag element with {…} %} with a descriptive variable (element, wrapper).

    line 65
  4. Very specific:

    Data manipulation: default to collect() chains.

    line 67
  5. Very specific:

    Output: every user-facing string through |t('category').

    line 68
  6. Very specific:

    Copy style: no em-dashes (—) or en-dashes (–) in template text or |t strings.

    line 69
  7. Very specific:

    Grepping output for a |e('js')-escaped phrase: every space becomes a \uXXXX sequence, so grep or assert on a single token.

    line 89

≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Twig Coding Standards — Craft CMS 5

Craft Version of This Project

!sh ${CLAUDE_SKILL_DIR}/scripts/craft-version.sh

If no Craft version line appears above, establish the version before any version-specific advice: craftcms/cms in composer.lock for a site, the lowest version the craftcms/cms constraint in composer.json allows for a plugin, or ask. Then check it against the version tags in this skill ([5.10+], [changed 5.12], [seomatic 5.1+]).

Coding conventions for Twig templates in Craft CMS 5 projects. These apply to all Twig code — atomic components, views, layouts, builders, partials.

Companion Skills — Always Load Together

  • craft-site — Template architecture and component patterns (atomic design, routing, builders). Required when creating or editing components, layouts, views, or builders.
  • craft-content-modeling — Content architecture. Required when template code involves element queries, field access, or section decisions.

For PHP coding standards, see craft-php-guidelines.

Documentation

Use WebFetch on specific doc pages when something isn't covered here.

Reference Files

TaskRead~Tokens
Correct/wrong examples for naming, ??/???/?., whitespace, only on include/render/include_only(), macros, comment headers, collect() classes, copy style; long form of the pitfallsreferences/core-conventions.md~2,450
{% tag %}, tag(), attr(), |attr, |parseAttr, |append, svg(), url(…, false), heading()/h() [5.10+], filter additions [5.10+], collect() vs array filters, |t/|purify/|raw, {% js %}/{% css %}, |e('js') escapingreferences/craft-helpers.md~1,950
Upgrading to 5.12 [changed 5.12]: case filters follow the site language, Twig 3.29 to 3.31 deprecations (spaceless, macro calls, matches)references/craft-5-12-changes.md~800

Rules

  • Naming: camelCase, single word when context allows (heading, buttonText). Never snake_case, never abbreviations: element not el, button not btn, navigation not nav, description not desc.
  • Nulls: ?? is the default. ??? only when nystudio107/craft-emptycoalesce or nystudio107/craft-seomatic is already installed (check composer.json; never install one just for ???). ?. for chains whose intermediate links may be null (entry?.author?.fullName ?? 'Anonymous'), not for a single property. Never is defined and is not null.
  • Whitespace: {%- / {{- / -%} where output whitespace matters. Never {%- minify -%}.
  • Every {% include %} uses only. No exceptions: without it the component silently depends on parent variables. The same goes for {% render %} [5.12+]:

    {# Correct #}
    {%- render entry.contentBlocks with { theme: 'dark' } only -%}

    [5.12+] Before 5.12 the tag fails the whole template at compile time: use {{ entry.contentBlocks.render({ theme: 'dark' }) }}. Inside the partial, entry is the rendered element, not the page entry. include_only() [5.12+] is the function form; [5.12+] before 5.12: include(…, with_context = false). Partial lookup: craft-site (references/element-partials.md).

  • No {% macro %} for UI components (no extends/block, different scoping). Macros only for string-returning helpers.
  • Every component file gets a header (props, variants, views, layouts too), with the same ========= separator as the PHP convention:

    {# =========================================================================
       Component Name
       Brief description of what this component does.
       ========================================================================= #}
  • Polymorphic elements use {% tag element with {…} %} with a descriptive variable (element, wrapper); false omits an attribute. tag(), attr(), |attr, svg(): references/craft-helpers.md.
  • Class strings: collect({}) with camelCase named keys, one key per style concern, joined with .implode(' '). Never 'flex ' ~ extraClass. Full patterns: craft-site (references/twig-collections.md, references/tailwind-conventions.md).
  • Data manipulation: default to collect() chains; a bare |filter/|map only for one trivial inline step. There is no |indexBy (use |index or .keyBy()).
  • Output: every user-facing string through |t('category'); |purify for untrusted HTML, |raw only for trusted field output; {% js %}/{% css %} instead of hand-written <script>/<style>; never ship {% dd %}/dump().
  • Copy style: no em-dashes (—) or en-dashes (–) in template text or |t strings; use commas, colons, parentheses, or a plain hyphen, and write ranges as "4 to 10". Twig comments are exempt. Grep for — and – before finishing.

Common Pitfalls

  1. ??? operator without the plugin — requires nystudio107/craft-emptycoalesce or nystudio107/craft-seomatic. Check composer.json before using. Default to ??.
  2. snake_case variables — use camelCase: heroImage not hero_image.
  3. Missing only — silent variable leaking, invisible coupling.
  4. {%- minify -%} — deprecated. Use {%- whitespace control.
  5. Abbreviations — el, btn, nav, desc, ctr → spell it out.
  6. is not defined — verbose null checking. ?? handles it.
  7. Macros as components — wrong scoping, no extends/block support.
  8. Hardcoded colors in class strings — bg-yellow-600 → bg-brand-accent.
  9. String concatenation for classes — 'flex ' ~ extraClass → use collect({}) with named keys.
  10. is empty / |default on Craft Models [changed 5.10]: from 5.10 every yii\base\Model counts as non-empty, so {{ user|default('Guest') }} renders the user. Test the property: {% if entry.title is empty %}.
  11. options.x pattern — old macro convention. Use direct variable names.
  12. Blocks inside conditionals: invalid Twig. Put the {% if %} inside the {% block %}.
  13. Hardcoded /admin CP URL: cpTrigger is configurable. Use cpUrl().
  14. Em/en-dashes in user-facing copy: see Copy style above.
  15. Twig has no instanceof: branch on object.hasProperty()/hasMethod() or have PHP pass a discriminator.
  16. Per-request output inside {% cache %} (CSP nonce, csrfInput(), active classes, language-switcher URLs) is frozen at the first render. See the craft-site skill's template-caching.md.
  17. Grepping output for a |e('js')-escaped phrase: every space becomes a \uXXXX sequence, so grep or assert on a single token. See references/craft-helpers.md.
  18. {% render %} on a site below 5.12 [5.12+]: unknown tag, the whole template fails to compile. Use .render() where the template must also run on older releases.
  19. Appending alpha to a colour [changed 5.12]: {{ entry.brand }}80 or entry.brand.hex ~ '1a' breaks once a Color field returns an 8-digit hex (translucent palette colours, or custom colours with opacity allowed). Use rgb({{ entry.brand.rgbChannels }} / 50%) [5.12+] or entry.brand.rgba [5.12+]; entry.brand.getHex(false) ~ '80' works on every version. Before 5.12: hex was always 6-digit.
  20. Macros called without parentheses [5.12+]: {{ macros.label }} is deprecated in Twig 3.29 and a syntax error in Twig 4. Write {{ macros.label() }}.
  21. |upper/|lower on identifiers [changed 5.12]: casing follows the site language, so a Turkish site lowercases I to ı. Pass ('en') for codes and handles. See references/craft-5-12-changes.md.
  22. heading() / h() on an older site: heading() and h() need [5.10+]. Before 5.10: build the tag from the level yourself. {{ tag('h' ~ level, { text: title }) }} (tag() takes an attributes array; text is encoded, html is not) or {% tag 'h' ~ level with { class: 'text-xl' } %}Subsection{% endtag %}. Don't ship heading() / h() to a site below 5.10.
  23. url() with false [5.11+] strips every query param from an absolute URL. Before 5.11: siteUrl(craft.app.request.pathInfo).

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.