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

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

问题描述

有没有办法从树枝中替换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天全站免登陆