# Complex loop in EEx

**URL:** <https://forum.elixirforum.com/t/complex-loop-in-eex/27698>\
**Category:** Questions\
**Tags:** eex, loop\
**Created:** [December 19, 2019, 10:01pm UTC](https://forum.elixirforum.com/t/complex-loop-in-eex/27698 "2019-12-19T22:01:59Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![patrickdm](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/patrickdm/32/7579_2.png) [@patrickdm](https://forum.elixirforum.com/u/patrickdm)\
**Post date:** [December 19, 2019, 11:35pm UTC](https://forum.elixirforum.com/t/complex-loop-in-eex/27698/2 "2019-12-19T23:35:24Z")

</div>

Hello, I’d try to simplify it by moving the function to the view module, using string interpolation, i.e. (disclaimer: untested code ahead..)

```elixir
def eval_item(x, index) do
  case x do
    {k, v} -> 
      "#{k} #{v} : @index: #{index}"
    val ->
      "#{val} : @index: #{index}"
    end
end

```

or with two functions using pattern matching on arguments (that I prefer as I find it easier to read):

```elixir
def eval_item({k, v} = x, index), do: "#{k} #{v} : @index: #{index}"

def eval_item(x, index), do: "#{x} : @index: #{index}"

```

then in the template you could just:

```elixir
<%= Enum.with_index(list) |> Enum.map(&eval_item/2) %> 

```

hth.

---

_[View the full topic](https://forum.elixirforum.com/t/complex-loop-in-eex/27698)._
