Sebb
Can't use for-variable in heex - {if for_var == ...} - Protocol.UndefinedError
Why do I get Protocol.UndefinedError for this code?
~H"""
<%= for index <- [0, 1, 2] do %>
<div {if 1 == index, do: [selected: ""]} ><%= index %></div>
<% end %>
"""
(Protocol.UndefinedError) protocol Enumerable not implemented for nil of type Atom
elixir 1.12.2-otp-23 /home/sf/.tool-versions
erlang 23.3.4.4 /home/sf/.tool-versions
Phoenix installer v1.6.0
btw, this works:
<div selected={1==index}><%= index %></div>
and produces a boolean attribute
<div>0</div>
<div selected>1</div>
<div>2</div>
nice.
Marked As Solved
Nicd
I guess because the if statement will return nil by default if the check is false.
3
Also Liked
chrismccord
Creator of Phoenix
<div {%{selected: index == 1}}><%= index %></div>
6
Sebb

, else: []
fixes it. thanks guys.
3
Sebb
the shortest I found (by accident) is:
<div boolean_attr={@one == 1}>boolean_attr</div>
I’d like a list of heex-recepies.
Here are some (including stupid ones):
<div {@dynamic_attr}>dynamic_attr from assign</div>
<div {@dynamic_attr} static_attr="is-static">dynamic_attr with static attribute</div>
<div boolean_attr={@one == 1}>boolean_attr</div>
<div {%{boolean_attr: @one == 1}}>boolean_attr from dict</div>
<div {if @one == 1, do: [boolean_attr: ""], else: []}>boolean_attr kw-list from if</div>
<div {if @one == 1, do: %{boolean_attr: ""}, else: %{}}>boolean_attr dict from if</div>
<div some_attr={if @one == 1, do: "one-is-one"}>value from if</div>
<div some_attr={if @one == 0, do: "one-is-zero", else: "one-is-one"}>value from if/else</div>
<div class={"some_class #{if(@one == 1, do: " optional_class", else: "")}"}>interpolation</div>
<div class={"some_class" <> if @one == 1, do: " optional_class", else: ""}>concatenation 1</div>
<div class={"some_class" <> if @one == 0, do: " optional_class", else: ""}>concatenation 2</div>
<div {heex_helper(@some_state)}>dynamic_attr from helper function</div>
<div case={
case @one do
1 -> "one-is-one"
_ -> "one-is-not-one"
end
}>
case
</div>
3
wanton7
I haven’t used HEEx yet but shouldn’t this
<div {if 1 == index, do: [selected: ""]} ><%= index %></div>
be
<div {if 1 == index, do: [selected: ""], else: []} ><%= index %></div>
From here it says it should be keyword list or map Views and templates — Phoenix v1.6.0 because you don’t have else returning [] it’s neither.
2
wanton7
It would be nice if it supported nil as well.
1
Popular in Questions
Hi! May someone helps me, please!
I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
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
As a follow up to my earlier question:
I have the code compiling and running but not getting a successful login from the rest server. ...
New
Hey guys.
I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
What is most correct way to open, read and parse JSON file with poison?
For example if we have example.json file in root of some projec...
New
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
Hello everybody,
usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
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
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
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
Other popular topics
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1]
15:22:35.803 [error] gen_event {lager_file_backend...
New
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
Good day to you all.
I have been struggling to get a query involving like and ilike to work.
Can anyone assist me on this, please?
pro...
New
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
Hi,
I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
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
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
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New







