一键导入
hugo-templates
Use when designing, debugging, or refactoring Hugo layouts, partials, lookup rules, render hooks, and asset pipelines at an expert level.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Use when designing, debugging, or refactoring Hugo layouts, partials, lookup rules, render hooks, and asset pipelines at an expert level.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
| name | hugo-templates |
| description | Use when designing, debugging, or refactoring Hugo layouts, partials, lookup rules, render hooks, and asset pipelines at an expert level. |
| version | 1.0.0 |
| author | Hermes Agent |
| license | MIT |
| platforms | ["linux","macos","windows"] |
| metadata | {"hermes":{"tags":["hugo","templates","go-templates","partials","render-hooks","assets","lookup-order"],"related_skills":["hugo","plan","requesting-code-review"]}} |
Use this skill when the task is specifically about Hugo's template system rather than Hugo installation or basic site setup.
This skill covers how Hugo chooses templates, how context and Go template syntax behave, how to structure layouts and partials, how to use render hooks and the resource pipeline, and how to debug template-level problems without guessing.
Prefer Hugo's official docs as source of truth for evolving behavior:
https://gohugo.io/templates/https://gohugo.io/templates/lookup-order/https://gohugo.io/templates/types/https://gohugo.io/functions/https://gohugo.io/render-hooks/Important details confirmed from the current docs and local Hugo v0.161.1:
type and layout in front matter can target a different template.layouts/page.html works for regular pages, while single.html is a fallback.baseof.html + block/define is the core composition pattern.layouts/_partials/.partial can return rendered HTML or any data type if the partial uses return.partialCached caches output and accepts variant keys.layouts/_markup/.resources.Get, minify, and fingerprint work naturally inside templates.hugo --templateMetrics --templateMetricsHints emits per-template timing data.baseof.html, block, define, and partials.Site, .Page, .Params, page collections, where, sort, group, and range. and $partialCachedDo not use this skill for:
For any request or build target, think in this order:
What page kind is this?
homepagesectiontaxonomytermWhat makes it more specific?
typelayoutWhich template file wins?
Does a base template apply?
baseof.html provides the shared shell.blocks via define.Does the selected template call partials, render hooks, or resources?
If you keep those five steps straight, most Hugo template bugs become obvious.
The modern high-signal files to reach for first are:
layouts/
baseof.html
home.html
page.html
section.html
taxonomy.html
term.html
_partials/
_markup/
Important fallback behavior from the docs:
page.html is the specific template for regular content pages.single.html is a fallback for regular pages when page.html does not exist._default/list.html.Practical guidance:
home.html, page.html, section.html, taxonomy.html, and term.html for clarity._default/single.html and _default/list.html when you intentionally want generic shared behavior.Key current rules from the docs:
type and layout.Use these rules when debugging:
layout or type.layouts/ before blaming the theme.A reliable override strategy:
layouts/page.html or layouts/section.html.layouts/posts/page.html when a specific section needs its own rendering.layout = 'landing' only when the content author should explicitly opt into a special template.When a user asks whether a Hugo theme parameter is used, do not stop at exact symbol matches.
Use this audit sequence:
.Site.Params.foosite.Params.foo.Site.Params.attributes.websiterange .Site.Params.attributesisset .Site.Paramsindex .Site.Params ...Example pattern:
{{ range $key, $value := .Site.Params.attributes }}
{{ $attrs = (printf "data-%s=\"%s\" %s" $key $value $attrs) | safeHTMLAttr }}
{{ end }}
If a theme then includes that partial in baseof.html, every entry under params.attributes is effectively used even when no key such as website is referenced by name.
This is the primary composition pattern for maintainable Hugo sites.
Base template:
<!doctype html>
<html>
<head>
<title>{{ .Title }}</title>
</head>
<body>
<header>{{ partial "nav.html" . }}</header>
<main>{{ block "main" . }}{{ end }}</main>
</body>
</html>
Page or section template:
{{ define "main" }}
<h1>{{ .Title }}</h1>
{{ .Content }}
{{ end }}
Best practices:
baseof.html structural: head, shell, header, footer, slots.define blocks, not in the base template.baseof.html with section-specific branching when separate templates would be clearer.The single most common Hugo mistake is losing track of context.
Rules:
. means the current context.. is typically a Page.range, with, and some partials, . changes.$ points to the root context originally passed into the current template.Example:
{{ range .Site.RegularPages }}
<a href="{{ .RelPermalink }}">{{ .Title }}</a>
<span>Current page title: {{ $.Title }}</span>
{{ end }}
Use this discipline:
{{ $page := . }}
{{ $pages := where site.RegularPages "Section" "posts" }}
dict.Safe partial call pattern:
{{ partial "card.html" (dict "page" . "class" "featured") }}
Then inside the partial:
{{ $p := .page }}
<article class="{{ .class }}">
<a href="{{ $p.RelPermalink }}">{{ $p.LinkTitle }}</a>
</article>
Use the collection that matches the page kind.
Common patterns:
{{ range site.RegularPages }}
<a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a>
{{ end }}
{{ range .Pages }}
<a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a>
{{ end }}
{{ range .Data.Terms.ByCount }}
<a href="{{ .Page.RelPermalink }}">{{ .Page.LinkTitle }}</a> ({{ .Count }})
{{ end }}
{{ range .Pages }}
<a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a>
{{ end }}
Practical rule:
site.RegularPages when you want a site-wide content collection..Pages when you want children of the current list-like page..Data.Terms only in taxonomy contexts.Reach for page collections before custom logic.
Common patterns:
{{ $posts := where site.RegularPages "Section" "posts" }}
{{ $featured := where $posts "Params.featured" true }}
{{ $sorted := sort $posts "Date" "desc" }}
{{ range first 5 $sorted }}
<a href="{{ .RelPermalink }}">{{ .Title }}</a>
{{ end }}
Guidelines:
partialCached.partialUse partial for reusable template fragments.
Key doc behavior:
partial optionally accepts context.return, it returns that value.return, it returns rendered HTML.HTML partial:
{{ partial "breadcrumbs.html" . }}
Data-returning partial:
{{ $card := partial "card-data.html" . }}
<a href="{{ $card.link }}">{{ $card.title }}</a>
With partial implementation:
{{ return (dict "title" .Title "link" .RelPermalink) }}
Use data-returning partials when you want shared derivation logic without coupling it to markup.
partialCachedUse partialCached only when the output is genuinely reusable.
Important doc behavior:
Pattern:
{{ partialCached "styles.html" . .Site.Language.Lang }}
Rules of thumb:
partialCached as a reflex; prove there is a repeated expensive partial first.Render hooks override Markdown rendering and belong in layouts/_markup/.
Common files:
layouts/_markup/render-link.html
layouts/_markup/render-image.html
layouts/_markup/render-heading.html
layouts/_markup/render-codeblock.html
Use render hooks when the source content is Markdown but the output HTML needs policy or structure changes.
Examples:
figureA minimal link render hook:
<a href="{{ .Destination | safeURL }}" data-hook="link"{{ with .Title }} title="{{ . }}"{{ end }}>{{ .Text | safeHTML }}</a>
Important boundary:
Hugo's resource pipeline is a template-level superpower.
Typical CSS pattern:
{{ with resources.Get "css/main.css" }}
{{ $css := . | minify | fingerprint }}
<link rel="stylesheet" href="{{ $css.RelPermalink }}" integrity="{{ $css.Data.Integrity }}">
{{ end }}
Useful resource functions confirmed in the docs:
resources.Getresources.GetMatchresources.Matchresources.FromStringresources.ExecuteAsTemplateresources.Minifyresources.Fingerprintresources.PostProcessresources.GetRemoteGuidance:
assets/ for pipeline-managed resources.static/ for files that should pass through unchanged.resources.ExecuteAsTemplate when an asset itself must be templated from site data.Use the right abstraction:
home.html, page.html, etc.): choose the full page structureGood decision rule:
Prefer stable defaults plus narrow overrides.
Examples:
layouts/page.htmllayouts/posts/page.htmllayout = 'landing'Avoid these anti-patterns:
if eq branchesWhen Hugo templates misbehave, do this in order.
Run a build and read the warnings:
hugo
If you suspect dev-server behavior differences:
hugo server -D --disableFastRender
Use template metrics to find slow templates or partials:
hugo --templateMetrics --templateMetricsHints
This was verified locally and emits a table with cumulative duration, average duration, cache potential, cached count, and template name.
Ask:
home, page, section, taxonomy, or term?layout or type?If values vanish inside a range or partial, suspect dot changes first.
Typical fix:
{{ $root := . }}
{{ range .Pages }}
{{ partial "card.html" (dict "root" $root "page" .) }}
{{ end }}
If lookup order is confusing, temporarily create the minimum files needed:
baseof.htmlThen add specificity one step at a time.
Confusing . with $.
range, . often becomes the current element.$ for the root context or pass values explicitly.Using a partial without passing the data it needs.
Caching the wrong thing with partialCached.
Debugging the wrong template file.
Using static/ instead of assets/ for pipeline-managed files.
resources.Get works with assets, not arbitrary static files.Expecting render hooks to affect manually-written HTML.
Overusing front matter layouts.
layout is powerful, but if every page needs it, the template hierarchy is probably wrong.Duplicating markup instead of using baseof.html and partials.
Mixing content concerns into layout selection.
Ignoring build warnings for missing template kinds.
section, taxonomy, or term templates explain broken pages immediately.layouts/baseof.html
layouts/home.html
layouts/page.html
layouts/section.html
baseof.html.define "main" blocks.hugo and inspect warnings.layouts/_partials/<name>.htmldictreturnlayouts/_markup/render-link.html.Destination, .Text, and .Titleassets/css/resources.Get | minify | fingerprintRelPermalink and integrity hashThese patterns were exercised successfully with local Hugo v0.161.1:
baseof.html + home.html, section.html, and page.htmllayouts/posts/page.htmlpartial "..." with dict inputreturn to produce a dict rather than HTMLpartialCached with a language variant keylayouts/_markup/render-link.htmlresources.Get, minify, and fingerprinthugo --templateMetrics --templateMetricsHintsSee the linked reference file for compact copy-paste snippets.
type and layout overrides in front matterhome.html, page.html, section.html, taxonomy.html, and term.html before generic fallbacksbaseof.htmldict$ or named variables when context changes under range/withpartialCached only with correct variant keysassets/, not static/layouts/_markup/hugo and read warnings before assuming the template is correcthugo --templateMetrics --templateMetricsHints when performance matters