APB9785
How to properly use case statements in EEx (LiveView)
I have code in page_live.html.leex that looks like this:
<div>
<%= case @nav do %>
<% :post -> %>
<div>Post view goes here</div>
<% :main -> %>
<div>Main view goes here</div>
<% :board -> %>
<div>Board view goes here</div>
<% end %>
</div>
But I get a compilation error like this:
unexpected operator ->. If you want to define multiple clauses, the first expression must use ->. Syntax error before: ‘->’
What am I doing wrong? I searched the documentation and the forum but could not find any solution that would compile.
Most Liked
benwilson512
Weird, we do exactly this a lot. And @kokolegorille the issue with content_tag is that it isn’t really usable with live view. You can’t for example do live_component inside of it and have it work correctly.
<%= case @event do %>
<% %{type: :research_status_changed, data: status_change} -> %>
stuff
<% %{type: :labeling, data: %{comment: comment, user: user}} = event -> %>
stuff
<% end %>
This is pulled straight out of our codebase.
Can you provide a reproduceable example?
patrickdm
Just a guess but… did you try without this html comment?
kokolegorille
You should use a helper for this… and try to remove any logic from template.
<%= display_nav(@nav) %>
and in page_view.ex
def display_nav(:post) do
content_tag(:div) do
"whatever"
end
end
# etc.
Popular in Questions
Other popular topics
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








