{{ query_params }}

{{ query_params }}

Object containing all of the query string parameters for the current page.

Properties

Specific parameters may be accessed using {{ request.query_params.parametername }} or {{ request.query_params['parameter-name'] }}.

You may also treat this object as a list containing all of the query parameters which may be iterated using a {% for %} loop. For more details, see the examples below:

Field Type Description
object_type string Will always be "query_params".
is_valid true/false Will always be true.
by_index list A list of all of the query parameters in the order they appear in the URL. Each item in the list is the full query parameter - including both the key and the value. Note that this may include empty and/or duplicate query parameters.
by_name object An object whose properties match the query parameters in the URL, including query parameters with empty values. If the same query parameter appears multiple times in the URL, the value will be a comma-separated list of all of the values for that key.
keys list A list of all of the non-empty parameter names in the URL.
count number The number of distinct query parameters (by name) in the URL.
length number The total number of query parameters (by index) in the URL.

Examples

Getting request parameters and fetching blog_posts

Copy
{% assign var postsPerPage = 10 %} {% assign pageParam = 1 %} {% if request.query_params['page'] %} {% assign pageParam = request.query_params['page'] | to_int %} {% endif %} {% if request.query_params['tag'] and request.query_params['tag'] != "" %} {% assign tagFilter = request.query_params['tag'] | url_decode | downcase %} {% endif %} {% blog_posts assign posts = blog:"The Kitchen Essentials" tag:tagFilter limit:postsPerPage page:pageParam sort_by:"post_date" sort_direction:"desc" %} {% for post in posts %} {{post.title}} {% endfor %}

URL: www.domain.com/path/to/page?alpha=abc&beta=b&&emptyvariable&animals=cat&animals=dog&animals=fish

Copy
{{ request.query_params }} => alpha=abc&beta=b&&emptyvariable&animals=cat&animals=dog&animals=fish {{ request.query_params.count }} => 4 {{ request.query_params.length }} => 7 {{ request.query_params.alpha }} => abc {{ request.query_params.emptyvariable }} => {{ request.query_params['animals'] }} => cat,dog,fish {{ request.query_params[1] }} => beta=b {% if request.query_params has_key 'alpha' %}has alpha{% else %}no alpha{% endif %} => has alpha {% if request.query_params contains 'animals=cat' %}cat{% else %}no cat{% endif %} => cat {% for param in request.query_params %} --same as {% for param in request.query_params.by_index %} {% unless forloop.first %}, {% endunless %}{{param}} {% endfor %} => alpha=abc, beta=b, , emptyvariable, animals=cat, animals=dog, animals=fish {% for param in request.query_params.keys %} {% unless forloop.first %}; {% endunless %}{{param}}: {{request.query_params[param]}} {% endfor %} => alpha: abc; beta: b; emptyvariable: ; animals: cat,dog,fish