Documentation
¶
Overview ¶
templ: version: v0.3.1020
Package layout provides page-level layout components including base HTML shell, containers, and structural templates.
templ: version: v0.3.1020
HTMX CDN version pinning and SRI hash lookup.
templ: version: v0.3.1020
templ: version: v0.3.1020
Index ¶
- Constants
- func Base(props PageProps) templ.Component
- func Minimal(props MinimalProps) templ.Component
- func Script(nonce string, src string, attrs templ.Attributes) templ.Component
- func Stylesheet(href string, attrs templ.Attributes) templ.Component
- func ThemeScript(nonce string) templ.Component
- func ThemeToggle(ariaLabel string, nonce string) templ.Component
- type HTMXVersion
- type MinimalProps
- type PageProps
Examples ¶
Constants ¶
const ( // DefaultThemeColor is the default light-mode theme color (indigo-600) DefaultThemeColor = "#4f46e5" // DefaultDarkThemeColor is the default dark-mode theme color (indigo-950) DefaultDarkThemeColor = "#1e1b4b" )
Variables ¶
This section is empty.
Functions ¶
func Base ¶
Base renders a complete HTML5 base layout with HTMX, theme support, and meta tags Use with children... to inject page content into the body
@layout.Base(props) {
<h1>My Page</h1>
}
func Minimal ¶
func Minimal(props MinimalProps) templ.Component
Minimal renders a minimal HTML5 document without HTMX or external dependencies. Defaults Locale to "en" when empty (valid HTML lang attribute). Useful for static pages, emails, or PDF generation
@layout.Minimal(layout.MinimalProps{Title: "My Title"}) {
<h1>Hello</h1>
}
func Script ¶ added in v0.7.0
Script renders a CSP-safe external `<script src="...">` tag with the nonce applied automatically. Use it instead of a raw `<script>` tag so the nonce can never be forgotten — under a strict Content-Security-Policy a forgotten nonce silently breaks the script in the browser with no error in your logs.
Pass optional attributes (async, defer, type="module", crossorigin, …) via attrs. Do not pass `nonce` via attrs; the nonce parameter owns it.
@layout.Script(nonce, "/static/app.js", nil)
@layout.Script(nonce, "/static/app.js", templ.Attributes{"defer": true})
@layout.Script(nonce, "/static/tailwind.min.js", templ.Attributes{"type": "module"})
Example ¶
package main
import (
"bytes"
"context"
"fmt"
"github.com/larsartmann/templ-components/layout"
)
func main() {
var buf bytes.Buffer
_ = layout.Script("nonce-abc", "/static/app.js", nil).Render(context.Background(), &buf)
fmt.Println(buf.String())
// Output will contain a CSP-safe script tag with nonce
}
Output:
func Stylesheet ¶ added in v0.7.0
func Stylesheet(href string, attrs templ.Attributes) templ.Component
Stylesheet renders a CSP-safe `<link rel="stylesheet">` tag. Companion to Script — ensures consistent, auditable external resource loading. While stylesheets are not subject to script-src nonce restrictions, this helper provides a single pattern for all external resource tags.
Pass optional attributes (media, crossorigin, integrity) via attrs.
@layout.Stylesheet("/static/app.css", nil)
@layout.Stylesheet("/static/print.css", templ.Attributes{"media": "print"})
func ThemeScript ¶
ThemeScript renders the inline script that sets the dark mode class before paint. Also sets `color-scheme` so native scrollbars, form controls, and <details> match the active theme. Include this in your base template head to prevent FOUC.
Only needed for the TOGGLE dark mode strategy (user-controlled .dark class). If you use OS-following (prefers-color-scheme), you do NOT need this script — Tailwind v4's default dark: variant handles it automatically.
func ThemeToggle ¶
ThemeToggle renders a theme toggle button with sun/moon icons. Uses role="switch" + aria-checked so screen-reader users know the active mode. Requires Tailwind CSS dark: variants and the ThemeScript in your base layout.
Only needed for the TOGGLE dark mode strategy (user-controlled .dark class). If you use OS-following (prefers-color-scheme), dark mode is automatic — there is nothing to toggle and this component is unnecessary.
@layout.ThemeToggle("Toggle theme", nonce)
Types ¶
type HTMXVersion ¶ added in v0.6.0
type HTMXVersion string
HTMXVersion is a pinned HTMX main-script version. Use the exported constants (e.g. HTMXVersion2_0_10) for compile-time safety; custom versions can be constructed via HTMXVersion("x.y.z") but will render without SRI (no integrity hash).
const ( // HTMXVersion2_0_10 is the htmx main-script version DefaultPageProps pins. HTMXVersion2_0_10 HTMXVersion = "2.0.10" )
type MinimalProps ¶
MinimalProps configures a minimal HTML5 document without HTMX or external dependencies
func DefaultMinimalProps ¶
func DefaultMinimalProps() MinimalProps
DefaultMinimalProps returns sensible defaults for MinimalProps
type PageProps ¶
type PageProps struct {
Title string
Description string
Locale string
OGImage string
ThemeColor string
DarkThemeColor string
// CSSPath auto-injects `<link rel="stylesheet" href={CSSPath}>` into the
// page `<head>`. DefaultPageProps sets it to "/app.css". Set CSSPath to ""
// to suppress the stylesheet tag entirely — for example when you load
// Tailwind via the browser Play CDN, embed styles inline, or serve your
// own stylesheet through HeadContent.
CSSPath string
Favicon string
// HTMXVersion controls auto-injection of the htmx `<script>` tag.
// DefaultPageProps sets it to HTMXVersion2_0_10, which emits
// `<script src="https://cdn.jsdelivr.net/npm/htmx.org@2.0.10" ...>`.
// Set HTMXVersion to "" (the zero value) to disable auto-injection — for
// example when you bundle htmx yourself, serve it from a local path via
// HeadContent, or don't use htmx at all.
HTMXVersion HTMXVersion
// HTMXUseSRI adds an `integrity` attribute (subresource integrity) to the
// auto-injected htmx script. Only meaningful when HTMXVersion is non-empty.
HTMXUseSRI bool
// HTMXCDN overrides the CDN base URL for htmx scripts. When empty, defaults
// to "https://cdn.jsdelivr.net/npm". Set to e.g. "https://unpkg.com" to use
// a different CDN, or a self-hosted origin. Both the htmx main script and
// the response-targets extension derive their URLs from this value.
HTMXCDN string
// HTMXResponseTargets controls whether the htmx response-targets extension
// is loaded alongside htmx. Defaults to true for backward compatibility;
// set to false to skip the extra request when you don't use it.
HTMXResponseTargets bool
BodyClass string
Nonce string
HeadContent templ.Component
// SecurityHeaders controls whether security meta tags are included
SecurityHeaders bool
}
PageProps configures the base page layout
func DefaultPageProps ¶
func DefaultPageProps() PageProps
DefaultPageProps returns sensible defaults for PageProps.
Two fields auto-inject `<head>` tags and are the most common defaults to override when integrating with an existing asset pipeline:
- CSSPath ("/app.css") — set to "" if you serve styles another way.
- HTMXVersion (HTMXVersion2_0_10) — set to "" to suppress the htmx CDN tag.
HTMXResponseTargets defaults to true (loads the response-targets extension alongside htmx); set to false to skip the extra request when unused.