iamkanishka
How to get current URL in app.html.heex
I have live_component (sidebar component) in app.html.heex in i need to hide and show based on the the current URL as every time the URL changes, any solution/suggestion please
Trending in Questions
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated!
To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead.
Sta...
New
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
Hi all, I wanted to ask how the community is dealing with post-release steps.
Today we have Ecto migrations, which make sure that the db...
New
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New
Hello,
I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #performance











First 10 of 18 Posts
kamaroly
Heres how I do it.
iamkanishka
Thanks kamaroly
pelopo
Is there nothing as a built in option for such a standard thing?
jswanner
The current URI is available in the
handle_params/3callback of every LiveView, and there are mechanisms available in the framework to reduce repeated application logic. What’s suggested here is a good example of using those available mechanisms to set the URI in the assigns of every LiveView (without much code, IMO).What were you hoping the “built in option” would work?
Stefano1990
I think for people who are coming from all-batteries-and-your-grandmother-included frameworks are confused when not everything is built into the framework.
pelopo
Something like “host_uri” from the socket, but this only gives me the domain, not the path.
I’ve got a global menu and in it I want to highlight buttons of the menu if they are selected. When clicked, the button takes you to a route and the logic would be “if the current_path is /example then highlight button “example”
My menu is in the “app.html.heex” and it only got “socket”, no “conn”
Thanks @jswanner . I tried to modify the handle-params in one of the live views by adding the url, but got different errors later in heex saying that I can’t use handle_params in a child LiveView . And even if I could, I would need to add this option manually to every liveview that is connected to a menu button.
Anyway, after trial and error, huffing and puffing, I wrote a JavaScript Hook. Now it all works, but it feels extremely cumbersome for something so small and basic.
PS: the JS hook does not identify the path, it just highlights any button that was clicked
pelopo
Yes @Stefano1990 , we kind of do. The understanding and the default assumption is that Phoenix is indeed a battery included framework. If it’s got Ecto and database Integration, it’s got authorisation nipped in the bud, It’s got email sending built-in, it’s got live view and pub subs. so yeah, I kind of expect it would be easy to get the current page from the heex template
LostKobrakai
There’s multiple things problematic with an approach like this:
Given those constraints a callback like
handle_paramsand letting all the change tracking work like anywhere else for any other retained custom data makes sense.sodapopcan
To add some context, the reason the current URL isn’t just available to views is that it needs to be stored on the socket (as mentioned above) which isn’t free. Not every application needs it and many applications only need pieces of it (for example perhaps they don’t care about query params or perhaps just the first slash segment) so it’s left up to the developer to decide what to store.
pelopo
Thanks @sodapopcan @LostKobrakai - I understand the listed reasons, but I still can’t justify them. I don’t know the how, but - if JS can do it, I’m sure Elixir/Phonenix can too and the memory footprint - I don’t think a couple of strings with slashes would clog the socket. I think that if Svelte (as compiled as Phoenix) can be fast and light, Elixir/Phoenix can do too. My take is that the reason for this is more historical and cultural than technical - and it’s fine by me.
I’m not going to win this battle. I’m just looking at it with the eyes of a newcomer and notice what in my opinion is something that is taken for granted elsewhere while that elsewhere is much more primitive (Svelte, Next.js) than Phoenix.
Anyway, if you can’t beat them - join them…
A question about
handle_params- I can’t use this approach for all of my menu buttons because one of them leads to a search modal based on a LiveView and myapp.html.heexis also backed by a LiveView (no@conn) and If I usehandle_params, I get an errorhandle_params/3 is not allowed on child LiveViews, only at the root.What could I do in this case? Thanks
P.S. Here is my repo
The ‘Territory’ menu button is the only one that can’t accept
handle-params. The rest of them I adapted to your @LostKobrakai approach. Thanks