potentElixir
Phoenix: injecting into assigns for passing to another template
In the post/show template I have:
<%= for comment <- @comments do %>
if (condition) do
# pass comment to _scripts.html
end
end
The objective is that in the _scripts.html, I need the comment available. (_scripts.html has javascript associated with the template)
By default, if I don’t do anything then the render line in post controller render(conn, "show.html", post: post, comments: comments) gives _scripts.html the Available assigns: [:conn, :post, :comments, ...].
So therefore to inject comment into the available assigns I do:
if (condition) do
# pass comment to _scripts.html
<%= render "_scripts.html", Map.put(@conn.assigns, :comment, comment) %>
end
which gives available assigns: [:comment, :post, :comments, ...] but the conn goes missing.
So, instead I tried doing:
if (condition) do
# pass comment to _scripts.html
<%= render "_scripts.html", Map.merge(@conn.assigns, %{comment: comment, conn: @conn}) %>
end
but then conn is available while comment goes missing, as in: assign @comment not available in eex template. Available assigns: [:conn, :post, :comments, ...]
Any help on this will be appreciated.
Most Liked
grych
Please share your exact code, you must have done some other mistake there. @comment should be available in the "_script.html" when you pass it to render/2
grych
As I understood from the code sample at the first post, comment is a local variable, not an assign.
zw0
<%= render "_game_app_nav.html", assigns %>








