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.0"
source_commit: "c96981964c226221cf2a34848e4350a494c08e8c"
---

# 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.0"
source_commit: "c96981964c226221cf2a34848e4350a494c08e8c"
---

# 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.0"
source_commit: "c96981964c226221cf2a34848e4350a494c08e8c"
---

# 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.0"
source_commit: "c96981964c226221cf2a34848e4350a494c08e8c"
---

# 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.0"
source_commit: "c96981964c226221cf2a34848e4350a494c08e8c"
---

# 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.0"
source_commit: "c96981964c226221cf2a34848e4350a494c08e8c"
---

# Standard HTML Reactivity
Expand Down
5 changes: 3 additions & 2 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.0"
source_commit: "c96981964c226221cf2a34848e4350a494c08e8c"
---

# Go Library API Reference
Expand Down Expand Up @@ -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.0"
source_commit: "c96981964c226221cf2a34848e4350a494c08e8c"
---

# 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.0"
source_commit: "c96981964c226221cf2a34848e4350a494c08e8c"
---

# Client Attributes Reference
Expand Down
4 changes: 2 additions & 2 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.0"
source_commit: "c96981964c226221cf2a34848e4350a494c08e8c"
---

# LiveTemplate Configuration Guide
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.0"
source_commit: "c96981964c226221cf2a34848e4350a494c08e8c"
---

# 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.0"
source_commit: "c96981964c226221cf2a34848e4350a494c08e8c"
---

# 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.0"
source_commit: "c96981964c226221cf2a34848e4350a494c08e8c"
---

# 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.0"
source_commit: "c96981964c226221cf2a34848e4350a494c08e8c"
---

# 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.0"
source_commit: "c96981964c226221cf2a34848e4350a494c08e8c"
---

# 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.0"
source_commit: "c96981964c226221cf2a34848e4350a494c08e8c"
---

# PubSub Reference
Expand Down
Loading
Loading