Skip to content

Latest commit

 

History

History
95 lines (72 loc) · 4.57 KB

htmltemplate.md

File metadata and controls

95 lines (72 loc) · 4.57 KB

Go html/template (Laravel Go) with the ui8kit CLI

Since ui8kit 3.7 / @ui8kit/registry 3.7 the htmltemplate runtime installs the UI8Kit primitives for the Go standard library html/template: one Go package ui with the templates embedded (//go:embed), no templ, no code generation step, no external modules. Works with Laravel Go views and any html/template app. Same DOM/ARIA as every other runtime.

1. Install the kit

# in the app root (next to go.mod)
npx ui8kit@latest init --yes --framework htmltemplate --dir internal/ui8kit
npx ui8kit@latest add --all
  • --framework htmltemplate is explicit only: a go.mod project is never auto-detected as htmltemplate (templ apps have a go.mod too).
  • init writes internal/ui8kit/utils/*.go, internal/ui8kit/ui/funcs.go, the agent rules .cursor/rules/ui8kit-primitives.mdc, ui8kit.config.json and ui8kit.lock.json. No package.json.
  • add writes internal/ui8kit/ui/<brick>.go and the embedded internal/ui8kit/ui/<brick>/*.html.
  • The generated import github.com/ui8kit/codegen/generated/htmltemplate/utils is rewritten from go.mod to <module>/internal/ui8kit/utils in every installed *.go (without go.mod the CLI warns and prints the fix).

2. Wire the template functions

Plain html/template:

import "example.com/app/internal/ui8kit/ui"

t := template.Must(template.New("page").Funcs(ui.Funcs()).ParseFS(views, "*.html"))

Laravel Go (bootstrap/app.go):

import (
	"context"
	"html/template"

	"app/internal/ui8kit/ui"
)

func Options() laravel.Options {
	return laravel.Options{
		// …
		Views:     resources.Views,
		ViewFuncs: func(ctx context.Context) template.FuncMap { return ui.Funcs() },
	}
}

(In a Laravel package: r.ViewFuncs(func(ctx context.Context) template.FuncMap { return ui.Funcs() }) in Register.) The kit's own templates are embedded in package ui: nothing goes to resources/views.

3. Use

{{ ui8kit_render "button" (ui8kit_dict "Variant" "outline" "Href" "/docs" "Children" "Docs") }}
{{ ui8kit_render "box" (ui8kit_dict "Tag" "section" "Children" (ui8kit_render "text" (ui8kit_dict "Tag" "h2" "Children" .Title))) }}
{{ ui8kit_render "select" (ui8kit_dict "Name" "c" "Options" (ui8kit_list (ui8kit_dict "Value" "a" "Label" "A"))) }}
  • Render names are snake case (select_option), props are canonical PascalCase (Tag, Class, Attrs).
  • Children are markup only as template.HTML (another ui8kit_render, Laravel's render "partial"); a plain string is escaped. Never convert user input to template.HTML.
  • ui8kit_classes "button" (ui8kit_dict …) composes classes without rendering (no asChild); ui8kit_attrs prints an attribute map with the kit's checks (invalid names dropped, unsafe URLs omitted).
  • Known difference: html/template percent-encodes URL values (/a b → /a%20b, also tel:+7 900).

4. Tailwind

Tailwind v4 (standalone CLI is fine) must scan the Go templates: add @source "../internal/ui8kit"; (path relative to your CSS entry) next to @import "tailwindcss";, plus your own views (@source "../resources/views";).

5. Blocks and compositions (registry.buildy.tw, CLI 3.7.1+)

The default registry https://registry.buildy.tw/htmltemplate also serves the compositions and the 168 blocks of the kit (the same items as every runtime, Go functions over ui.<Part> returning (template.HTML, error)):

npx ui8kit@latest add hero-simple faq-simple   # or add --all
  • They land next to the primitives as packages of your module: {dir}/compose, {dir}/blocks (+ blocks/presets, blocks/recipes, embedded), {dir}/orgutils, {dir}/icons. The CLI rewrites the kit module root github.com/ui8kit/codegen/generated/htmltemplate/… to <module>/<dir>/… (3.7.0 rewrote …/utils only).
  • Every block registers itself; parse your views with blocks.Funcs() (ui.Funcs() + ui8kit_block): ViewFuncs: func(ctx context.Context) template.FuncMap { return blocks.Funcs() }.
  • In a view: {{ ui8kit_block "hero-simple" (ui8kit_dict "title" "Hello" "description" .Lead) }}; props are the JSON shapes of the TypeScript props (camelCase), children is template.HTML or escaped text. In Go: blocks.HeroSimple(blocks.HeroSimpleProps{Title: "Hello"}) or blocks.Render("hero-simple", map[string]any{…}).

See also: Server runtimes · codegen docs/runtimes/htmltemplate.md.