PhoenixLiveViewExt.MultiRender (phoenix_live_view_ext v1.0.1) View Source
The module provides a @before_compile macro enabling multiple template files per live component or live view. Each template file gets pre-compiled as a private render/2 function in its live component or live view module. This in turn enables (conditional) invocation from the render/1 functions.
The template files should share the component or live view folder and start with their respective underscored names.
In the example below we have a component that requires a different template when flagged for deletion (as part of, say, an appended container list) than the one used in the base-case scenario.
defmodule MyComponent do
use Phoenix.LiveComponent
require PhoenixLiveViewExt.MultiRender
@before_compile PhoenixLiveViewExt.MultiRender
@impl true
def render( %{ delete: :true} = assigns) do
render( "my_component_delete.html", assigns)
end
def render( assigns) do
render( "my_component_enjoy.html", assigns)
end
end
The component folder tree may look as follows:
my_app
lib
my_app_web
live
components
my_component.ex
my_component_delete.html.leex
my_component_enjoy.html.leex
..