Formats Elixir values for HEEx template attributes.
Handles the mapping between in-memory attr values and their HEEx text
representation. Phoenix function component attrs use the Elixir atom
name directly (underscores), so :variant becomes variant=....
Special rules:
nilvalues are omitted (returns nil).truebooleans emit the bare attr name:disabled.falsebooleans are omitted.- Atoms are wrapped in curlies:
variant={:destructive}. - Strings are double-quoted:
label="Save". - Numbers are wrapped in curlies:
count={3}. - Lists and maps use
inspect/1inside curlies. - The
:classattr always renders as a plain HTML string attribute.
Summary
Functions
Format a single attr key-value pair for HEEx output.
Format a map of attrs into a single space-separated string suitable for insertion into an opening HEEx tag.
Functions
Format a single attr key-value pair for HEEx output.
Returns the formatted attribute string, or nil if the attr should be
omitted from the output (nil value, false boolean).
Examples
iex> AttrFormatter.format(:disabled, true)
"disabled"
iex> AttrFormatter.format(:variant, :destructive)
"variant={:destructive}"
iex> AttrFormatter.format(:label, "Save")
~s(label="Save")
iex> AttrFormatter.format(:hidden, false)
nil
iex> AttrFormatter.format(:tooltip, nil)
nil
Format a map of attrs into a single space-separated string suitable for insertion into an opening HEEx tag.
Keys are sorted alphabetically for deterministic output.
Examples
iex> AttrFormatter.format_attrs(%{variant: :outline, disabled: true})
"disabled variant={:outline}"
iex> AttrFormatter.format_attrs(%{})
""