Overview
A function component is any function that receives an assigns map as an argument and returns a rendered struct built with the~H sigil.
Macros
sigil_H/2
The~H sigil for writing HEEx templates inside source files.
- Built-in handling of HTML attributes
- HTML-like notation for function components
- Compile-time validation of template structure
- Minimized data sent over the wire
- Out-of-the-box code formatting via
mix format
attr/3
Declares attributes for HEEx function components.The attribute name.
The attribute type:
:any, :string, :atom, :boolean, :integer, :float, :list, :map, :fun, {:fun, arity}, :global, or a struct module.Options:
:required- Marks attribute as required:default- Default value if not provided:examples- Example values for documentation:values- Exhaustive list of accepted values:doc- Documentation string
slot/3
Declares a function component slot.The slot name (use
:inner_block for the default slot).Options:
:required- Marks slot as required:validate_attrs- Validate slot attributes (default:true):doc- Documentation string
Code block containing
attr/3 calls for slot attributes.embed_templates/2
Embeds external template files into the module as function components.Wildcard pattern for files to embed.
Options:
:root- Root directory (default:__DIR__):suffix- String to append to function names
Functions
assign/3
Adds a key-value pair to assigns.The assigns map from a function component.
The assign key.
The assign value.
Updated assigns map.
assign_new/3
Assigns a key with value from function if one does not yet exist.The assigns map.
The assign key.
Function returning the value (0 or 1 arity).
update/3
Updates an existing key with a function.The assigns map.
The assign key to update.
Function receiving current value (and optionally assigns) returning updated value.
changed?/2
Checks if the given key changed in assigns.The assigns map.
The assign key to check.
true if the key changed, false otherwise.render_slot/2
Renders a slot entry with optional argument.The slot to render.
Optional argument passed to the slot.
Rendered slot content, or
nil if slot has no entries.to_form/2
Converts a data structure to aPhoenix.HTML.Form.
The source data for the form.
Options:
:as- Name prefix for form inputs:id- ID prefix for form inputs:errors- Keyword list of errors:action- Form action (:validate,:save, etc.)
Form struct for use in templates.
used_input?/1
Checks if the input field was used by the client.The form field to check.
true if field was focused, interacted with, or submitted.live_render/3
Renders a LiveView within a template.Connection or parent LiveView socket.
The LiveView module to render.
Options:
:session- Extra session data:container- Container tag and attributes:id- Unique DOM and LiveView ID (required for child LiveViews):sticky- Maintain across live redirects
assigns_to_attributes/2
Filters assigns as a keyword list for use in dynamic tag attributes.The assigns map.
List of keys to exclude.
Filtered assigns as keyword list.
Built-in Components
live_component/1
Renders aPhoenix.LiveComponent within a parent LiveView.
The LiveComponent module to render.
A unique identifier for the LiveComponent.
live_title/1
Renders a title with automatic prefix/suffix on@page_title updates.
A prefix added before the content.
A suffix added after the content.
The default title to use if inner block is empty.
form/1
Renders a form.The form source data.
The action to submit the form to (required for non-LiveView forms).
The prefix for input names and IDs.
CSRF token (auto-generated for POST actions).
The HTTP method (only used with
:action).Sets
enctype to multipart/form-data for file uploads.inputs_for/1
Renders nested form inputs for associations or embeds.The form field for the association or embed.
Upload Components
upload_errors/1
Returns errors for the upload as a whole.The upload configuration.
List of error atoms (e.g.,
:too_many_files).upload_errors/2
Returns errors for a specific upload entry.The upload configuration.
The upload entry.
List of error atoms (e.g.,
:too_large, :not_accepted).