Extract data from Plug.assign to render in json views

Is it possible to access Plug.assign data in my phoenix views? I find myself writing long strings like

render("show.json",status: account_status, key_status: key_notification_status

Somehow I feel like I am able to extract Plug data from views or these are simply for processing of the plug pipeline?

1 Like

Hello @chu,

The keyword list in Phoenix.Controller.render/3 get merged into the assigns and are made available to the view in render/2 as a map.

So if you wanted you could use Plug.Conn.assign/3 like so:

conn
|> assign(:status, account_status)
|> assign(:key_status, key_notification_status)
|> render("show.json")

Then in your view, you could pull them out of the assigns as usual:

def render("show.json", assigns) do
  ...
end

I hope that answers your question.

Sonny

5 Likes