jgonet

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:

  1. create universal thumbnail template and embed switching between types in view module.
  2. 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

  1. Did I missed something in case?
  2. 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

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

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.

Where Next?

Popular in Questions Top

sergio
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
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
johnnyicon
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
nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; somethi...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call t...
New

Other popular topics Top

Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30877 112
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

We're in Beta

About us Mission Statement