Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I handle the empty case in Elixir/Phoenix templates?

In a template I have an iteration over some days in an itinerary:

<%= for day <- @days do %>
  <tr>
    <td><%= day.number %></td>
    <td><%= day.main_event_time %></td>
    <td><%= day.main_event %></td>
    <td><%= day.location %></td>
    <td><%= day.who %></td>
    <td><%= day.summary %></td>
    <td><%= day.lodging %></td>

    <td><%= link "Show", to: day_path(@conn, :show, day) %></td>
    <td><%= link "Edit", to: day_path(@conn, :edit, day) %></td>
    <td><%= link "Delete", to: day_path(@conn, :delete, day), method: :delete %></td>
  </tr>
<% end %>

I'd like have a different message come up in the case when there are no days yet. How can I do that?

like image 546
ijt Avatar asked Dec 06 '22 22:12

ijt


1 Answers

You can put this before or after your for comprehension:

<%= if Enum.empty?(@days) do %>
  No days yet.
<% end %>
like image 188
Patrick Oscity Avatar answered Jan 07 '23 04:01

Patrick Oscity