Raees678
I have a sidebar as a function component that takes the current route as well as a list of items to display:
[%{
title: "some title",
route: ~p"/some/route"
}, ...]
The sidebar displays all of the items vertically, and if any of their routes matches the current route, then it adds a little highlight to display the currently selected page.
But now I would like to embed this into my layout, so that all dead and LiveViews can use this. So I put this in my app layout, since I would like the highlighted item to update if i live_redirect. But here is where I run into my issue.
If my app is rendering a dead view, I can access the @conn variable here, and obtain the current route to pass to my sidebar component. But if it is rendering a LiveView @conn is not available, and I must use @socket, where the current route is not available. Is there a way I can obtain the current route from the socket, or is there any other solution for obtaining the current route form the layout regardless if the page being rendered is a dead view/LiveView, and also one that updates across live_redirects etc.
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex










Showing Posts 1 to 8- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
gpopides
You can make use of handle_params
Second parameter as you can see in the documentation is the current uri, so removing the base uri you can get the current path.
Something that you could use is
brady131313
I made a hook that I attach in a live session so the current path is always available
Raees678
I thought this too, but this does not work in the
app.html.heexfile. I need the sidebar in this is the file since it is the layout that embeds the<.sidebar current_url={...} items={...}/>component, in all views (both dead views and liveviews).I think the file is just a template, that I’m assuming compiles down to a function body in the
layouts.exfile, similar to how other.heexfiles get converted into function bodies in the respective controller etc. Since the layout.ex file isn’t a LiveView, puttinghandle_paramsin that file doesn’t work. Even attaching a hook doesn’t work since thesocket.assignsthemselves are unavailable at the time thatapp.html.heexis evaluated. If I try to print them, even with a hook attached, the value of the assigns isPhoenix.LiveView.Socket.AssignsNotInSocket<>.gpopides
Can you make the sidebar a live view? then you can use
handle_paramsand in
app.html.heexcall it with<%= live_render(@conn, Sidebar) %>Raees678
I tried this as well, but this does not work for another reason. If you use
live_renderthenhandle_paramsisn’t invoked, and the app doesn’t even compile, and throws an error.handle_paramsis only ever invoked for live routes in the router.Raees678
Actually @brady131313 @gpopides, I think I have got it working with a hook. I think I was checking the wrong
assigns.socketproperty and hence I was seeingPhoenix.LiveView.Socket.AssignsNotInSocket<>. When I just check theassignsin theapp.html.heex, I can see the hook’s url output in there.I’m still confused about the order of how the assigns works though, since
handle_paramsis invoked after themountof a liveview. So at what point do things get put in theassignsof the layout?sodapopcan
You can assign
@current_pathin both a plug and a LiveView hook (not to be confused with a js hook).Plug:
…and add to your browser pipeline in the router:
Live Hook:
…and attach the hook in your router:
(
:globalcan be something more descriptive if you like)Now
@current_pathis always available in the layout whether rendered by a live or dead view.You can use these methods to stuff other data into assigns as well.
EDIT: I forgot the router portion for the plug… see above.
2ND EDIT: Sorry, @brady131313, I blew past your message that offered the same hook advice.
bo_dev
Don’t be sorry, Soda. I am a complete beginner, and your example has been incredibly helpful.