Both layouts for liveview and normal view renders when using live_render on root.html.leex

Hello,

let’s imagine you have 3 files:

  • root.html.leex
  • app.html.eex
  • live.html.leex

In your router, in the brower pipeline you can set

  pipeline :browser do
    plug(:put_layout, {DropShippingWeb.LayoutView, :app})
    plug(:put_root_layout, {DropShippingWeb.LayoutView, :root})
  end

Basically it will set the default root layout as root.html.leex and the default layout as app.html.eex.
Then, when you need to use live_view (so basically when you don’t have your @conn) you will write things like:

defmodule YourLiveView do
  use Phoenix.LiveView, layout: {MyAppWeb.LayoutView, "live.html"}

  ...
end

Now in the root.html.leex you should put general things, like menus, footer etc.
In the app.html.eex you can simply write your get_flash/2 and in the live.html.leex you ca use @flash.

So what happen when using layout:

  • in default case : use root layout → use app layout → render the view
  • when specifying the layout in liveview → use general layout → use live layout → render the view

Your double flash messages should disappear.

There is also other methods, describe here Phoenix.LiveView — Phoenix LiveView v0.15.4. Where you can set the layout in the router.

Have a great day :wink:

5 Likes