Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions content/contributing/livetemplate.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
title: "Contributing to LiveTemplate Core Library"
source_repo: "https://github.com/livetemplate/livetemplate"
source_path: "CONTRIBUTING.md"
source_ref: "v0.16.0"
source_commit: "f4f9147c7066382d821c022caa48683d0886ad9a"
source_ref: "v0.18.1"
source_commit: "f6f22cc3190ec0bf15f9d8bbec14f34b35409f77"
---

# Contributing to LiveTemplate Core Library
Expand Down
4 changes: 2 additions & 2 deletions content/guides/ephemeral-components.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
title: "Ephemeral Components Guide"
source_repo: "https://github.com/livetemplate/livetemplate"
source_path: "docs/guides/ephemeral-components.md"
source_ref: "v0.16.0"
source_commit: "f4f9147c7066382d821c022caa48683d0886ad9a"
source_ref: "v0.18.1"
source_commit: "f6f22cc3190ec0bf15f9d8bbec14f34b35409f77"
---

# Ephemeral Components Guide
Expand Down
4 changes: 2 additions & 2 deletions content/guides/observability.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
title: "LiveTemplate Observability Guide"
source_repo: "https://github.com/livetemplate/livetemplate"
source_path: "docs/guides/OBSERVABILITY.md"
source_ref: "v0.16.0"
source_commit: "f4f9147c7066382d821c022caa48683d0886ad9a"
source_ref: "v0.18.1"
source_commit: "f6f22cc3190ec0bf15f9d8bbec14f34b35409f77"
---

# LiveTemplate Observability Guide
Expand Down
4 changes: 2 additions & 2 deletions content/guides/progressive-complexity.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
title: "Progressive Complexity Guide"
source_repo: "https://github.com/livetemplate/livetemplate"
source_path: "docs/guides/progressive-complexity.md"
source_ref: "v0.16.0"
source_commit: "f4f9147c7066382d821c022caa48683d0886ad9a"
source_ref: "v0.18.1"
source_commit: "f6f22cc3190ec0bf15f9d8bbec14f34b35409f77"
---

# Progressive Complexity Guide
Expand Down
4 changes: 2 additions & 2 deletions content/guides/scaling.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
title: "LiveTemplate Scaling Guide"
source_repo: "https://github.com/livetemplate/livetemplate"
source_path: "docs/guides/SCALING.md"
source_ref: "v0.16.0"
source_commit: "f4f9147c7066382d821c022caa48683d0886ad9a"
source_ref: "v0.18.1"
source_commit: "f6f22cc3190ec0bf15f9d8bbec14f34b35409f77"
---

# LiveTemplate Scaling Guide
Expand Down
4 changes: 2 additions & 2 deletions content/guides/standard-html-reactivity.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
title: "Standard HTML Reactivity"
source_repo: "https://github.com/livetemplate/livetemplate"
source_path: "docs/guides/standard-html-reactivity.md"
source_ref: "v0.16.0"
source_commit: "f4f9147c7066382d821c022caa48683d0886ad9a"
source_ref: "v0.18.1"
source_commit: "f6f22cc3190ec0bf15f9d8bbec14f34b35409f77"
---

# Standard HTML Reactivity
Expand Down
9 changes: 5 additions & 4 deletions content/reference/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
title: "Go Library API Reference"
source_repo: "https://github.com/livetemplate/livetemplate"
source_path: "docs/references/api-reference.md"
source_ref: "v0.16.0"
source_commit: "f4f9147c7066382d821c022caa48683d0886ad9a"
source_ref: "v0.18.1"
source_commit: "f6f22cc3190ec0bf15f9d8bbec14f34b35409f77"
---

# Go Library API Reference
Expand Down Expand Up @@ -423,11 +423,11 @@ Options passed to `New()`:

| Option | Signature | Description |
|--------|-----------|-------------|
| `WithDevMode` | `(enabled bool)` | Enable development mode |
| `WithDevMode` | `(enabled bool)` | Enable dev mode: allows all WS origins, verbose logging, `{{.lvt.DevMode}}` |
| `WithSessionStore` | `(store SessionStore)` | Set session store |
| `WithAuthenticator` | `(auth Authenticator)` | Set authenticator |
| `WithAllowedOrigins` | `(origins []string)` | Allowed WebSocket origins |
| `WithPermissiveOriginCheck` | `()` | Bypass origin check (dev only) |
| `WithPermissiveOriginCheck` | `()` | Bypass origin check without dev mode (`WithDevMode` already relaxes origins) |
| `WithMaxConnections` | `(max int64)` | Max WebSocket connections |
| `WithMaxConnectionsPerGroup` | `(max int64)` | Max connections per group |
| `WithWebSocketDisabled` | `()` | HTTP-only mode |
Expand All @@ -437,6 +437,7 @@ Options passed to `New()`:
| `WithCookieMaxAge` | `(maxAge time.Duration)` | Session cookie max age |
| `WithUpgrader` | `(upgrader *websocket.Upgrader)` | Custom WebSocket upgrader |
| `WithParseFiles` | `(files ...string)` | Explicit template files |
| `WithParseFS` | `(fsys fs.FS, patterns ...string)` | Templates from an `fs.FS` (e.g. `embed.FS`); precedence over `WithParseFiles` |
| `WithTemplateBaseDir` | `(dir string)` | Template discovery base dir |
| `WithIgnoreTemplateDirs` | `(dirs ...string)` | Skip directories during discovery |
| `WithUpload` | `(name string, config UploadConfig)` | Configure upload field |
Expand Down
4 changes: 2 additions & 2 deletions content/reference/authentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
title: "Authentication Reference"
source_repo: "https://github.com/livetemplate/livetemplate"
source_path: "docs/references/authentication.md"
source_ref: "v0.16.0"
source_commit: "f4f9147c7066382d821c022caa48683d0886ad9a"
source_ref: "v0.18.1"
source_commit: "f6f22cc3190ec0bf15f9d8bbec14f34b35409f77"
---

# Authentication Reference
Expand Down
4 changes: 2 additions & 2 deletions content/reference/client-attributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
title: "Client Attributes Reference"
source_repo: "https://github.com/livetemplate/livetemplate"
source_path: "docs/references/client-attributes.md"
source_ref: "v0.16.0"
source_commit: "f4f9147c7066382d821c022caa48683d0886ad9a"
source_ref: "v0.18.1"
source_commit: "f6f22cc3190ec0bf15f9d8bbec14f34b35409f77"
---

# Client Attributes Reference
Expand Down
10 changes: 5 additions & 5 deletions content/reference/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
title: "LiveTemplate Configuration Guide"
source_repo: "https://github.com/livetemplate/livetemplate"
source_path: "docs/references/CONFIGURATION.md"
source_ref: "v0.16.0"
source_commit: "f4f9147c7066382d821c022caa48683d0886ad9a"
source_ref: "v0.18.1"
source_commit: "f6f22cc3190ec0bf15f9d8bbec14f34b35409f77"
---

# LiveTemplate Configuration Guide
Expand Down Expand Up @@ -140,9 +140,9 @@ Enable development mode.
- **Accepted values**: `true`, `false`, `1`, `0`, `yes`, `no`, `on`, `off` (case-insensitive)

**Features when enabled**:
- Uses local client library instead of CDN
- Allows **all** WebSocket origins — disables the same-origin/CSRF check, so `WithPermissiveOriginCheck()` is redundant in dev (**never enable in production**)
- More verbose logging
- Less strict origin checking
- Exposes `{{.lvt.DevMode}}` to templates

**Use case**: Local development and debugging.

Expand Down Expand Up @@ -345,7 +345,7 @@ tmpl := livetemplate.New("app",
| `WithPubSubBroadcaster(broadcaster)` | Redis pub/sub for distributed deployments |
| `WithComponentTemplates(sets...)` | Register component template sets |
| `WithIgnoreTemplateDirs(dirs...)` | Skip directories during template discovery |
| `WithPermissiveOriginCheck()` | Bypass origin check (dev only) |
| `WithPermissiveOriginCheck()` | Bypass origin check without dev mode (`WithDevMode` already relaxes origins) |
| `WithProgressiveEnhancement(enabled)` | Non-JS form submission support (default: true) |
| `WithCookieMaxAge(duration)` | Session cookie max age (default: 365 days) |

Expand Down
58 changes: 54 additions & 4 deletions content/reference/controller-pattern.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
title: "Controller+State Pattern Reference"
source_repo: "https://github.com/livetemplate/livetemplate"
source_path: "docs/references/controller-pattern.md"
source_ref: "v0.16.0"
source_commit: "f4f9147c7066382d821c022caa48683d0886ad9a"
source_ref: "v0.18.1"
source_commit: "f6f22cc3190ec0bf15f9d8bbec14f34b35409f77"
---

# Controller+State Pattern Reference
Expand Down Expand Up @@ -213,10 +213,59 @@ func (c *TodoController) OnDisconnect() {
- Cancel background jobs
- Unsubscribe from data feeds

## State methods in templates

Exported zero-arg methods on your State type are callable from templates just like
fields — `{{.ActiveCount}}` resolves to `ActiveCount()`'s return value. Methods
returning `(T, error)` are omitted (with a warning) when the error is non-nil.

Because LiveTemplate converts State to a map before rendering, these methods are
*precomputed* — but only for methods your templates actually reference. A method that
appears nowhere in any template is never called, so an expensive or side-effecting
helper you keep on State but don't render costs nothing.

Two caveats:

- **Conditional references still run.** The scoping is by name, not by reachability:
`{{if .Show}}{{.Expensive}}{{end}}` still calls `Expensive()` on every render even
when `.Show` is false, because the name appears in the template text. Keep genuinely
expensive work in an action method, not a render-time State method.
- **Prefer methods without side effects.** Precompute timing is an implementation
detail; a State method should compute a view of the state, not mutate anything or
perform I/O.

## Context API

For the complete Context API (data extraction, HTTP operations, struct binding), see [API Reference — Context](api-reference.md#context).

### Request context

`*livetemplate.Context` **embeds `context.Context`** — it carries the request/connection
context (cancellation, deadline, request-scoped values). So `ctx` *is* a `context.Context`:
pass it directly to any context-aware call (database queries, outbound HTTP, tracing) instead
of `context.Background()`.

```go
func (c *TodoController) Add(state TodoState, ctx *livetemplate.Context) (TodoState, error) {
// ✅ ctx is a context.Context — propagates cancellation when the request/connection ends
if err := c.DB.InsertTodo(ctx, todo); err != nil {
return state, err
}
return state, nil
}
```

Reaching for `context.Background()` inside an action instead silently discards that
cancellation and any deadline or trace attached to the request:

```go
// ❌ throws away request-scoped cancellation, deadline, and tracing
if err := c.DB.InsertTodo(context.Background(), todo); err != nil { ... }
```

Use `context.Background()` only for work that must deliberately **outlive** the request (e.g.
a fire-and-forget goroutine you start from the action) — not for the action's own calls.

## Error Handling

For validation errors, field errors, and template error display, see [Error Handling Reference](error-handling.md).
Expand Down Expand Up @@ -266,7 +315,8 @@ type TodoController struct {
}

func (c *TodoController) Mount(state TodoState, ctx *livetemplate.Context) (TodoState, error) {
items, err := c.DB.GetTodos()
// Pass ctx (a context.Context) so the query is cancelled if the request ends.
items, err := c.DB.GetTodos(ctx)
if err != nil {
return state, fmt.Errorf("failed to load todos: %w", err)
}
Expand All @@ -285,7 +335,7 @@ func (c *TodoController) Add(state TodoState, ctx *livetemplate.Context) (TodoSt
Title: title,
}

if err := c.DB.InsertTodo(todo); err != nil {
if err := c.DB.InsertTodo(ctx, todo); err != nil {
return state, fmt.Errorf("database error")
}

Expand Down
4 changes: 2 additions & 2 deletions content/reference/error-handling.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
title: "Error Handling Reference"
source_repo: "https://github.com/livetemplate/livetemplate"
source_path: "docs/references/error-handling.md"
source_ref: "v0.16.0"
source_commit: "f4f9147c7066382d821c022caa48683d0886ad9a"
source_ref: "v0.18.1"
source_commit: "f6f22cc3190ec0bf15f9d8bbec14f34b35409f77"
---

# Error Handling Reference
Expand Down
4 changes: 2 additions & 2 deletions content/reference/limitations.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
title: "Current Limitations"
source_repo: "https://github.com/livetemplate/livetemplate"
source_path: "docs/references/current-limitations.md"
source_ref: "v0.16.0"
source_commit: "f4f9147c7066382d821c022caa48683d0886ad9a"
source_ref: "v0.18.1"
source_commit: "f6f22cc3190ec0bf15f9d8bbec14f34b35409f77"
---

# Current Limitations
Expand Down
4 changes: 2 additions & 2 deletions content/reference/navigate.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
title: "Navigate Action Reference"
source_repo: "https://github.com/livetemplate/livetemplate"
source_path: "docs/references/navigate.md"
source_ref: "v0.16.0"
source_commit: "f4f9147c7066382d821c022caa48683d0886ad9a"
source_ref: "v0.18.1"
source_commit: "f6f22cc3190ec0bf15f9d8bbec14f34b35409f77"
---

# Navigate Action Reference
Expand Down
4 changes: 2 additions & 2 deletions content/reference/progressive-complexity.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
title: "Progressive Complexity Reference"
source_repo: "https://github.com/livetemplate/livetemplate"
source_path: "docs/references/progressive-complexity-reference.md"
source_ref: "v0.16.0"
source_commit: "f4f9147c7066382d821c022caa48683d0886ad9a"
source_ref: "v0.18.1"
source_commit: "f6f22cc3190ec0bf15f9d8bbec14f34b35409f77"
---

# Progressive Complexity Reference
Expand Down
4 changes: 2 additions & 2 deletions content/reference/pubsub.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
title: "PubSub Reference"
source_repo: "https://github.com/livetemplate/livetemplate"
source_path: "docs/references/pubsub.md"
source_ref: "v0.16.0"
source_commit: "f4f9147c7066382d821c022caa48683d0886ad9a"
source_ref: "v0.18.1"
source_commit: "f6f22cc3190ec0bf15f9d8bbec14f34b35409f77"
---

# PubSub Reference
Expand Down
Loading
Loading