# Interpolation inside a variable?

**URL:** <https://forum.elixirforum.com/t/interpolation-inside-a-variable/45637>\
**Category:** Questions\
**Tags:** eex, elixir\
**Created:** [January 29, 2022, 6:42pm UTC](https://forum.elixirforum.com/t/interpolation-inside-a-variable/45637 "2022-01-29T18:42:19Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![APB9785](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/apb9785/32/24101_2.png) [@APB9785](https://forum.elixirforum.com/u/APB9785)\
**Post date:** [January 29, 2022, 7:31pm UTC](https://forum.elixirforum.com/t/interpolation-inside-a-variable/45637/3 "2022-01-29T19:31:32Z")

</div>

You shouldn’t dynamically create atoms - that’s not what they’re for. If you anticipate NEEDING interpolation (e.g. you have 1000 computers), then each one should not be given its own atom key in the assigns, but rather stored with string (or integer) keys in a map, like:

```elixir
def mount(_, _, socket) do
  computer_ids = %{"computer0_id" => 123, "computer1_id" => 456, ...}
  socket = assign(socket, computer_ids: computer_ids)
  {:ok, socket}
end

```

Then you can access the ids from the template like:

```elixir
for x <- 0..5 do
  <%= @computer_ids["computer#{x}_id"] %>
end

```

But if you don’t have so many IDs, and you would like to keep each one as a separate atom key in the assigns, then you should create a helper function in your view which explicitly handles each case, for example:

```elixir
def computer_id(x, socket) do
  case x do
    0 -> socket.assigns.computer0_id
    1 -> socket.assigns.computer1_id
    2 -> socket.assigns.computer2_id
    3 -> socket.assigns.computer3_id
    4 -> socket.assigns.computer4_id
    5 -> socket.assigns.computer5_id
  end
end

```

and then in the template

```elixir
for x <- 0..5 do
  <%= computer_id(x, @socket) %>
end

```

---

_[View the full topic](https://forum.elixirforum.com/t/interpolation-inside-a-variable/45637)._
