AshDispatch.Event.Interpolation (AshDispatch v0.5.1)

View Source

Handles variable interpolation in DSL content strings.

Supports {{variable}} syntax for interpolating values from:

  1. Context data (ctx.data.variable)
  2. Template assigns from prepare_template_assigns/2

Examples

# In DSL:
content do
  subject "Order #{{order_number}} created"
  notification_message "Your order for {{total_items}} items is ready"
end

# In event module:
def prepare_template_assigns(context, channel) do
  %{
    order_number: format_order_id(context.data.order),
    total_items: length(context.data.order.items)
  }
end

Interpolation Rules

  1. Variables are looked up in template assigns first
  2. Fallback to context.data with same key
  3. Missing variables render as empty string (no errors)
  4. Nested access is supported with dot notation: {{project.name}}

Summary

Functions

Interpolates {{variable}} placeholders in a string.

Validates interpolation string for valid variable names.

Functions

interpolate(string, context, channel, event_module)

@spec interpolate(
  String.t() | nil,
  AshDispatch.Context.t(),
  AshDispatch.Channel.t(),
  module()
) ::
  String.t()

Interpolates {{variable}} placeholders in a string.

Examples

iex> context = %Context{data: %{user: %{name: "John"}}}
iex> Interpolation.interpolate("Hello {{user_name}}", context, channel, event_module)
"Hello John"

iex> Interpolation.interpolate("No variables here", context, channel, event_module)
"No variables here"

validate(string)

@spec validate(String.t()) :: {:ok, [String.t()]} | {:error, String.t()}

Validates interpolation string for valid variable names.

Returns {:ok, variables} or {:error, reason}.

Examples

iex> Interpolation.validate("Order #{{order_number}}")
{:ok, ["order_number"]}

iex> Interpolation.validate("Invalid {{123invalid}}")
{:error, "Invalid variable name: 123invalid"}