Suppose I have the default root & app layouts, and now I want to have a new layout, called cms, and I want it to be nested under app.
The idea is simple, I have a scope called /cms and I want it to have its own navigation while keeping functionality from app layout.
Further down the line I’ll probably have a bit more layouts as well.
I went over the web, the docs, the forum, the different LLMs, but couldn’t find a way of achieving that.
There is no arbitrary nesting of layouts. The split of root/app layout exists for purely technical reasons and didn‘t exist before LiveView was added to phoenix.
Instead of the nesting at the layout level I‘d suggest composing layouts from shared parts e.g. being function components. Those components can be arbitrarily nested.
So in that case, I’ll have a lot of repetition, won’t I?
Suppose I have 10 pages under /cms, each will have to have it.
I can make a function component to host it, but it feels meh, seems like having a nested layout is way more efficient.
What repetition are you talking about? You can configure the used layout on the router level with live_session. There might be a bit of repetition between app.html.heex and cms.html.heex, but with a handful of function components that’s mostly boilerplate and no details.
I feel like I’m missing something in your answers.
I’ve attached a screen shot of what I’m trying to achieve.
I want to have a dedicated layout for the CMS.
Yes, you have tiny amounts of duplication, but really you’ll hardly touch those template files. The meat of what those layouts are about is in the function components.
Seems like something’s off with the way assigns get passed around.
In my router, I got -
defp assign_cms_env(conn, _opts) do
conn =
assign(conn, :cms_env, :dev)
IO.inspect(conn.assigns, label: "Conn")
conn
end
pipeline :cms do
plug :assign_cms_env
end
Then, for my ‘/cms’ scope, I pipe it like that -
pipe_through [:browser, :cms]
The inspect with the “Conn” label prints out properly.
But then, no matter what I’m doing in my cms.html.heex layout, I never get access to the cms_env value.
I do have other assigns that were made in the page’s mount function, but not the ones from the pipe.
Do you have an idea?
That’s my current cms.html.heex file, many attempts were made to pass stuff around -
I’m probably missing out something.
If I look at UserAuth.fetch_current_user, that’s being used in the browser pipeline, it alters the conn and then I have it on my socket.
I was under the impression that data on conn gets passed down to the socket.
What am I getting wrong here?