How do I put an IF statement in a templates file in Elixir Phoenix?

In templates/appointment/index.html.eex:

<%= for appointment <- @appointments do %>
    <tr>
      <td><%= appointment.rut_paciente%></td>
      <td><%= appointment.fecha %></td>
      <td><%= appointment.id_medico %></td>
      <td><%= appointment.bloque %></td>

I’m trying to show only some fields from the table appointment, in this case, only those where “appointment.rut_paciente” is not 0. How can I do it?

<%= if appointment.rut_paciente != 0 %>
  HTML
<% end %>

Thanks! But I already tried that and it doesn’t work :c

<%= for appointment <- @appointments do %>
  <%= if appointment.rut_paciente != 0 %>
    <tr>
      <td><%= appointment.rut_paciente%></td>
      <td><%= appointment.fecha %></td>
      <td><%= appointment.id_medico %></td>
      <td><%= appointment.bloque %></td>
  <% end %>


<%= if appointment.rut_paciente != 0 do %>

i.e. the do is missing

1 Like

Same error :c Thanks anyway :slight_smile:

Do you have a module called Medica.AppointmentView? You should be getting a compile error if the EEx is invalid. I would add the do that peerreynders mentioned and do a mix clean, followed by a mix compile. If there’s an error you should get a compiler error.

That error usually means that you have a compilation error. You should look at the output from your Phoenix server for the details. You can also try restarting your Phoenix server since that will nicely print out any compilation errors.

Thanks! I’ll paste the compilation error:

== Compilation error in file web/views/appointment_view.ex ==
** (SyntaxError) web/templates/appointment/index.html.eex:18: syntax error before: do__EEX__
(eex) lib/eex/compiler.ex:96: 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
web/views/appointment_view.ex:1: Medica.AppointmentView (module)
(elixir) lib/kernel/parallel_compiler.ex:198: anonymous fn/4 in Kernel.ParallelCompiler.spawn_workers/6

Yes, there is. And it is a compilation error, I hope we can solve this.

Paste the lines around that line in the template

<h2 align="center">Modo Administrador</h2>
<h2>Lista de Horas Pedidas</h2>

<table class="table">
  <thead>
    <tr>
      <th>R.U.T. Paciente</th>
      <th>Fecha</th>
      <th>ID Médico</th>
      <th>Bloque</th>

      <th></th>
    </tr>
  </thead>
  <tbody>

<%= for appointment <- @appointments do %>
  <%= if appointment.rut_paciente != 0 do%>
    <tr>
      <td><%= appointment.rut_paciente%></td>
      <td><%= appointment.fecha %></td>
      <td><%= appointment.id_medico %></td>
      <td><%= appointment.bloque %></td>
  <% end %>

      <td class="text-right">
        <span><%= link "👁", to: appointment_path(@conn, :show, appointment), class: "btn btn-default btn-xs" %></span>
        <span><%= link "🖉", to: appointment_path(@conn, :edit, appointment), class: "btn btn-default btn-xs" %></span>
        <span><%= link "✖", to: appointment_path(@conn, :delete, appointment), method: :delete, data: [confirm: "¿Está Seguro?"], class: "btn btn-danger btn-xs" %></span>
      </td>
    </tr>
<% end %>
  </tbody>
</table>

<span><%= link "Pedir Hora", to: appointment_path(@conn, :new), class: "btn btn-success" %></span>
<span><%= link "Médicos", to: medico_path(@conn, :index), class: "btn btn-primary" %></span>
<span><%= link "Restricción de Horas", to: bnodisponibles_path(@conn, :index), class: "btn btn-primary" %></span>
<span><%= link "Regresar", to: page_path(@conn, :index), class: "btn btn-danger" %></span>

which is line 18?

This one!
<%= if appointment.rut_paciente != 0 do%>
Thanks for the help <3

Not sure if this is it, but add a space after do

Thanks, it actually compiled now :smiley:
But it still doesn’t do what I need, I don’t know why :cry:
I need the column that has a 0 in Rut Paciente to not be seen:

What is the type of the data there? String or Number?

Since it didn’t hide, I suspect you want appointment.rut_paciente != "0"

3 Likes

IT WOOOORKED!!! Oh my God, I love you <3
I previously tried ‘0’, and it didn’t work, but now I tried “0” as you said and it worked!!! :blush:

1 Like

In elixir '0' is a character list and "0" is a string/binary. Unless you’re using an erlang api that asks for it, you pretty much always want a string/binary.

It’s also worth noting that you can filter items in your for comprehension. So, you could just have this line:

<%= for appointment <- @appointments, 
    appointment.rut_paciente != 0 do %>
  <tr> ... </tr>
<% end %>
4 Likes

Excuse me, @blatyo: Is there any way to manipulate each character of that string? Something like:
<%= if appointment.rut_paciente[0]!= “0” do %>
Please, help :sob: