Phoenix 1.6 custom layout for controller action? has it changed?

I assume something has changed in Phoenix 1.6 because now when I set a layout in say an edit controller action I get nested pages, i.e. the root has the custom layout page inside it.

This used to work

def index(conn, _params) do
  posts = Blog.list_posts()
  render(conn, "index.html",
    posts: posts,
    layout: {TeacherWeb.LayoutView, "blog.html"})
end

but now it’s nesting the blog.html layout page inside the root.html

what to do?

router has browser pipeline which sets root_layout:

  pipeline :browser do
    plug :accepts, ["html"]
    plug :fetch_session
    plug :fetch_live_flash
    plug :put_root_layout, {TeacherWeb.LayoutView, :root} #<- this one 
    plug :protect_from_forgery
    plug :put_secure_browser_headers
  end

If you want to override the root layout - then try using put_root_layout/2:

    conn
    |> put_root_layout({TeacherWeb.LayoutView, "blog.html"})
    |> render("index.html",  posts: posts)
3 Likes

thanks, thats the update I was looking for

1 Like

It would be a better forum etiquette to mark @kartheek’s comment as the solution. You basically copied part of his comment and marked your copy as the solution.

6 Likes

fixed, thats for the feedback

4 Likes