I found a clause in 'app.html.eex' auto-created by elixir phoenix:
<main role="main">
<%= render @view_module, @view_template, assigns %>
</main>
but where is this keyword map 'assigns' coming from?
assigns
is a template-context local variable declared via hygiene bypassing in the template compile-time.
per the docs Phoenix.View.hmtl
Assigns
"Assigns are meant to be user data that will be available in templates. However, there are keys under assigns that are specially handled by Phoenix, they are:
:layout - tells Phoenix to wrap the rendered result in the given layout. See next section. The following assigns are reserved, and cannot be set directly:
@view_module - The view module being rendered @view_template - The @view_module’s template being rendered "
assigns is a property on the conn object designed to add data to pass around like options.
assigns itself is coming @conn.assigns, in any controller you can play around with this by adding this code to a controllers index
defmodule MyAppWeb.PageController do
use MyAppWeb, :controller
def index(conn, _params) do
conn = assign(conn, :thing, "this is not a taco")
render conn, "index.html"
end
end
then in app.html.eex add this line
<%= assigns.thing %>
then you should see "this is not a taco" when you hit the controllers index, "/" in this example
if you add this line to your controller you can see it in your server
...
conn = assign(conn, :thing, "this is not a taco")
IO.inspect(conn.assigns)
...
I have most commonly seen this used to set a user to assigns to have access in the views like this ...plugs/set_user
def init(_params) do
end
def call(conn, _params) do
if conn.assigns[:user] do
conn
else
user_id = get_session(conn, :user_id)
cond do
user = user_id && Repo.get(User, user_id) ->
assign(conn, :user, user)
true ->
assign(conn, :user, nil)
end
end
end
...view/html
<%= if @conn.assigns.user do %>
Hello, <%= @conn.assigns.user.first_name %>!
<% else %>
... do something else
<% end %>
to assign value you use 'assign'
to get the values you use 'assigns'
render conn, "index.html", key: :value
, the assigns will be populated with thekey: :value
pair, as well as anywhere you callassign(conn, :other_key, :other_value)
. The actual assigns are held in theconn
. You just get convenient access to them in the templates. - Justin Woodassigns
variable? Because that reads differently from your question. - Justin Woodassigns
just refers toconn.assigns
. Unless I am misunderstanding the question? - Dan