Twig - 动态替换 GET 参数的值 [英] Twig - dynamically replace GET parameter's value

查看:11
本文介绍了Twig - 动态替换 GET 参数的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法从 twig 中替换 GET 参数值?

Is there a way to replace a GET parameter value from twig?

例如,我在这个地址有一个页面:

For example, I have a page at this address:

http://localhost/app_dev.php/test/?param1=40&sort=name

在我的树枝中,我想建立 3 个这样的链接:

And in my twig I want to build 3 links like this:

http://localhost/app_dev.php/test/?param1=40&sort=name 
http://localhost/app_dev.php/test/?param1=40&sort=address 
http://localhost/app_dev.php/test/?param1=40&sort=code 

现在我在 URL 的末尾再次添加了&sort"参数,但这个解决方案实际上是一个补丁",它很糟糕!

For now I added the "&sort" parameter once again at the end on the URL, but this solution is actually a "patch" and it sucks!

<a href="{{app.request.requesturi}}&sort=address">address</a>

在这个例子中我只有 2 个参数,但实际上我有大约 6 个参数,因为它生成的链接是通过提交一个 .

In this example I only have 2 parameters, but in reality I have around 6 parameters, because the link that's generated it's obtained by submitting a .

推荐答案

这应该可以解决您的问题:

This should solve your problem:

{{ path(app.request.attributes.get('_route'),
   app.request.query.all|merge({'sort': 'address'})) }}

它获取当前路由和所有查询参数,这些参数在附加之前与您想要更新的参数合并.

It gets the current route and all query parameters which are merged with the one you like to update before they are appended.

这篇关于Twig - 动态替换 GET 参数的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆