Accessing values added to conn

how do i access values assigned to conn via plug
ie

defmodule Broker.SettingsPlug do
  import Plug.Conn
def call(conn, _options) do
sitename ="name"
conn =  conn
     |> assign(:appname, sitename)
end

im trying ti access in a page commander via

sitename = Application.get_env(:broker, Broker.Endpoint)[:sitename]
IO.inspect(@conn, label: "PageCommander:customerinfo: sitename")

result is nil

@conn.assigns.appname, when you assign(conn, :appname, sitename) it puts it on the assign map in the conn, so @conn.assigns is that map, so just access your key on it via @conn.assigns.appname if you know it will be on there or via @conn.assigns[:appname] if it ‘might’ be on there (returns nil if not). :slight_smile:

maybe its my implentation what im doing is preloading some environment vars via a plug and trying to access them via a controller not a view ie

sitename = ItemController.fetch_sitename()

–itemconmtroller
def fetch_sitename() do
@conn.assigns[:appname]
end

sitename always == nil

Well @conn is definitely view syntax, not a controller. If you want to access an assign on a conn in a controller you just conn.assigns.blah for whatever blah key is, just take out the @ from my prior post. :slight_smile:

Remember a variable tagged with @ in a view is a special assigned variable, they do not exist outside of views, use normal variables, I.E. just conn without the @conn. :slight_smile:

Something like this is never going to work. @conn isn’t some instance variable that can be mutated. Conn values cannot be altered in templates.

1 Like