lcabrini
I have a schema with a status field, that can be :draft, :published, etc. In the index live view, I only want the delete link to be available for items that have not been published, that is they are :draft.
I thought this should be extremely simple:
<%= if article.status == :draft do %>
<:action :let={{id, article}}>
<.link
phx-click={JS.push("delete", value: %{id: article.id}) |> hide("##{id}")}
data-confirm="Are you sure?"
>
Delete
</.link>
</:action>
<% end %>
However, this tells me:
== Compilation error in file lib/foobar_web/live/article_live/index.ex ==
** (Phoenix.LiveView.Tokenizer.ParseError) lib/foobar_web/article_live/index.ex:37:11: invalid slot entry <:action>. A slot entry must be a direct child of a component
|
34 | Delete
35 | </.link>
36 | </:action>
37 | <% end %>
| ^
I don’t really get it. As far as I understand, it IS a direct child of a component, all that exists above it is a logic statement, a “preprocessor statement”, which should have been evaluated in an initial pass, or?
In any case, I accept that for some reason this doesn’t work.. But then, how would I achieve what I’m trying to do? It seems common enough that I imagine this should be easy to do. I can’t seem to find the right keywords to search on.
If I re-arrange it a bit so that the <% if … %> is embedded inside the <:action …>, then it does work. Is that the best way to do it?
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #security










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
matt-savvy
What happens if you do it like this:?
FlyingNoodle
The error message tells you the issue:
you need to put your conditional inside the slot.
LostKobrakai
or better on the slot using
:ifas suggested by @matt-savvy. that way the slot is not passed with empty content to the component.garrison
Tbh this is what convinced me that named slots are not a very good API. They’re a cool idea but they don’t hold up because they don’t compose properly. The introduction of
:ifand:foris, IMO, an ugly bandaid. Components are a more versatile primitive.But yeah, as mentioned above, you have to use
:if(or potentially:for) if you want to elide the slot itself from the output.Alternatively you could ditch slots and use a component like
<.article_action />and then your intuitive solution would actually work.lcabrini
This does look like the proper way to solve it.
lcabrini
Of course there was going to be a simple and logical solution. And of course my mind was not going to figure it out. Thanks.
lcabrini
Maybe I wrote a bit too soon.
Gives me:
undefined variable “article”. Which one get evaluated first, the:letor the:if? Or more to the point, how to I usearticlein the conditional?garrison
In the OP you are using an
articlefrom the outer scope and then rebinding the variable inside the:let. Where did the outerarticlego?This is kinda just proving my point, though. I have absolutely no idea how precedence is handled between
:letand:ifbecause it’s not real code, it’s a made up template language. So now we’re both confused.(Just gonna casually tag @bartblast (hi) because this is a pretty clear real-world example of why I am anti-template, which he may appreciate.)
lcabrini
Since this is just an experimental project where I’m trying different things out, I was going to try both solutions. I wanted to start with the one @matt-savvy proposed and then yours. Apart from the
:ifclause I added, this is standard index view thatphx.gen.livewould create. I’m guessing that the outer article “variable” is created by<.table>.The only way I can understand why the variable is not available to the
:ifwould be that it creates new scope that knows nothing about article. If that is the case, then I don’t understand what the use of the conditional clause would be.garrison
The
:ifsolution is still idiomatic even if I don’t (personally) like that it exists.But what I’m asking is, in the OP, what is the
articlein the if statement? We can’t see the rest of the code. Where does it come from?