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
Hi,
I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
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 will often find my self writing things similar to:
case some_value do
nil -> something()
"" -> something()
_ -> somethi...
New
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
Credo is smart enough to check for (something like) this:
assert length(the_list) == 0
with this response:
Checking if an enum is empt...
New
In the Dialyzer docs ( dialyzer — OTP 29.0.2 (dialyzer 6.0.1) ), there is a way to turn off a specific warning for a function:
-dialyzer...
New
Hello!
Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
Other popular topics
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including.
What is Phoenix LiveV...
New
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
Hey all,
I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
What learn first? Rust or Elixir
Hi Elixir community!
I’m here because i want learn a new language. I’m a junior developer and mainly i ...
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
What is the proper way to load a module from a file in to IEX?
In the python world, doing something like this pretty standard:
from ....
New
I would like to know what is the best IDE for elixir development?
New
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex









