Skip to content

Latest commit

 

History

History
222 lines (150 loc) · 12.3 KB

dot-context.md

File metadata and controls

222 lines (150 loc) · 12.3 KB

Dot context

The dot context is the value of . on every template execution for a request. It is a struct assembled per request from builtin providers plus any configured core or custom dot providers.

It is the sole channel for request data, response control, and backing data sources. Stateless helpers are template functions (FuncMap), not request-scoped.

Builtin providers

Field Available on Go docs
.X all requests DotX
.Req all requests DotReq
.Vars all requests DotVars
.Resp buffered handlers DotResp
.Flush flushing / SSE handlers DotFlush

Field assembly order is {X, Req, Vars} + configured providers + {Resp | Flush}. The names X, Req, Vars, Resp, and Flush are reserved.

Instance data in .X

Read-only view of the loaded instance. Common methods:

Method Role
StaticFileHash path Content hash for SRI / cache-busting query params
Template name dot Execute the named template with the given dot; returns rendered HTML
Func name Look up a template function by name

Full API: DotX.

{{- with $hash := .X.StaticFileHash `/assets/reset.css`}}
<link rel="stylesheet" href="/assets/reset.css?hash={{$hash}}" integrity="{{$hash}}">
{{- end}}

When the query string carries the content hash, xtemplate can emit long-lived Cache-Control so clients cache aggressively without stale static files after a change.

Request details in .Req

Embeds *http.Request. Use standard fields and methods: Method, URL, Header, PathValue, FormValue, Cookie, and ReadBody.

Call .Req.ParseForm before relying on .Req.Form / .Req.PostForm if you are not using FormValue (which parses as needed).

.Req.ReadBody returns the raw body as a string, for JSON or webhook payloads. With no argument the limit is 1 MiB (DefaultMaxBodyBytes); pass one positive byte count to override it ({{.Req.ReadBody 4096}}). Zero, negative, or extra arguments are a template error. A body over the limit fails the request with HTTP 413. A successful read restores the body, so a second ReadBody in the same template returns the same bytes when its limit is at least the body size, and FormValue / ParseForm still see the payload when ReadBody runs first.

<p>Path: {{.Req.URL.Path}}</p>
<p>Id: {{.Req.PathValue "id"}}</p>
<p>Name: {{.Req.FormValue "name"}}</p>
{{define "POST /hooks/example"}}
  {{with .Req.ReadBody | fromJson}}{{.event}}{{end}}
{{end}}

Request scratch in .Vars

Per-request map[string]any for light composition: define-templates can act as shallow subroutines with out-params. A non-nil empty map is created every request; maps are not shared across requests and are not a session or flash store.

Primary API:

Method Role
Set key value Store a value; returns ""
Get key Value or nil if missing
Has key Whether the key is present
Delete key Remove a key; returns ""

Keys must be non-empty strings. Prefer short noun keys (list, todo, owner) and set-then-get in the same handler tree. If a helper needs more than a few keys or real domain logic, use a custom provider or FuncMap instead.

{{define "require-list"}}
  {{- $r := try .DB "QueryRow" `SELECT id, name FROM lists WHERE id=?` (.Req.PathValue "id")}}
  {{- if not $r.OK}}
    {{.Resp.RespondWith 404 (.X.Template "/shared/.404.html" .)}}
  {{- end}}
  {{- .Vars.Set "list" $r.Value -}}
{{end}}

{{define "POST /list/{id}/todos"}}
  {{- template "require-list" .}}
  {{- $list := .Vars.Get "list"}}
  {{- /* mutate using $list */}}
{{end}}

Because .Vars is a map, Sprout dict helpers (set, index, hasKey, range, including the Sprig names) also work. Prefer the method API in app code. Caveat: Sprout/Sprig get returns "" on missing keys; .Vars.Get returns nil.

Full API: DotVars.

Response control in .Resp

Available on buffered template handlers (normal GET/POST/… routes). Output is buffered so status and headers can be set during execution; on error the buffer is discarded.

A buffered page sends Content-Type: text/html; charset=utf-8 when the rendered body contains non-whitespace content and neither the template nor the response already set Content-Type. Empty and whitespace-only bodies, and statuses that must not include a body (1xx, 204, 304), do not get that default. Action-only redirects whose only output is the newlines between actions stay unset. An explicit SetHeader or AddHeader value is left as written.

Common methods: AddHeader, SetHeader, DelHeader, SetStatus, ReturnStatus (status + early return, keeps buffer), RespondWith (status + body, replaces buffer), ServeContent (file-like payload, replaces buffer). Full API: DotResp.

{{.Resp.AddHeader "Location" "/"}}
{{.Resp.ReturnStatus 303}}

Replace the response with RespondWith

Use when discovery mid-handler shows the client should not see partial template output: 404 pages, plain-text 400s, empty-body redirects.

{{.Resp.AddHeader "Location" "/"}}
{{.Resp.RespondWith 303 ""}}

{{.Resp.RespondWith 400 "name is required"}}

{{.Resp.RespondWith 404 (.X.Template "/shared/.404.html" .)}}
Exit Buffer Body
Success / return / ReturnStatus kept whatever the template wrote
RespondWith replaced explicit body ("" when empty)
ServeContent replaced file-like content
failf discarded generic 500
  • Body is required; pass "" for empty. Supported types: string, template.HTML, []byte.
  • Headers set on .Resp before RespondWith are kept.
  • Prefer .X.Template for HTML pages (private buffer → template.HTML), then install with RespondWith. Do not {{template "404" .}} then ReturnStatus for the replace case — that appends into the request buffer.
  • If body is template.HTML, non-empty, and Content-Type is unset, it defaults to text/html; charset=utf-8. Empty bodies do not force a Content-Type.
  • RespondWith is buffered-only (field is .Resp); not available on .Flush / SSE.

Streaming control in .Flush

Available on flushing handlers - routes defined with the SSE method prefix. Use for Server-Sent Events and other incremental responses. Common methods: SendSSE, Flush, Repeat, Sleep (returns early if the request or server is cancelled), WaitForServerStop. Full API: DotFlush; example: sse-chat.

{{- define "SSE /reload"}}{{.Flush.WaitForServerStop}}data: reload{{printf "\n\n"}}{{end}}
<script>new EventSource("/reload").onmessage = () => location.reload()</script>

Core providers (configured dot fields)

Core providers are provider packages under github.com/infogulch/xtemplate/providers/…. Default CLI binaries and the Caddy standard provider set blank-import them. Each can contribute a dot field when configured by the user (name in JSON / field token in Caddyfile); examples use conventional names (.DB, .FS, …), not fixed globals.

Configure via JSON provider config, Caddyfile, or Go (WithProvider / package helpers).

Queries and exec with sql

Package: providers/dotsql. Provider type: "sql".

<ul>
  {{range .DB.QueryRows `SELECT id, name FROM contacts`}}
  <li><a href="/contact/{{.id}}">{{.name}}</a></li>
  {{end}}
</ul>

Methods include QueryRows, QueryRow, QueryVal, Exec, and explicit Commit. Each request gets a value that opens a transaction on first use and commits on success or rolls back on error (via Finalizer).

Default builds include the sqlite3 driver (ncruces/go-sqlite3); other drivers need a custom build.

See also examples/contacts.

Filesystem access with fs

Package: providers/dotfs. Provider type: "fs".

<ol>
{{range .FS.ReadDir "dir/"}}
  <li>{{.Name}}</li>
{{end}}
</ol>

Optional writable: true exposes multipart upload (ReceiveFiles). Full API: package docs. Demo: examples/filebrowser.

Static key/value config with flags

Package: providers/dotflags. Provider type: "flags". Exposes a fixed map of strings (feature flags, env labels, versions) without a separate config file format inside templates.

In-process broadcast with bus

Package: providers/dotbus. Provider type: "bus". Process-local multi-producer multi-consumer topic fan-out for SSE and live UI — no external broker.

Typical field name: .Bus. Optional buffer is the per-subscriber channel capacity (default 16). Publish is best-effort: if a subscriber's buffer is full, that message is dropped for that subscriber (never blocks the publisher). Prefer nats when you need multi-process delivery, request/reply, or durable streams.

{{define "SSE /events"}}{{range .Bus.Subscribe "messages"}}{{$.Flush.SendSSE "" .}}{{end}}{{end}}
{{define "POST /messages"}}{{.Req.ParseForm}}{{.Bus.Publish "messages" (.Req.FormValue "msg")}}ok{{end}}

JSON:

{ "type": "bus", "name": "Bus", "buffer": 16 }

Messaging with nats

Package: providers/dotnats. Provider type: "nats". Send, request/reply, and stream-oriented patterns. Integration tests under test/templates/nats/ exercise a working configuration with an in-process server.

For raw JSON provider config, nats connection options follow the nats.Options field names (e.g. "Url" with a capital U when setting the server URL). Caddyfile conn_options { url … } maps correctly via the Caddyfile adapter.

Email with smtp

Package: providers/dotsmtp. Provider type: "smtp". Synchronous, send-only SMTP delivery. Body rendering stays with .X.Template; this provider only transports already-rendered strings. There is no built-in queue — compose with nats/JetStream if you need durable async delivery.

Typical field name: .Email. Config requires host and from (default sender). Optional connection settings: port (default 587), username / password, auth (plain, login, cram-md5, none, or empty for auto), tls (starttls default, tls, none), helo. Safety limits: max_recipients (default 50), max_message_bytes (default 1 MiB), send_timeout (default "30s"; JSON duration string only — see Configuration).

{{$body := .X.Template "email/welcome.html" .}}
{{$id := .Email.Send "alice@example.com" "Welcome!" $body}}

Send(to, subject, body, extra…) delivers one message and returns the generated Message-ID. to is a single address string or a list of address strings (each string is one RFC 5322 address; recipients are not split on commas). Optional final map keys: cc, bcc (same shape as to), from (override default sender), replyTo, text (plaintext alternative). Unknown keys and wrong value types error.

Custom providers

Implement xtemplate.Provider and attach with a WithProvider factory (or register a provider type for JSON/Caddyfile). See How to create a custom dot provider and examples/dotprovider.