PhoenixLiveViewExt.MultiRender (phoenix_live_view_ext v1.2.2) 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
..
When using MultiRender with a live view instance, there may be a function naming conflict between the MultiRender generated render/2 function and the render functions imported or used from Phoenix.View.
As of version 1.1.1, the library supports providing a different name for the render/2 function by defining an otherwise optional @multi_render_fun module attribute as shown in the example below.
..
require PhoenixLiveViewExt.MultiRender
@multi_render_fun :prerender
@before_compile PhoenixLiveViewExt.MultiRender
def my_render( assigns) do
prerender( "my_component_enjoy.html", assigns)
end
..
In addition, if a different function name is specified, the MultiRender before_compile macro will no longer require a render/1 function to be present in the module, hence the above my_render/1 function name.