phoenix_live_view v0.7.0 Phoenix.LiveView.Helpers View Source
A collection of helpers to be imported into your views.
Link to this section Summary
Functions
Renders a Phoenix.LiveComponent
within a parent LiveView.
Returns the flash message from the LiveView flash assign.
Generates a link that will patch the current LiveView.
Generates a link that will redirect to a new LiveView.
Renders a LiveView within an originating plug request or within a parent LiveView.
Provides ~L
sigil with HTML safe Live EEx syntax inside source files.
Link to this section Functions
live_component(socket, component, assigns \\ [], do_block \\ [])
View Source (macro)Renders a Phoenix.LiveComponent
within a parent LiveView.
While LiveView
s can be nested, each LiveView starts its
own process. A LiveComponent provides similar functionality
to LiveView, except they run in the same process as the
LiveView
, with its own encapsulated state.
LiveComponent comes in two shapes, stateful and stateless.
See Phoenix.LiveComponent
for more information.
Examples
All of the assigns
given are forwarded directly to the
live_component
:
<%= live_component(@socket, MyApp.WeatherComponent, id: "thermostat", city: "Kraków") %>
Note the :id
won't necessarily be used as the DOM ID.
That's up to the component. However, note the :id
has
a special meaning: whenever an :id
is given, the component
becomes stateful. Otherwise, :id
is always set to nil
.
Returns the flash message from the LiveView flash assign.
Examples
<p class="alert alert-info"><%= live_flash(@flash, :info) %></p>
<p class="alert alert-danger"><%= live_flash(@flash, :error) %></p>
Generates a link that will patch the current LiveView.
When navigating to the current LiveView, c:handle_params/3
is
immediately invoked to handle the change of params and URL state.
Then the new state is pushed to the client, without reloading the
whole page. For live redirects to another LiveView, use
live_redirect/2
.
Options
:to
- the required path to link to.:replace
- the flag to replace the current history or push a new state. Defaultsfalse
.
All other options are forwarded to the anchor tag.
Examples
<%= live_patch "next", to: Routes.live_path(@socket, MyLive, @page + 1) %>
<%= live_patch to: Routes.live_path(@socket, MyLive, dir: :asc), replace: false do %>
Sort By Price
<% end %>
Generates a link that will redirect to a new LiveView.
The current LiveView will be shut down and a new one will be mounted
in its place, without reloading the whole page. This can
also be used to remount the same LiveView, in case you want to start
fresh. If you want to navigate to the same LiveView without remounting
it, use live_patch/2
instead.
Options
:to
- the required path to link to.:replace
- the flag to replace the current history or push a new state. Defaultsfalse
.
All other options are forwarded to the anchor tag.
Examples
<%= live_redirect "next", to: Routes.live_path(@socket, MyLive, @page + 1) %>
<%= live_redirect to: Routes.live_path(@socket, MyLive, dir: :asc), replace: false do %>
Sort By Price
<% end %>
Renders a LiveView within an originating plug request or within a parent LiveView.
Options
:session
- the map of extra session data to be serialized and sent to the client. Note all session data currently in the connection is automatically available in LiveViews. You can use this option to provide extra data. Also note the keys in the session are strings keys, as a reminder that data has to be serialized first.:container
- the optional tuple for the HTML tag and DOM attributes to be used for the LiveView container. For example:{:li, style: "color: blue;"}
. By default it uses the module definition container. See the "Containers" section for more information.:id
- both the DOM ID and the ID to uniquely identify a LiveView. One:id
is automatically generated when rendering root LiveViews but it is a required option when rendering a child LiveView.:router
- an optional router that enables this LiveView to perform live navigation. Only a single LiveView in a page may have the:router
set. LiveViews defined at the router with thelive
macro automatically have the:router
option set.
Examples
# within eex template
<%= live_render(@conn, MyApp.ThermostatLive) %>
# within leex template
<%= live_render(@socket, MyApp.ThermostatLive, id: "thermostat") %>
Containers
When a LiveView
is rendered, its contents are wrapped in a container.
By default, said container is a div
tag with a handful of LiveView
specific attributes.
The container can be customized in different ways:
You can change the default
container
onuse Phoenix.LiveView
:use Phoenix.LiveView, container: {:tr, id: "foo-bar"}
You can override the container tag and pass extra attributes when calling
live_render
(as well as on yourlive
call in your router):live_render socket, MyLiveView, container: {:tr, class: "highlight"}
Provides ~L
sigil with HTML safe Live EEx syntax inside source files.
iex> ~L"""
...> Hello <%= "world" %>
...> """
{:safe, ["Hello ", "world", "\n"]}