jgonet
Phoenix crashing after embedding `case` in template
Hey,
I recently started building my first Phoenix app, it’ll be a YT downloader.
Design
So, when I started implementing searching I realised that you can search for playlist, channels and videos.
I thought about two designs:
- create universal thumbnail template and embed switching between types in view module.
- create 3 separate thumbnails and use case to get type from received API response, rendering appropriate partial template
The problem
I went with 2nd solution, but after writing it Phoenix crashes. Here’s the code:
<%= for item <- @search_results["items"] do %>
<% case kind(item["id"]) do %>
<% :playlist -> render "playlist_thumbnail.html", data: extract_video_data(item), conn: @conn %>
<% :video -> render "video_thumbnail.html", data: extract_video_data(item), conn: @conn %>
<% end %>
<% end %>
def kind(%{"kind" => "youtube#playlist"}), do: :playlist
def kind(%{"kind" => "youtube#video"}), do: :video
def kind(_), do: :error
Error:
== Compilation error in file lib/yt_downloader_web/views/search_view.ex ==
** (SyntaxError) lib/yt_downloader_web/templates/search/search.html.eex:3: syntax error before: ‘->’
(eex) lib/eex/compiler.ex:45: EEx.Compiler.generate_buffer/4
(eex) lib/eex/compiler.ex:54: EEx.Compiler.generate_buffer/4
(phoenix) lib/phoenix/template.ex:378: Phoenix.Template.compile/2
(phoenix) lib/phoenix/template.ex:186: anonymous fn/3 in Phoenix.Template.“MACRO-before_compile”/2
(elixir) lib/enum.ex:1899: Enum.“-reduce/3-lists^foldl/2-0-”/3
(phoenix) expanding macro: Phoenix.Template.before_compile/1
lib/yt_downloader_web/views/search_view.ex:1: YtDownloaderWeb.SearchView (module)
(elixir) lib/kernel/parallel_compiler.ex:198: anonymous fn/4 in Kernel.ParallelCompiler.spawn_workers/6
Questions
- Did I missed something in
case? - Is there simpler or more Phoenixy way to do this?
EDIT:
I got it working by using:
<%= for item <- @search_results["items"] do %>
<%= case kind(item["id"]) do %>
<% :playlist -> %> <%= render "playlist_thumbnail.html", data: extract_video_data(item), conn: @conn %>
<% :video -> %> <%= render "video_thumbnail.html", data: extract_video_data(item), conn: @conn %>
<% end %>
<% end %>
But I still dunno why this is working and why earlier code wasn’t working. Anybody?
Marked As Solved
peerreynders
You’re the one with access to the source code - tried this yet?
<%= for item <- @search_results["items"] do
case kind(item["id"]) do
:playlist ->
render "playlist_thumbnail.html", data: extract_video_data(item), conn: @conn
:video ->
render "video_thumbnail.html", data: extract_video_data(item), conn: @conn
end
end %>
or
<%= for item <- @search_results["items"] do %>
<%= case kind(item["id"]) do
:playlist ->
render "playlist_thumbnail.html", data: extract_video_data(item), conn: @conn
:video ->
render "video_thumbnail.html", data: extract_video_data(item), conn: @conn
end %>
<% end %>
Also Liked
peerreynders
In this case I was just extrapolating from the example:
<%= if true do %>
It is obviously true
<% else %>
This will never appear
<% end %>
Knowing that everything in Elixir is an expression if true evaluates to "It is obviously true". If I’m not mistaken that could be rewritten as:
<%= if true do
"It is obviously true"
else
"This will never appear"
end %>
so the separate <% %> are just there to keep the quotes around the strings out of the template.
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









