Best way to make a query result available in all application instead of just in a controller action

I want to make a query result that I use in a warning popup available to my all application regardless of the route, i.e., I would like to use @destaks in my layout.
Currently I’m limited to a specific route because I’m doing this inside my page_controller:

def admin(conn, _params) do
    destaks = Data.listAll(Destak, nil)
    render(conn, "admin.html", destaks: destaks)
  end

What’s the cleanest alternative? Creating a plug? Is there a Phoenix predefined approach?

I wonder if putting it into LayoutController would work.

We’re using plugs for similar use cases. You can just Plug.Conn.assign something to your conn and it’ll be available in your templates.

1 Like