How to get the current anchor/_fragment in twig

  anchor, javascript, symfony, twig

I have a page with 3 anchors (named a, b and c)
When i access this page i can have

  • http://local/personne/1#a
  • http://local/personne/1#b
  • http://local/personne/1#c

As you can imagine, my page display some personne data and i also have a quick navigator to navigate to the next user.

I would like this quick navigator to keep the current anchor (or at least the anchor the page was loaded with)

I know about ‘_fragment’ stuff in symfony, and i think it can bu use for my ‘at least’ solution

<a href="{{ path('next_personne', {'id':, '_fragment': 'TODO get current fragment here'}) }}">
          {{ }}

So if we access

  • http://local/personne/1#a ==> http://local/personne/2#a
  • http://local/personne/1#b ==> http://local/personne/2#b
  • http://local/personne/1#c ==> http://local/personne/2#c

And if possible, the solution i would like to implement:

If we access the page using http://local/personne/1#a, it will render the url for next personne as http://local/personne/2#a. And if we click so that the anchor change to b, the url of the ‘next’ button will change to b also. I guess this would require some JS.

So basically if you know how to get the current anchor in twig or / and in JS, it would probably solve my problem.

Source: Symfony Questions