在 Symfony2 中使用 Twig 删除 URL 参数
Remove URL parameter using Twig in Symfony2
我有一个视图,其中包含搜索结果和一些添加过滤器的按钮。操作很简单,通过点击按钮再次调用当前页面通过URL传递一个参数?foo=foo
。
系统运行良好,但我想再次点击过滤器按钮是活动参数引用被删除。
这可能吗?
树枝代码:
<div class="col-lg-12 unfilled" style="float:none; margin-bottom:1.3em">
<a href="{{ path(app.request.attributes.get('_route'), app.request.query.all|merge({'filter': 'pa'})) }}" class="btn btn-default btn-sm {{ app.request.get('filter') == 'pa' ? 'active' : '' }}">Menor precio</a>
<a href="{{ path(app.request.attributes.get('_route'), app.request.query.all|merge({'filter': 'pd'})) }}" class="btn btn-default btn-sm {{ app.request.get('filter') == 'pd' ? 'active' : '' }}">Mayor precio</a>
<a href="{{ path(app.request.attributes.get('_route'), app.request.query.all|merge({'stars1': '1'})) }}" class="btn btn-default btn-sm {{ app.request.get('stars1') > 0 ? 'active' : '' }}">1 <i class="glyphicon glyphicon-star"></i></a>
<a href="{{ path(app.request.attributes.get('_route'), app.request.query.all|merge({'stars2': '3'})) }}" class="btn btn-default btn-sm {{ app.request.get('stars2') > 0 ? 'active' : '' }}">2 <i class="glyphicon glyphicon-star"></i></a>
<a href="{{ path(app.request.attributes.get('_route'), app.request.query.all|merge({'stars3': '5'})) }}" class="btn btn-default btn-sm {{ app.request.get('stars3') > 0 ? 'active' : '' }}">3 <i class="glyphicon glyphicon-star"></i></a>
<a href="{{ path(app.request.attributes.get('_route'), app.request.query.all|merge({'stars4': '7'})) }}" class="btn btn-default btn-sm {{ app.request.get('stars4') > 0 ? 'active' : '' }}">4 <i class="glyphicon glyphicon-star"></i></a>
<a href="{{ path(app.request.attributes.get('_route'), app.request.query.all|merge({'stars5': '9'})) }}" class="btn btn-default btn-sm {{ app.request.get('stars5') > 0 ? 'active' : '' }}">5 <i class="glyphicon glyphicon-star"></i></a>
</div>
非常感谢。
使用参数设置为空以避免参数:
{{ path(app.request.attributes.get('_route'), app.request.query.all|merge({'filter': 'pd', 'star1': null, [...]} }}"
对所有参数执行此操作以禁用 ([...])。
我有一个视图,其中包含搜索结果和一些添加过滤器的按钮。操作很简单,通过点击按钮再次调用当前页面通过URL传递一个参数?foo=foo
。
系统运行良好,但我想再次点击过滤器按钮是活动参数引用被删除。
这可能吗?
树枝代码:
<div class="col-lg-12 unfilled" style="float:none; margin-bottom:1.3em">
<a href="{{ path(app.request.attributes.get('_route'), app.request.query.all|merge({'filter': 'pa'})) }}" class="btn btn-default btn-sm {{ app.request.get('filter') == 'pa' ? 'active' : '' }}">Menor precio</a>
<a href="{{ path(app.request.attributes.get('_route'), app.request.query.all|merge({'filter': 'pd'})) }}" class="btn btn-default btn-sm {{ app.request.get('filter') == 'pd' ? 'active' : '' }}">Mayor precio</a>
<a href="{{ path(app.request.attributes.get('_route'), app.request.query.all|merge({'stars1': '1'})) }}" class="btn btn-default btn-sm {{ app.request.get('stars1') > 0 ? 'active' : '' }}">1 <i class="glyphicon glyphicon-star"></i></a>
<a href="{{ path(app.request.attributes.get('_route'), app.request.query.all|merge({'stars2': '3'})) }}" class="btn btn-default btn-sm {{ app.request.get('stars2') > 0 ? 'active' : '' }}">2 <i class="glyphicon glyphicon-star"></i></a>
<a href="{{ path(app.request.attributes.get('_route'), app.request.query.all|merge({'stars3': '5'})) }}" class="btn btn-default btn-sm {{ app.request.get('stars3') > 0 ? 'active' : '' }}">3 <i class="glyphicon glyphicon-star"></i></a>
<a href="{{ path(app.request.attributes.get('_route'), app.request.query.all|merge({'stars4': '7'})) }}" class="btn btn-default btn-sm {{ app.request.get('stars4') > 0 ? 'active' : '' }}">4 <i class="glyphicon glyphicon-star"></i></a>
<a href="{{ path(app.request.attributes.get('_route'), app.request.query.all|merge({'stars5': '9'})) }}" class="btn btn-default btn-sm {{ app.request.get('stars5') > 0 ? 'active' : '' }}">5 <i class="glyphicon glyphicon-star"></i></a>
</div>
非常感谢。
使用参数设置为空以避免参数:
{{ path(app.request.attributes.get('_route'), app.request.query.all|merge({'filter': 'pd', 'star1': null, [...]} }}"
对所有参数执行此操作以禁用 ([...])。