Create a link with an anchor with Twig path function in Symfony 2

ぃ、小莉子 提交于 2020-06-25 07:23:18

问题


I'm trying to create a link with an anchor like "www.example.com/services#anchor1" in my Twig template. So far I've been using the path function to create links path('services'). I have tried with path('services#anchor1') but obviously it doesn't work.

It doesn't seem to be a lot of information about this function or it's just that I can't find it. Any idea about how could I do it?

Thanks!


回答1:


Try <a href="{{ path('_welcome') }}#home">Home</a>




回答2:


As of Symfony 3.2 you can use the _fragment option:

<a href="{{ path('homepage', {'_fragment': 'home'}) }}">Home</a>

Check out the feature introduction on the Symfony blog.




回答3:


I had the same problem. I had just link being generated in the a tag but then needed to open in a new page aka add target="_blank" to the generated url so I changed the href to this and it worked!

<a class="new-item"  target="_blank" href="{{ link(item.title, item.url) }}"></a>


来源:https://stackoverflow.com/questions/8794886/create-a-link-with-an-anchor-with-twig-path-function-in-symfony-2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!