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