redpoll

redpoll

Interpolation inside a variable?

Hi, I am wondering if this could be possible:

for x <- 0..5 do
  <%= @computer{x}_id %>
end

I have tried

<%= {"computer#{x}_id"} %>

which actually renders computer1_id, computer2_id etc. as a string, but does not run the command to get the actual id from the @computer1 assigned to the socket. Is what I want possible?

Most Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

It’s all still sort of weird though, it’d be much more idiomatic to have the key of the computers map just be the id itself, I don’t see how it helps to have it surrounded by extra atom or string content:

%{
  1 => %Computer{},
  2 => %computer{}, etc
}

Then you can simply look up the computers by id. BUT even this is sort of weird, it would make more sense for the @computers assign to be an ordinary list where the items are simply sorted already into the desired order, and then it can be iterated through as for computer <- @computers do and we skip all of the extra lookups.

Xty

Xty

It’s usually a bad idea to create atoms dynamically, yes. But in this case I’d argue it’s fine, considering they’re not generated from random user input, but rather a bounded well-defined range.

Could also use String.to_existing_atom to do it without risking the usual problems with dynamically generated atoms.

Xty

Xty

You can probably do the following:

assigns[String.to_atom("computer#{x}")]

This works because @computer1 is shorthand for assigns.computer1 which can also be expressed as assigns[computer1]

APB9785

APB9785

Creator of ECSx

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:

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:

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:

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

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

Xty

Agreed! Your suggestions are definitely more idiomatic and likely the way to go.

I just took the opportunity to show how to dynamically access assigns if needed.

Where Next?

Popular in Questions Top

senggen
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
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
jerry
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
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
johnnyicon
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
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
openscript
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
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
AstonJ
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
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New

We're in Beta

About us Mission Statement