AshDispatch.VariableInterpolator (AshDispatch v0.5.0)
View SourceInterpolates variables in template strings.
Replaces {{variable}} placeholders with actual values from event data.
Examples
Basic interpolation:
iex> interpolate("Order #{{id}}", %{order: %{id: 123}}, :order)
"Order #123"Nested attributes (preloaded relationships):
iex> interpolate("Hello {{user.name}}", %{order: %{user: %{name: "Alice"}}}, :order)
"Hello Alice"Flattened keys (user.name becomes user_name):
iex> interpolate("Hello {{user_name}}", %{order: %{user: %{name: "Alice"}}}, :order)
"Hello Alice"Missing values become empty string:
iex> interpolate("Value: {{missing}}", %{order: %{}}, :order)
"Value: "Variable Syntax
Variables use double curly braces: {{variable_name}}
Direct attributes:
{{id}}→ resource.id{{status}}→ resource.status{{total}}→ resource.total
Nested attributes (dot notation):
{{user.name}}→ resource.user.name{{user.email}}→ resource.user.email{{organization.name}}→ resource.organization.name
Flattened keys (underscore notation):
{{user_name}}→ resource.user.name{{user_email}}→ resource.user.email{{organization_name}}→ resource.organization.name
Resource Key
The resource_key parameter specifies which key in the data map contains
the main resource record:
data = %{order: %Order{id: 123}, user: %User{name: "Alice"}}
interpolate("Order {{id}}", data, :order)
# Uses data.order for variable lookupPreloading
For nested attributes to work, relationships must be preloaded:
dispatch do
event :created,
load: [:user, :organization], # Preload relationships
content: [
notification_message: "{{user_name}} created order in {{organization_name}}"
]
endType Conversion
Values are converted to strings:
- Atoms:
:active→ "active" - Numbers:
123→ "123" - Booleans:
true→ "true" - DateTime:
~U[2025-01-01 10:00:00Z]→ "2025-01-01 10:00:00Z" - Nil:
nil→ "" (empty string)
Safety
- Missing variables become empty strings (no errors)
- Nil values become empty strings
- Handles missing relationships gracefully
- Does not execute code (just string replacement)
Summary
Functions
Interpolates variables in a template string.
Functions
Interpolates variables in a template string.
Parameters
template- String template with{{variable}}placeholdersdata- Map containing event data (resource records)resource_key- Atom key for the main resource in data map
Returns
String with variables replaced by actual values
Examples
iex> interpolate("Order #{{id}}", %{order: %{id: 123}}, :order)
"Order #123"
iex> interpolate("{{user.name}}", %{ticket: %{user: %{name: "Alice"}}}, :ticket)
"Alice"
iex> interpolate("{{missing}}", %{order: %{}}, :order)
""