krasenyp

krasenyp

You probably already know that <span>{nil}</span> in a HEEX template produces <span> </span> when rendered. I find this very inconvenient because I can’t conditionally display a placeholder content via the :empty CSS pseudo-class, as it doesn’t tolerate whitespace at all. I know, there’s :blank but it’s still experimental and it’s not supported by any browser.

I encountered an article which tries to demonstrate how to check if a slot is empty. The solution falls apart as soon as you call slot_empty? with a slot from a list of slots. The following fragment makes the function report that both slots are not empty while in fact they are.

<.some_component>
  <:item></:item>
  <:item></:item>
</.some_component>

The following macro is inspired by Phoenix.Component.render_slot/2 and actually renders the slot and checks if it’s all whitespace. If it is, then it returns nil, otherwise - the rendered slot, ready to be put in the template.

defmacro maybe_render_slot(slot, argument \\ nil) do
  # This is what `Phoenix.Component.render_slot/2` does with the twist
  # that our function can be used outside of a HEEX template.
  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
      # Using Erlang's API because it accepts IO lists and we can avoid
      # "materialising" the IO list to a binary. Of course, one can compile
      # the regular expression but I skipped it for brevity sake.
      |> :re.run("^\s*$")
      |> then(&match?({:match, _}, &1))

    if not blank? do
      Phoenix.HTML.raw(rendered)
    end
  end
end

Then, in a template, you can use the macro to conditionally render a non-blank slot and use a sibling element as a placeholder content.

<p :for={item <- @items}>
  <span :if={rendered = maybe_render_slot(item)}>{rendered}</span>
  <span class="hidden only:inline">{@placeholder}</span>
</p>

I hope someone finds this helpful. I can’t wait for the browsers to support the :blank CSS pseudo-class so we don’t need to do such template gymnastics.

Where Next? Top

Trending in Guides/Tuts Top

krasenyp
You probably already know that &lt;span&gt;{nil}&lt;/span&gt; in a HEEX template produces &lt;span&gt; &lt;/span&gt; when rendered. I fin...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews