vekvek
How to use slots in <.live_component>?
I recently started exploring LiveView.
I can’t figure out how to pass custom named slots into <.live_component>.
Here is my code:
<.live_component
id="modal"
module={Modal}
>
<:header>
<p>Welcome from header</p>
</:header>
<:content>
<p>Welcome</p>
</:content>
</.live_component>
Modal component:
def render(assigns) do
assigns =
assigns
|> assign_new(:header, fn -> [] end)
|> assign_new(:content, fn -> [] end)
~H"""
<div class="bg-red-500" id={@id}>
<div class="border-b-2 border-gray-300 bg-blue-500">
<%= render_block(@header) %>
</div>
<%= render_block(@content) %>
</div>
"""
end
Error I got:
key :header not found in: %{__changed__: %{flash: true, id: true, inner_block: true}, flash: %{}, id: "modal-1", inner_block: #Function<8.90377761/2 in LiveviewPlaygroundWeb.TestLive.render/1>, myself: %Phoenix.LiveComponent.CID{cid: 1}, socket: #Phoenix.LiveView.Socket<id: "phx-F0LTuDdKH6Jl5wOC", endpoint: LiveviewPlaygroundWeb.Endpoint, view: LiveviewPlaygroundWeb.TestLive, parent_pid: nil, root_pid: nil, router: LiveviewPlaygroundWeb.Router, assigns: #Phoenix.LiveView.Socket.AssignsNotInSocket<>, transport_pid: nil, ...>}
Thanks!
Marked As Solved
fceruti
[edit] False: LiveComponents don’t have slots, only components.
You’d need to so something like:
<.live_component
id="modal"
module={Modal}
>
<.page_component>
<:header>
<p>Welcome from header</p>
</:header>
<:content>
<p>Welcome</p>
</:content>
</.page_component>
</.live_component>
2
Also Liked
Eiji
Are you sure?
Slots
LiveComponent can also receive slots, in the same way as a
Phoenix.Component:
I just tried it in example script:
defmodule MyTable do
use Phoenix.LiveComponent
def render(assigns) do
~H"""
<table>
<tr>
<%= for col <- @col do %>
<th><%= col.label %></th>
<% end %>
</tr>
<%= for row <- @rows do %>
<tr>
<%= for col <- @col do %>
<td><%= render_slot(col, row) %></td>
<% end %>
</tr>
<% end %>
</table>
"""
end
end
defmodule SamplePhoenix.SampleLive do
use Phoenix.LiveView
# …
def mount(_params, _session, socket) do
users = [
%{address: "john.doe@example.com", name: "John Doe"},
%{address: "sample@example.com", name: "Sample"}
]
{:ok, assign(socket, :users, users)}
end
# …
def render(assigns) do
~H"""
<.live_component id="my-table" module={MyTable} rows={@users}>
<:col :let={user} label="Name">
<%= user.name %>
</:col>
<:col :let={user} label="Address">
<%= user.address %>
</:col>
</.live_component>
"""
end
end
4
Popular in Questions
In Ruby, I can go:
User.find_by(email: "foobar@email.com").update(email: "hello@email.com")
How can I do something similar in Elixir?
...
New
I have an umbrella app.
Some of the apps inside depend on other apps in the umbrella, unsurprisingly.
I’m writing a test for one of the...
New
Hello all!
I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
Hi all,
I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage.
I’m trying to use Postgres...
New
I have followed this StackOverflow post to install the specific version of Erlang.
And When I am running mix ecto.setup then getting fol...
New
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this:
...
New
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this
"1000"
What is the ...
New
Hi!
In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir?
Searched the docs for ip address and the web, no good results.
Thanks!
New
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar.
I p...
New
Other popular topics
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode.
The solution seems to be, in a hyphena...
New
I have an umbrella app.
Some of the apps inside depend on other apps in the umbrella, unsurprisingly.
I’m writing a test for one of the...
New
Hi,
I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
What is the idiomatic way of matching for not nil in Elixir?
E.g.,
First way:
defp halt_if_not_signed_in(conn, signed_in_account) when...
New
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
Using vs code and installed ElixirLS: support and debugger.
And I got an error popped up on start up says
Failed to run ‘elixir’ comma...
New
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine)
This is a plugin that adds support for Elixir to JetBrains IntelliJ...
New
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar.
I p...
New
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!)
This post collects co...
New
Hello!
Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








