简体   繁体   中英

How to pass query url parameter to a twig template

I am trying to pass query string parameters through my url to a node twig template (node--template.html.twig) I have tried

 {% set queryParams = app.request.query.all %}

However, nothing is showing when passed.

You are almost there.

 {% set queryParams = app.request.query.all %}

This statement will not show anything. Because it is only supposed to assign query variables array to queryParams

If you want to display, you have multiple ways after the above statement.

For example, if you just want to display the value of user_id query variable from URL.

{% set queryParams = app.request.query.all %}
{{ queryParams["user_id"] }}

Another example, if you want to loop through all query variables:

{% set queryParams = app.request.query.all %}
{% for key, value in queryParams %}
    {{ key }} => {{ value }}
{% endfor %} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM