Use when: (1) creating Hugo layouts with HTMX integration, (2) building partials for dynamic content, (3) writing shortcodes with Alpine.js state, (4) configuring params.api endpoints.
Create Hugo Go templates that integrate with HTMX for dynamic content and Alpine.js for client-side state.
| Template Type | Location | Purpose | Reference |
|---|---|---|---|
| Base layout | layouts/_default/baseof.html | Page wrapper with HTMX/Alpine setup | layouts-partials.md |
| Partial | layouts/partials/*.html | Reusable template fragments | layouts-partials.md |
| Shortcode | layouts/shortcodes/*.html | Embeddable content components | shortcodes.md |
| List/Single | layouts/_default/*.html | Content type templates | layouts-partials.md |
Configure dynamic endpoints in hugo.toml:
[params.api]
contact = "/app/_/contact"
comments = "/app/_/comments"
search = "/app/_/search"
Access in templates: {{ .Site.Params.api.contact }}
<form hx-post="{{ .Site.Params.api.contact }}" hx-target="#response" hx-swap="innerHTML">
<input type="text" name="message" required />
<button type="submit">Send</button>
</form>
<div id="response"></div>
<div x-data="{ submitting: false, errors: {} }">
<form @htmx:before-request="submitting = true" @htmx:after-request="submitting = false">
<button :disabled="submitting">
<span x-show="!submitting">Submit</span>
<span x-show="submitting">Loading...</span>
</button>
</form>
</div>
{{ if not .Draft }} {{ .Content }} {{ end }}
Hugo static pages use hash-based CSP (not nonce-based). Inline scripts are allowed via SHA-256 hashes.
Current approach: unsafe-inline is used for simplicity (see hugo/static/_headers and hugo/CLAUDE.md).
To migrate to hash-based CSP:
Find inline scripts:
grep -r "<script>" layouts/
Compute SHA-256 hash for each script:
echo -n 'exact script content' | openssl dgst -sha256 -binary | openssl base64
Update CSP in hugo/static/_headers:
Content-Security-Policy: default-src 'self'; script-src 'self' 'sha256-HASH1' 'sha256-HASH2'
Alternative: External scripts with SRI:
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/cdn.min.js"
integrity="sha384-HASH"
crossorigin="anonymous"
defer
></script>
When to migrate:
DO NOT use nonce-based CSP for static Hugo pages (nonces only work for dynamic per-request responses).
baseof.htmlhugo.toml params.api