jgonet

jgonet

Popcorn Core Team

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?

First 5 of 5 Posts Switch mode

peerreynders

peerreynders

EEx - Tags:

All expressions that output something to the template must use the equals sign (=). Since everything in Elixir is an expression, there are no exceptions for this rule.

i.e. case/2 is an expression, not some kind of a switch statement.

jgonet

jgonet OP

Popcorn Core Team

Okey, but even with double <%= %> in case and comprehension it crashes. I didn’t go digging into macros yet, but if I understand this correctly case/2 evaluates to matching expression or nil. Doesn’t that mean I should be able to connect match -> and render call into one <% %> as this is in body of case?

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 %>
jgonet

jgonet OP

Popcorn Core Team

Oooh, I was convinced that I have to use <% %> per line.

Thank you so much, this is working.

Sidenote:
As a beginner I’m kinda confused in official guide, I couldn’t find anywhere guide that tell where I should put what. For instance, entire folder structure, design guide, etc. There’re bits of this knowledge shattered in docs, but some page gathering this would be very helpful.

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.

— All posts loaded —

Where Next?

Trending in Questions Top

jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement