craft-site
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCraft CMS 5 — Front-End Twig (Atomic Design)
Craft CMS 5 — 前端Twig(原子设计)
Atomic design system patterns for Craft CMS 5 site templates. Vanilla Twig —
no module dependency. Works with any Craft 5 project.
This skill is scoped to front-end template architecture — component design,
routing, composition, theming, and buildchain. For extending Craft (plugins,
modules, PHP), see the skill. For Twig coding standards (naming,
null handling, whitespace, helpers), see . For PHP
standards, see . For content architecture (sections,
fields, entry types), see .
craftcmscraft-twig-guidelinescraft-php-guidelinescraft-content-modeling适用于Craft CMS 5站点模板的原子设计系统模式。原生Twig — 无模块依赖,可在任何Craft 5项目中使用。
本技能的范围限定为前端模板架构 — 组件设计、路由、组合、主题化和构建链。如需扩展Craft功能(插件、模块、PHP),请参考技能。如需Twig编码规范(命名、空值处理、空格、辅助方法),请参考。如需PHP规范,请参考。如需内容架构(分区、字段、条目类型),请参考。
craftcmscraft-twig-guidelinescraft-php-guidelinescraft-content-modelingDocumentation
文档
- Twig in Craft: https://craftcms.com/docs/5.x/development/twig.html
- Template tags: https://craftcms.com/docs/5.x/reference/twig/tags.html
- Template functions: https://craftcms.com/docs/5.x/reference/twig/functions.html
- Twig 3 docs: https://twig.symfony.com/doc/3.x/
Use on specific doc pages when a reference file doesn't cover enough detail.
web_fetch- Craft中的Twig:https://craftcms.com/docs/5.x/development/twig.html
- 模板标签:https://craftcms.com/docs/5.x/reference/twig/tags.html
- 模板函数:https://craftcms.com/docs/5.x/reference/twig/functions.html
- Twig 3官方文档:https://twig.symfony.com/doc/3.x/
当参考文件覆盖的细节不足时,可以对特定文档页面使用工具。
web_fetchCommon Pitfalls (Cross-Cutting)
常见陷阱(跨领域)
- Missing on
only— ambient variables leak in silently.{% include %} - Variant logic via conditionals () instead of extends/block.
{% if variant == 'x' %} - Naming atoms by parent context () instead of visual treatment (
hero-button).button--primary - prop used as override — it's additive. Override via named-slot merge.
utilities - Queries inside views — views receive data, they don't fetch it.
- Missing on relation fields in views — causes N+1 queries.
.eagerly() - Missing fallback in builders for unknown block types.
devMode - Hardcoded Tailwind colors () instead of brand tokens (
bg-yellow-600).bg-brand-accent - Mixing buttons and links — buttons are actions (resolve to ,
<a>, or<button>from props), links are navigation (always<span>). Separate atom categories.<a> - Tracking/analytics inside components — decouple to data attributes at view/page level.
- 缺少
{% include %}修饰符 — 环境变量会静默泄露。only - 通过条件判断实现变体逻辑(),而非使用extends/block。
{% if variant == 'x' %} - 按父级上下文命名原子(如),而非按视觉样式命名(如
hero-button)。button--primary - 将属性作为覆盖使用 — 它是追加属性,应通过命名插槽合并实现覆盖。
utilities - 在视图中编写查询逻辑 — 视图只接收数据,不负责拉取数据。
- 视图中的关联字段缺少方法 — 会导致N+1查询问题。
.eagerly() - 构建器中缺少针对未知块类型的降级逻辑。
devMode - 硬编码Tailwind颜色(),而非使用品牌令牌(
bg-yellow-600)。bg-brand-accent - 混淆按钮和链接 — 按钮是操作元素(根据属性渲染为、
<a>或<button>),链接是导航元素(始终渲染为<span>),需分属不同的原子类别。<a> - 在组件中内置埋点/分析逻辑 — 应解耦到视图/页面层级的数据属性中。
Reference Files
参考文件
Read the relevant reference file(s) for your task. Multiple files often apply together.
Task examples:
- "Build a new card component" → read +
atomic-patterns.md+component-inventory.mdtailwind-conventions.md - "Set up a new project's template structure" → read +
boilerplate-routing.mdcomponent-inventory.md - "Add a content builder for a Matrix field" → read +
boilerplate-routing.mdatomic-patterns.md - "Handle responsive images" → read +
image-presets.mdplugins/image-optimize.md - "Add multi-brand theming" → read
tailwind-conventions.md - "Decide between Alpine and Vue for a feature" → read
javascript-boundaries.md - "Compose Tailwind classes without conflicts" → read +
tailwind-conventions.mdtwig-collections.md - "Understand atomic design methodology" → read
atomic-design.md - "Set up Vite + Tailwind in a new Craft project" → read
vite-buildchain.md - "Debug why assets aren't loading in production" → read
vite-buildchain.md - "Install GTM/analytics/CMP in a Craft project" → read
third-party-integration.md - "Configure SEOMatic for a section" → read
plugins/seomatic.md - "Set up Blitz caching with Cloudflare" → read
plugins/blitz.md - "Add a form to a page" → read
plugins/formie.md - "Configure CKEditor with nested entries" → read
plugins/ckeditor.md - "Build a navigation menu" → read
plugins/navigation.md - "Add a link field to a component" → read
plugins/hyper.md - "Set up redirects for a site" → read
plugins/retour.md - "Add recurring/repeating dates to entries" → read
plugins/timeloop.md - "Create a JSON API endpoint" → read
plugins/element-api.md - "Debug N+1 queries in templates" → read
plugins/elements-panel.md - "Run a security audit" → read
plugins/sherlock.md - "Embed a YouTube/Vimeo video as an asset" → read
plugins/embedded-assets.md - "Configure email delivery via SES" → read +
plugins/amazon-ses.mdthird-party-integration.md
| Reference | Scope |
|---|---|
| Methodology: Brad Frost's atomic design principles, 5-to-3 tier compression, composability, context-agnostic naming, classification problem, decompose-downward workflow. Technology-independent. |
| Craft CMS implementation: props/extends/block pattern, button/link/text/icon atoms, molecule and organism composition, structural embed pattern, include/extends/embed decision table |
| Classification methodology: decision tree, naming conventions, file naming, props design, scaffold guidelines, tier promotion, audit checklist |
| Template chain: layout hierarchy, Craft section template paths, global variables, routers, views, content builders, directory structure |
| Class composition: named-key collections, standard key names, |
| Craft CMS Vite setup: nystudio107 plugin bridge, |
| Image handling: single atom with presets, srcset/sizes, ImageOptimize vs Craft Cloud, hosting strategies |
| JS decision tree: Twig → Alpine/DataStar → Vue, mount points, data handoff, coexistence rules |
| |
| Script loading order, CMP (UserCentrics/CookieBot), GTM/sGTM data layer, analytics (Fathom/Plausible), AWS SES transport, n8n webhooks, Blitz compatibility, full head template example |
根据你的任务阅读相关参考文件,通常多个文件会同时适用。
任务示例:
- "构建新的卡片组件" → 阅读+
atomic-patterns.md+component-inventory.mdtailwind-conventions.md - "搭建新项目的模板结构" → 阅读+
boilerplate-routing.mdcomponent-inventory.md - "为Matrix字段添加内容构建器" → 阅读+
boilerplate-routing.mdatomic-patterns.md - "处理响应式图片" → 阅读+
image-presets.mdplugins/image-optimize.md - "添加多品牌主题能力" → 阅读
tailwind-conventions.md - "为功能选型Alpine或Vue" → 阅读
javascript-boundaries.md - "组合Tailwind类避免冲突" → 阅读+
tailwind-conventions.mdtwig-collections.md - "了解原子设计方法论" → 阅读
atomic-design.md - "在新Craft项目中配置Vite + Tailwind" → 阅读
vite-buildchain.md - "调试生产环境资源加载失败问题" → 阅读
vite-buildchain.md - "在Craft项目中安装GTM/分析工具/CMP" → 阅读
third-party-integration.md - "为分区配置SEOMatic" → 阅读
plugins/seomatic.md - "配置Blitz缓存与Cloudflare集成" → 阅读
plugins/blitz.md - "为页面添加表单" → 阅读
plugins/formie.md - "配置支持嵌套条目的CKEditor" → 阅读
plugins/ckeditor.md - "构建导航菜单" → 阅读
plugins/navigation.md - "为组件添加链接字段" → 阅读
plugins/hyper.md - "配置站点重定向规则" → 阅读
plugins/retour.md - "为条目添加重复日期能力" → 阅读
plugins/timeloop.md - "创建JSON API端点" → 阅读
plugins/element-api.md - "调试模板中的N+1查询问题" → 阅读
plugins/elements-panel.md - "执行安全审计" → 阅读
plugins/sherlock.md - "将YouTube/Vimeo视频作为资源嵌入" → 阅读
plugins/embedded-assets.md - "配置SES邮件投递" → 阅读+
plugins/amazon-ses.mdthird-party-integration.md
| 参考文件 | 适用范围 |
|---|---|
| 方法论:Brad Frost的原子设计原则、5层到3层压缩、可组合性、上下文无关命名、分类问题、自上而下拆解工作流,技术无关。 |
| Craft CMS实现:props/extends/block模式、按钮/链接/文本/图标原子、分子与有机体组合、结构嵌入模式、include/extends/embed选型表 |
| 分类方法论:决策树、命名规范、文件命名、属性设计、脚手架指引、层级升级、审核检查清单 |
| 模板链:布局层级、Craft分区模板路径、全局变量、路由器、视图、内容构建器、目录结构 |
| 类组合:命名键集合、标准键名、 |
| Craft CMS Vite配置:nystudio107插件桥接、 |
| 图片处理:带预设的单一原子、srcset/sizes、ImageOptimize与Craft Cloud对比、托管策略 |
| JS选型决策树:Twig → Alpine/DataStar → Vue、挂载点、数据传递、共存规则 |
| |
| 脚本加载顺序、CMP(UserCentrics/CookieBot)、GTM/sGTM数据层、分析工具(Fathom/Plausible)、AWS SES传输、n8n webhook、Blitz兼容性、完整头部模板示例 |
Plugin References
插件参考
Detailed configuration, Twig API, and pitfalls for Craft plugins. Located in .
references/plugins/| Reference | Plugin | Key Surface |
|---|---|---|
| SEOMatic (nystudio107) | Meta cascade, Twig get/set API, JSON-LD, custom element SEO bundles, sitemaps, GraphQL |
| Blitz (putyourlightson) | Refresh modes, Twig dynamic content, driver architecture (storage/purger/deployer), Nginx rewrite, Cloudflare integration |
| Formie (verbb) | Form rendering (one-line/granular), theme config for Tailwind, submission querying, hooks, integrations |
| ImageOptimize (nystudio107) | OptimizedImages field, |
| CKEditor (craftcms) | Toolbar config, nested entries with chunk rendering, HTML Purifier, custom styles, Redactor migration |
| Colour Swatches (craftpulse) | Palette config, multi-colour swatches, Tailwind class mapping, Twig model API |
| Password Policy (craftpulse) | Validation rules, HIBP check, retention/expiry, console commands, recommended settings |
| Hyper (verbb) | Link types, |
| Retour (nystudio107) | Redirect types (exact/regex), 404 tracking, CSV import, auto slug-change redirects, config |
| Navigation (verbb) | Node querying, custom rendering, active states, custom fields on nodes, GraphQL |
| Typogrify (nystudio107) | |
| Cache Igniter (putyourlightson) | GlobalPing warmer, geographic locations, refresh delay, Blitz companion |
| Knock Knock (verbb) | Staging password protection, environment-aware config, URL exclusions |
| Timeloop (craftpulse) | Repeating date field, |
| Element API (craftcms) | JSON API via config file, Fractal transformers, endpoint routing, pagination, caching |
| Elements Panel (putyourlightson) | Debug toolbar panels for element population counts, duplicate detection, eager-loading opportunities |
| Sherlock (putyourlightson) | Security scanning, HTTP headers, CMS config checks, scheduled scans, IP restriction, monitoring |
| Embedded Assets (spicyweb) | oEmbed as first-class assets, |
| Amazon SES (putyourlightson) | SES mail transport adapter, AWS credential config, SNS bounce tracking |
Craft插件的详细配置、Twig API和常见陷阱,位于目录下。
references/plugins/| 参考文件 | 插件 | 核心覆盖范围 |
|---|---|---|
| SEOMatic (nystudio107) | Meta层叠、Twig get/set API、JSON-LD、自定义元素SEO包、站点地图、GraphQL |
| Blitz (putyourlightson) | 刷新模式、Twig动态内容、驱动架构(存储/清除器/部署器)、Nginx重写、Cloudflare集成 |
| Formie (verbb) | 表单渲染(单行/细粒度)、Tailwind主题配置、提交查询、钩子、集成 |
| ImageOptimize (nystudio107) | OptimizedImages字段、 |
| CKEditor (craftcms) | 工具栏配置、支持分块渲染的嵌套条目、HTML Purifier、自定义样式、Redactor迁移 |
| Colour Swatches (craftpulse) | 调色板配置、多色样本、Tailwind类映射、Twig模型API |
| Password Policy (craftpulse) | 校验规则、HIBP检查、留存/过期、控制台命令、推荐设置 |
| Hyper (verbb) | 链接类型、 |
| Retour (nystudio107) | 重定向类型(精确/正则)、404追踪、CSV导入、slug变更自动重定向、配置 |
| Navigation (verbb) | 节点查询、自定义渲染、激活状态、节点自定义字段、GraphQL |
| Typogrify (nystudio107) | |
| Cache Igniter (putyourlightson) | GlobalPing预热、地理位置、刷新延迟、Blitz配套工具 |
| Knock Knock (verbb) | staging环境密码保护、环境感知配置、URL排除 |
| Timeloop (craftpulse) | 重复日期字段、 |
| Element API (craftcms) | 基于配置文件的JSON API、Fractal转换器、端点路由、分页、缓存 |
| Elements Panel (putyourlightson) | 调试工具栏面板,支持元素填充计数、重复检测、预加载优化提示 |
| Sherlock (putyourlightson) | 安全扫描、HTTP头、CMS配置检查、定时扫描、IP限制、监控 |
| Embedded Assets (spicyweb) | oEmbed作为一等资源、 |
| Amazon SES (putyourlightson) | SES邮件传输适配器、AWS凭证配置、SNS退信追踪 |
Component System Conventions
组件系统规范
One canonical component system across all projects. Atoms are context-agnostic — always named by visual treatment, never by parent. HTML element type is resolved from props ( → , → , fallback → ).
url<a>type<button><span>External link detection is derived from the URL, never passed as a prop. Components auto-apply , , external icon, and sr-only text when a URL is external.
target="_blank"rel="noopener noreferrer"FontAwesome is the universal icon system. Icons are passed as FA class strings.
Visual variants use extends/block — base template defines structure, variant overrides classes. Never use conditional logic to switch between variant styles. For structural skeletons with content slots, use — see .
{% embed %}atomic-patterns.mdImage handling uses a single atom with config-driven presets, not separate variant files per context.
所有项目使用统一的标准组件系统。原子是上下文无关的 — 始终按视觉样式命名,绝不按父级上下文命名。HTML元素类型通过属性推导( → 、 → 、降级为)。
url<a>type<button><span>外部链接检测自动通过URL判断,无需作为属性传入。当URL为外部链接时,组件会自动添加、、外部图标和屏幕阅读器专用文本。
target="_blank"rel="noopener noreferrer"FontAwesome是通用图标系统,图标通过FA类名字符串传入。
视觉变体使用extends/block实现 — 基础模板定义结构,变体覆盖样式类。绝不要使用条件逻辑切换变体样式。对于带内容插槽的结构骨架,使用 — 参考。
{% embed %}atomic-patterns.md图片处理使用单一原子配合配置驱动的预设,而非为每个上下文单独创建变体文件。
CSS & Theming Conventions
CSS与主题规范
This skill assumes Tailwind CSS for class composition examples. Adapt patterns
to your CSS framework — the architectural principles (named keys, additive
utilities, semantic tokens) are framework-agnostic.
Class collections use named keys per style concern — this is the primary mechanism for preventing conflicts. The prop is additive (extending), not overriding. Override specific concerns via named-slot merge.
utilitiesMulti-brand theming uses CSS custom properties activated by on . Components reference semantic Tailwind classes () resolved via CSS variables. Template-level brand overrides only exist when the HTML structure itself differs between brands — not for color/font/spacing differences.
data-brand="{{ currentSite.handle }}"<html>bg-brand-accentToken naming follows three layers: primitives (), semantics (), and framework mapping ().
--brand-{color}-{shade}--brand-{purpose}--color-brand-{purpose}Tailwind v4 cascade layers don't fix class conflicts within the same utilities layer. Named-key collections remain necessary.
本技能的类组合示例基于Tailwind CSS编写,你可以将模式适配到你的CSS框架 — 架构原则(命名键、追加工具类、语义令牌)是框架无关的。
类集合按样式维度使用命名键 — 这是避免冲突的核心机制。属性是追加(扩展)属性,而非覆盖属性,需通过命名插槽合并覆盖特定维度的样式。
utilities多品牌主题使用CSS自定义属性实现,通过标签上的激活。组件引用语义化Tailwind类(),通过CSS变量解析。仅当不同品牌的HTML结构本身存在差异时,才在模板层级做品牌覆盖 — 颜色/字体/间距差异不需要。
<html>data-brand="{{ currentSite.handle }}"bg-brand-accent令牌命名遵循三层结构:原语()、语义()、框架映射()。
--brand-{color}-{shade}--brand-{purpose}--color-brand-{purpose}Tailwind v4的级联层无法解决同一工具层内的类冲突,仍需要使用命名键集合。
Routing Conventions
路由规范
PHP handles data, Twig handles presentation. Views receive data through includes — they never query it themselves.
collect()The JS boundary follows a decision tree: Twig is the default → Alpine/DataStar for UI state → Vue for application state.
PHP负责数据处理,Twig负责展示层。视图通过include接收数据 — 绝不自行查询数据。
collect()JS边界遵循决策树:默认使用Twig → UI状态需求使用Alpine/DataStar → 应用级状态需求使用Vue。