I’m trying to build a loop in an EEx template that may have to iterate over a list OR a map (i.e. loop over its keys/values) and include an index (yes, I know that’s a lot).
So far, my EEx template looks like this:
The start...
<%= Enum.with_index(list)
|> Enum.map(fn {x, index} ->
case x do
{k, v} -> %>
<%= k %> <%= v %>: @index: <%= index %>
<% x -> %>
<%= x %>: @index: <%= index %>
<% end %>
<% end) %>
...The end.
But that results in an error:
** (TokenMissingError) nofile:12: missing terminator: end (for "fn" starting at line 3)
lib/eex/compiler.ex:101: EEx.Compiler.generate_buffer/4
lib/eex/compiler.ex:54: EEx.Compiler.generate_buffer/4
lib/eex.ex:199: EEx.eval_string/3
my_file.exs:37: (file)
A simpler EEx template works:
The start...
<%= Enum.with_index(list)
|> Enum.map(fn {x, index} -> %>
<%= x %>: @index: <%= index %>
<% end) %>
...The end.
but that one only handles lists. I think I’m getting stuck on where exactly I’m allowed to break in and out of Elixir code within an EEx template. The basic logic works when it’s not in EEx.
Thanks for any pointers!






















