PhoenixDatastar.Router (PhoenixDatastar v0.1.12)

Copy Markdown View Source

Router macros for PhoenixDatastar.

Usage

import PhoenixDatastar.Router

scope "/", MyAppWeb do
  pipe_through :browser

  # Stateless view (use PhoenixDatastar)
  datastar "/counter", CounterStar

  # Live view (use PhoenixDatastar, :live)
  datastar "/multiplayer", MultiplayerStar

  # With custom HTML module
  datastar "/custom", CustomStar, html_module: MyAppWeb.CustomHTML
end

datastar/3 generates per-view routes:

  • GET /counter - renders the initial page
  • POST /counter/_event/:event - handles events
  • GET /counter/stream - SSE stream endpoint (live views only)

Configuration

Set a default HTML module in your config:

config :phoenix_datastar, :html_module, MyAppWeb.DatastarHTML

Or pass html_module option per-route to override.

Summary

Functions

Defines routes for a PhoenixDatastar view.

Functions

datastar(path, view, opts \\ [])

(macro)

Defines routes for a PhoenixDatastar view.

The macro auto-detects whether the view is stateless (use PhoenixDatastar) or live (use PhoenixDatastar, :live) and generates appropriate routes.

Options

  • :html_module - The Phoenix HTML module to use for rendering the mount template. Defaults to Application.get_env(:phoenix_datastar, :html_module).