Lily
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?
Marked As Solved
blatyo
Conduit Core Team
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
Also Liked
blatyo
Conduit Core Team
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
peerreynders
Lets say you have
defmodule Demo do
def rut?(rut) when is_binary(rut)do
rut
|> clean()
|> valid?()
end
def rut?(_) do
false
end
defp clean(value),
do: Regex.replace(~r/[^0-9K]/, String.upcase(value), "")
defp valid?(rut) do
reversed = String.reverse(rut)
if verify(reversed) do
{digits, check} = split(reversed)
check == checksum(digits)
else
false
end
end
defp verify(reversed),
do: Regex.match?(~r/^(\d|K)\d{7,8}$/, reversed)
defp split(reversed) do
[check_digit | rest] = String.graphemes(reversed)
digits = Enum.map(rest, &(String.to_integer(&1)))
check =
case check_digit do
"K" -> 10
_ -> String.to_integer(check_digit)
end
{digits, check}
end
defp checksum(digits) do
result =
digits
|> length()
|> make_coefficients()
|> Enum.zip(digits)
|> Enum.reduce(0,&to_sum/2)
|> Integer.mod(11)
case (11 - result) do
11 -> 0
n -> n
end
end
defp make_coefficients(amount) do
(2..7)
|> Stream.cycle()
|> Enum.take(amount)
end
def to_sum({a,b}, sum),
do: a * b + sum
end
IO.inspect(Demo.rut?("30.686.957-4")) # true
IO.inspect(Demo.rut?("306869574")) # true
IO.inspect(Demo.rut?("30.686.954-K")) # true
IO.inspect(Demo.rut?("30686954K")) # true
IO.inspect(Demo.rut?("3068695K4")) # false
IO.inspect(Demo.rut?("1938909K")) # true
IO.inspect(Demo.rut?("19389093")) # false
IO.inspect(Demo.rut?("0")) # false
IO.inspect(Demo.rut?("")) # false
IO.inspect(Demo.rut?(nil)) # false
IO.inspect(Demo.rut?(306869574)) # false
then you should be able to:
defmodule Medica.AppointmentView do
import Demo, only: [ rut?: 1 ]
# ...
end
and then
<%= if rut?(appointment.rut_paciente) do %>
should work.
4
peerreynders
1.3 only released about a year ago (which introduced the phx mix tasks) - the technique was in use well before that so I doubt that it will make a difference whether you used mix phx.new or mix phoenix.new - it should work in either case.
- Functions defined inside or imported into the
Viewmodule are available inside the rendered templates. - In general one should prefer to not have detailed logic inside the template itself. It’s usually better to put that logic into a function inside the associated
Viewmodule (orimportit into theViewmodule from another module) - and then just use the function inside the template.
2
Popular in Questions
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
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
Hi All,
I set a environment variables in dev.exs , like below code.
when i start server, how can i set the ${enable} value?
thanks.
d...
New
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
Hi all,
I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage.
I'm trying to use Postg...
New
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
I have followed this StackOverflow post to install the specific version of Erlang.
And When I am running mix ecto.setup then getting fol...
New
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
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 th...
New
Other popular topics
Hi,
I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
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
Hi, I have the following ecto query: post_query = from p in Post, where: p.id == ^id, join: c in assoc(p, :comments), join: u in assoc(c,...
New
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
New
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
New
Hello, I have map which I want to convert it to string like this:
the map:
%{last_name: "tavakkoli", name: "shahryar"}
the string I ne...
New
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!)
This post collects co...
New








