在 Symfony 2 中使用 Twig 路径函数创建带有锚点的链接

发布于 2024-12-26 05:07:39 字数 249 浏览 2 评论 0原文

我正在尝试在 Twig 模板中创建一个带有“www.example.com/services#anchor1”这样的锚点的链接。到目前为止,我一直在使用 path 函数来创建链接 path('services')。我尝试过 path('services#anchor1') 但显然它不起作用。

关于这个功能的信息好像不是很多或者只是找不到。知道我该怎么做吗?

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

扛刀软妹 2025-01-02 05:07:39

尝试 Home

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

面如桃花 2025-01-02 05:07:39

从 Symfony 3.2 开始,您可以使用 _fragment 选项:

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

查看 功能介绍

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.

玩心态 2025-01-02 05:07:39

我也有同样的问题。我刚刚在 a 标签中生成了链接,但随后需要在新页面中打开,即添加 target="_blank" 到生成的 url,所以我将 href 更改为此并且它起作用了!

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文