I have some “gadget” html block, with their self logics, expected to place in “app.html.heex” ; and I hope even the client disable javascript I can use it ,then depend on it I can show different content. like a language switcher, read the first request header and cookie then decide what “current language” is. I tried add some code inside pipeline :browser but in “app.html.heex” I can not read the :conn
, but in “root.html.heex” read it well. so how to assign them and use them in “app.html.heex” ?
thanks for any help
EDIT - I read your question wrong.
You will need to separate the logic between Conn based and Socket based (assuming you are using LiveView?). You can use plugs to set assigns and you should be able to access them in app.html.heex
just fine. But once the view is upgraded to LiveView you will have to deal with that in Server hooks or something.
Thanks for reply. I expected that:
1, when there is a crawler/spider client(without javascript enable) came in, I can give it correspone content depend on its request header language-related info;
2, when human user came in, In addition to these, they also can switch to other language (through liveview or live component)
I have make a plug with which assign my custom variables, place it in pipeline :browser , I can only access my custom variables in “root.html.heex” but not “app.html.heex”.
I’ve figured it out. I have to place these assign logics in on_mount
like this:
scope "/", MyAppWeb do
pipe_through [:browser]
delete "/users/log_out", UserSessionController, :delete
live_session :current_user,
on_mount: [
{MyAppWeb.UserAuth, :mount_current_user},
{MyApp.Utils.I18n, :assign_custom_vars}
] do
live "/", HomePageController, :home
...
then I can access them in everywhere, “app.html.heex” or inside liveview . Question solved
Hey. That’s what I said on my first edit. But tought you wanted something else so edited it out. Glad you figured it out.