And I copy the original layouts and make the layouts components as below :
This will make whenever I visit link /users/log_in (Using Original Phx Auth) it will render root file from folder components auth_layouts.
However when I use code <%= @inner_content %> inside root file auth_layouts/root.html.heex it will render page from file layouts/app.html.heex instead of auth_layouts/app.html.heex. Please suggest/assist where can I change to make it render auth_layouts/app.html.heex. Thanks. Any response is highly appreciated
side note : Sorry if the question kinda noob, this is my first project using Phx Framework. Excited but canāt find the solution.
Iām not the best person to answer this, but one thing that immediately comes to mind is have you checked your web_app.ex file (making assumptions about your naming based on what I see in your code example) and what it may/may not do with layouts? By default thereās some layout assignment happening there and my recollection was that it could interfere with the plug based stuff.
I use different layouts as well and rather than trying to control layouts via plugs, I created entries in my equivalent to web_app.ex to deal with the layouts and other kinds of setup as appropriate to the type of form.
Again⦠I could be way off-base and there are others here that can probably be more helpfulā¦
def live_view do
quote do
use Phoenix.LiveView,
layout: {GelaWeb.Layouts, :public}
unquote(html_helpers())
end
end
def admin_live_view do
quote do
use Phoenix.LiveView,
layout: {GelaWeb.Layouts, :admin}
unquote(html_helpers())
end
end
Then:
defmodule MyAppWeb.SomeAdminPage do
use MyAppWeb, :admin_live_view
end
You can do the same with the controller helper.
Iām sure you can make this work with directories if you use a string instead of an atom.
[I had posted my code, but @sodapopcan did so before me and mine had nothing new to offer on that response⦠canāt delete my comment so⦠clearing it.]
You can return a layout from mount/3 like so: {:ok, socket, layout: {module, atom}}. You can also use live_session to change the layout for a bunch of liveviews. No need to drop down to the macros to do that.
Thanks @sodapopcan This give me the idea. I can create a custom function, and on a specific / any live view that I need to be different layouts, I can use that custom function. Thanks.
Below are my solution. Create new custom function on web_app.ex
def live_view_login do
quote do
use Phoenix.LiveView, layout: {WebApp.AuthenticationLayouts, :app}
unquote(html_helpers())
end
end
and on LiveView use use WebApp, :live_view_login instead of using use WebApp, :live_view_login (the original)
Ya, I remember I was doing with this then switched to the macro helper version, I canāt remember quite why. I do add some extra helpers in the admin route though those could probably be moved to hooks ā nm, that doesnāt make any sense, lol. Ya, I think it was just because I had additional helpers but that was so long ago now that it is something I just do. I could re-evaluate.
thank you, I am struggling to find where in the docs it defines what should be in the tuple. Sticking to the overrides for now. Maybe if I continue learning the platform I will figure it out someday.
Although I did the opposite of your admin and used a guest layout instead. Guest layout is used for all routes that require a user to be un-authenticated, such as login, register, reset password etc.
Coming from a Laravel background, it made more sense to me in my head
Iām still sporadically getting pinged on this issue so I wanted to note that I regret my answer and I do NOT recommend doing this. @LostKobrakaiās post is the right way and should be the accepted answer. AFAIC itās objectively better to use what the framework provides over writing custom macros.
I recently inherited a codebase that uses this pattern. As is almost guaranteed to happen when blazing your own trails additional concerns like authn ended up in these macros which should have been in on_mount callbacks. This was not only really confusing to figure out at first but the inclusion of the extra logic within macros caused unnecessary compile time dependencies.
Return {:ok, socket, layout: {MyAppWeb.Layouts, :layout} from mount/3 or use live_session, :my_session, layout: {MyAppWeb.Layouts, :layout} in the router.
Hereās how you do it for many live views, by using live_session.
scope "/", MyAppWeb, host: "dietitian." do
pipe_through [:browser]
live_session :dietitian_dashboard, layout: {MyAppWeb.Layouts, :dietitian} do
live "/", Dietitian.DashboardLive
end
end