PhoenixDatastar.Actions (PhoenixDatastar v0.1.12)

Copy Markdown View Source

Provides functions for generating Datastar action expressions.

These functions generate Datastar-compatible action strings for use in data-on:* attributes in your templates.

The generated expressions reference $session_id and $event_path Datastar signals, which are automatically initialized by PhoenixDatastar.DefaultHTML (or your custom html_module). This means event/1,2 works in any component without needing to pass session_id or event_path through assigns.

Requirements

  • A <meta name="csrf-token" content="..."> tag must be present in your layout (Phoenix includes this by default)

Summary

Functions

Generates a Datastar @post action expression for triggering server events.

Functions

event(event_name, opts \\ nil)

Generates a Datastar @post action expression for triggering server events.

The generated expression references $event_path and $session_id signals, which Datastar resolves client-side. This means it works in any component regardless of nesting depth — no need to pass framework assigns through.

Parameters

  • event_name - The event name to trigger on the server
  • opts - Optional additional options to pass in the request body (as a string)

Examples

# Simple event
<button data-on:click={event("increment")}>+1</button>

# Event with options
<button data-on:click={event("toggle_code", "name: 'counter'")}>Toggle</button>

# With signals
<button data-on:click={event("update", "value: $count")}>Update</button>