Create a link with an anchor with Twig path functi

2020-08-09 04:50发布

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!

标签: symfony twig
3条回答
乱世女痞
2楼-- · 2020-08-09 05:41

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>
查看更多
我想做一个坏孩纸
3楼-- · 2020-08-09 05:46

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

查看更多
家丑人穷心不美
4楼-- · 2020-08-09 05:51

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.

查看更多
登录 后发表回答