I am trying to create applications with Phoenix LiveView. I find it awesome and extremely powerful for providing rich client experience. Thanks.
I have a situation where say I have 5 live routes Page 1 to Page 5 each rendered using their own live view.
I have created a menu in app.html.eex.
Now i want to highlight the corresponding menu item when the user navigates to a page. For ex if user is in Page 3 i want to highlight the Page 3 menu item and so on.
I have created an assign called current_page in all the live views and set it accordingly.
assign(socket, %{
...
current_page: "page1"
})
But I am not able to query that assign in the app.html.eex. I think because conn assigns and socket assigns are different.
How can i access socket assigns from outside of it or from the parent view like layout view or app.html.eex.
Or is there some other way to solve this problem using liveview.
Thank you so much for giving your time and help.
Hi @sreyansjain. Conn assigns and socket assigns are definitely different, if you’re accessing assigns in the root layout those need to be on the conn.
In that case you need to set those assigns via a plug, or move them into the live view. I have a pattern for having most of the layout be live viewed that I can elaborate on tomorrow if necessary as well.
My fallback is rendering the live view inside a normal view and setting the conn assigns in the controller.
That would mean a few extra routes, but it would get the job done.
I am really looking forward to your solution @benwilson512. Thank you so much.
I got it to work by using a plug. Thank you so much. If you have time, i would be really grateful if you can share your pattern for live views. Thank you so so much.