Conditionally Render a Non-blank HEEx Slot
There are situations where a placeholder value should be displayed in
place of a blank HEEx slot. The :empty CSS pseudo-class doesn’t work
as it doesn’t tolerate whitespace at all. There’s :blank which
tolerates whitespace but it’s still experimental and in practice not
supported by any major browser. If CSS can’t do it, the server can.
I encountered the pseudo-classes’ limitations when I wrote a grid component that accepted one or more cell slots and arranged them in a specific way. When a cell slot’s content was blank, meaning empty or all whitespace, a dash had to be displayed as a placeholder. I could’ve used a conditional expression in the template but I was looking for a more generic way to solve the problem.
There’s an article that tries to demonstrate how to check if a slot is
blank. I find the approach cumbersome because it pattern matches on
the structure of the slot assign. In my experiments, the proposed
solution fell apart as soon as slot_empty? was called with a slot
from a list of slots. The fragment below makes the function report
that both slots are not blank when in fact they are.
<.grid>
<:cell></:cell>
<:cell></:cell>
</.grid>
The approach I came up with is in the spirit of “easier to ask
forgiveness than permission”. The following macro unconditionally
renders a slot, exactly as Phoenix.Component.render_slot/2 would,
but returns it to the caller only if the result is not blank.
defmacro maybe_render_slot(slot, argument \\ nil) do
changed =
if Macro.Env.has_var?(__CALLER__, {:changed, Phoenix.LiveView.Engine}) do
Macro.var(:changed, Phoenix.LiveView.Engine)
end
quote do
rendered =
unquote(changed)
|> Phoenix.Component.__render_slot__(unquote(slot), unquote(argument))
|> Phoenix.HTML.Safe.to_iodata()
blank? =
rendered
|> :re.run(~r/^[[:space:]]*$/.re_pattern)
|> then(&match?({:match, _}, &1))
if not blank? do
Phoenix.HTML.raw(rendered)
end
end
end
I didn’t ask for permission to build upon render_slot/2’s internals:
the changed variable and the __render_slot__ pseudo-private
function, so it’s only right to ask for forgiveness.
Forgiven or not, the benefits of reusing the internals are real. The
changed variable allows the macro to take advantage of the
change-tracking mechanism when used inside a HEEx template. Outside of
a template, changed is bound to nil so naturally there’s no change
tracking but the macro can still be used. Piggy-backing on
__render_slot__ ensures the same rendering behaviour as
render_slot/2. Rendering compatibility is a must because, after all,
this approach only adds a conditional expression to the slot-rendering
function provided by Phoenix.
Getting back to the grid example above, I can use the macro to conditionally render a non-blank slot and use a sibling element as placeholder content.
<p :for={cell <- @cell}>
<span :if={rendered = maybe_render_slot(cell)}>{rendered}</span>
<span class="hidden only:inline">{@placeholder}</span>
</p>
A binding expression returns the value it binds. If rendered is
nil, the condition is falsy and the first span is not rendered.
The second span is hidden by default and visible only if it’s the
only child of the parent element, thus the hidden and only:inline
Tailwind classes.
Even though this approach works and was a good exercise, I don’t
advise you to use it in production. I can’t wait for browsers to
support the :blank CSS pseudo-class so I don’t have to resort to
such template gymnastics.