How to select layout

Hi,

Phoenix views documentations says that views and templates are rendered (by default) with app.html.eex. Is there a way (I guess it is) to change the default payout used to render pages ?

The question behind is that I wonder to how manage the case when you need to add some css or js in the html header, depending on the rendered page ?

Thanks to your replies.

You can call put_layout/2 as a “plug” somewhere in your “plug pipeline” depending on the scope you want it to have.

defmodule Web.Endpoint do
  # ...
  plug put_layout, "other.html" # affects every request to this app since it is in Web.Endpoint
  # ...
end
defmodule Web.Router do
  # ...
  pipeline :admin do
    # ...
    plug put_layout, "admin.html" # affects every request piped through :admin
    # ...
  end
  # ...
end
defmodule Web.SomeController do
  # ...
  plug put_layout, "some.html" # affects only requests processed by this controller
  # ...
end
8 Likes

Thanks.
Meanwhile i’ve found this page which gives the same answer.

2 Likes