Logo

dev-resources.site

for different kinds of informations.

Phoenix LiveView is slot empty?

Published at
12/10/2024
Categories
webdev
elixir
liveview
phoenix
Author
neophen
Categories
4 categories in total
webdev
open
elixir
open
liveview
open
phoenix
open
Author
7 person written this
neophen
open
Phoenix LiveView is slot empty?

Here's a simple function to check if the slot is empty, where empty can mean.

  1. No slot provided
  2. Slot provided but has only whitespace
defp slot_empty?(slot) do
  case slot do
    [] ->
      true

    slots when is_list(slots) ->
      not Enum.any?(slots, fn slot ->
        case slot do
          %{inner_block: inner_block} when is_function(inner_block) ->
            try do
              {:ok,
                inner_block.(%{}, nil)
                |> Phoenix.HTML.html_escape()}
            rescue
              _ -> :error
            end
            |> case do
              {:ok, html} ->
                html
                |> Phoenix.HTML.safe_to_string()
                |> String.trim()
                |> Kernel.!=("")

              _ ->
                true
            end

          _ ->
            false
        end
      end)

    _ ->
      true
  end
end
Enter fullscreen mode Exit fullscreen mode

Example usage:

slot :inner_block

@spec heading(assigns :: map()) :: Rendered.t()
def heading(assigns) do
  ~H"""
  <header :if={not slot_empty?(@inner_block)} class="grid gap-4 pb-8 text-left">
    <%= render_slot(@inner_block) %>
  </header>
  """
end

Enter fullscreen mode Exit fullscreen mode

Hope this helps.

phoenix Article's
30 articles in total
Favicon
Pseudolocalization in Phoenix with gettext_pseudolocalize
Favicon
Unlocking the Power of Elixir Phoenix and Rust: A Match Made for High-Performance Web Applications
Favicon
Sql commenter with postgrex
Favicon
Phoenix LiveView is slot empty?
Favicon
Bridging the Gap: Simplifying Live Component Invocation in Phoenix LiveView
Favicon
Find and Fix N+1 Queries Using AppSignal for a Phoenix App in Elixir
Favicon
Managing Distributed State with GenServers in Phoenix and Elixir
Favicon
Complete Guide: Setting up VS Code for Elixir and Phoenix Development
Favicon
A Complete Guide to Phoenix for Elixir Monitoring with AppSignal
Favicon
Better LiveView Hooks with Typescript
Favicon
Scaling Your Phoenix App in Elixir with FLAME
Favicon
Running Elixir Phoenix on Windows
Favicon
How to use gettext in phoenix?
Favicon
Custom Instrumentation for a Phoenix App in Elixir with AppSignal
Favicon
Building a Table of Contents Component for a Phoenix Blog
Favicon
Mastering Phoenix Framework - Part 2
Favicon
Mobile app development with LiveView Native and Elixir. Part - 3
Favicon
How to integrate Tabler Icons into your Phoenix project
Favicon
Mobile app development with LiveView Native and Elixir. Part - 2
Favicon
Mobile app development with LiveView Native and Elixir
Favicon
(Unofficial) Getting Started with Elixir Phoenix Guide
Favicon
Using Ecto (without Db) for validating Phoenix form
Favicon
API Prototypes with dbb: Another step to better prototypes
Favicon
Adding stream_async() to Phoenix LiveView
Favicon
SaladUI - Implement avatar component for Phoenix LiveView
Favicon
Connectivity status with Phoenix LiveView
Favicon
Taming data with Ecto.Enum and Ecto.Type
Favicon
ecto's cast/4 function explained
Favicon
Phoenix Liveview components for Shadcn UI
Favicon
How to run a local Phoenix app on another machine

Featured ones: