silverdr

silverdr

Phoenix 1.6.6 phx.new puts some new plugs on top of router.ex these days. Those are:

plug :fetch_live_flash 
plug :put_root_layout, {PhxApp.LayoutView, :root}

While I take (please somebody correct me if I am wrong) the first one to be an extended, direct replacement of fetch_flash/2, I am wondering what the use of the second one is. What problem it solves? What was not working well enough in 1.5 that this helps with?

Showing Posts 1 to 6

kartheek

kartheek

It puts root.html.heex as root layout while rendering.

The “root” layout is shared by both “app” and “live” layouts. It is rendered only on the initial request and therefore it has access to the @conn assign. The root layout must be defined in your router:

You can also override if you want in controllers.

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

For Live Views, you can override in live_session

live_session :session_name, root_layout: {MyAppWeb.LayoutView, "custom_layout.html"} do
  live "/path", MyAppWeb.PageLive
end

Live Layouts explains about this.

put_root_layout documentation

kokolegorille

kokolegorille

It’s useful when You want to render in different format.

Sometime I need to render dynamic css, or files, or iframe…

I just need to switch which layout to use.

03juan

03juan

This was committed back in v1.4 branch so it’s been working well since then… Add root layouts (#3679) · phoenixframework/phoenix@a81dd86 · GitHub

silverdr

silverdr OP

Yeah, but you don’t need the said plug in your router to do that, do you? I’ve been switching those as needed happily for some time but only now got to know about that put_root_layout/2 stuff.

Might be. It’s just that I got it in my router.ex only today, once I started migrating project to 1.6.

Well, that’s what it says/implies :wink: and that’s what the controller docs say. But after a few months of work on a sizeable project I don’t recall ever missing it in my router.ex or anywhere else for that matter. I’ve been happily using app.html.slim as my “root” layout all the time. The docs on Live Layouts shed some light on it though. I believe putting it there by default is another step into closer binding of both “live” and “dead” views, right?

03juan

03juan

You don’t need it in the router in general but you have it right that it’s to bridge the gap between live and dead views. It helps to have a way to share the same root in a standard manner, so that the inital unconnected view of a live view can be rendered from the plug and you can set different pipelines to progressively upgrade the root layout. Or you can do progressive overrides e.g. the standard dead view gets a basic root, then a live session can set an upgraded root that pulls in the livesocket js stuff, and an admin live session can override the root with its own layout that may have other js which standard love views don’t need.

kartheek

kartheek

Well, they chose the best possible name for the func :smile:

I migrated an old project from 1.4 to 1.6 - i was surprised by this new plug. It was in phx.new from 1.5 according to git. I messed up migration and it was rendering white pages, turns out this plug was missing from router.

These days I am finding more interesting information in phoenix_live_view docs.

Yes, Common things like js, css, title, etc which go in can be put in root.html.heex to eliminate duplication between live and dead views.

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New
dillonoconnor
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New
thiagogsr
** (ArgumentError) expected :max_attempts to be a positive integer, got: {:@, [line: 10, column: 19], [{:max_attempts, [line: 10, column:...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews