Query Params

Query Params

{{ query_params }}

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

Properties of {{ query_params }} objects
Name Type Description
is_valid Boolean 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 containing all of the unique query parameter names
count Integer The number of unique query parameter names in the URL.
length Integer The total number of query parameters in the URL, including empty and duplicate query parameters
* String Specific query parameter values may be accessed using {{ request.query_params.parametername }} or {{ request.query_params['parameter-name'] }}
output String The full raw querystring from the request

You may treat the query_params object as a list containing all of the query parameters which may be iterated using a {% for %} loop. The query_params object is copyable using the {% copy_to_dictionary %} method.

Related

object

May be any object, including simple, complex and list objects. In some cases may even include symbols and null.

{% query_param_link %}

Builds a link by combining the query parameters from the current request with the query parameters provided to this method as key:value pairs.

{{ request }}

The request object is available on every page, and contains information regarding the HTTP request that may be useful for serving and rendering the page.

string

Can be any text, from the empty string ("") to the full HTML output of the template. When used alone in a conditional, all strings evaluates as true - even if they are empty or a value such as "0" or "false".

number

Can be any number, including integers, decimals, or the value 0. Any value - including 0 - evaluates as true when used alone in a conditional.

{{ url }}

Field containing a URL.

{{ post_params }}

Object containing all of the HTTP posted parameters from the current request.

{{ time }}

Represents a specific instant in a specific timezone.

integer

A whole (non-fractional, non-decimal) number. May be 0. Any value - including 0 - evaluates as true when used alone in a conditional.

empty

Represents the empty string (""). When used alone in a conditional, evaluates as true, but when used with the is_valid conditional, evaluates as false.

list

An enumerable list containing zero or more objects. Lists may contain many different object types, although most lists only contain a single object type. There are a large number of other complex object types that are also lists (ag: an articlelist is a complex object but is also a list of article objects).

Examples

Getting request parameters and fetching blog_posts

Copy
{% assign var postsPerPage = 10 %}
{% assign pageParam = 1 %}
<!-- Did a page param get passed in the url? -->
{% if request.query_params['page'] %}
{% assign pageParam = request.query_params['page'] | to_int %}
{% endif %}
<!-- Did a tag param get passed in the url? -->
{% 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 %}

Convert a query parameter to a boolean value with a default value of true.

Convert to boolean

Copy
{% var show_buttons = request.query_params['show_btns'] | to_boolean | default:true %}

Convert a query parameter to an integer value with a default value.

Convert to integer

Copy
{% var limit = request.query_params['limit'] | default: 20 | to_int %}

Convert a query parameter to a number with a default value.

Convert to number

Copy
{% var average = request.query_params['average'] | default: 2.5 | to_number %}

Query Param Link

Copy
Original URL: "https://www.domain.com/path/to/page"
Template: <a href="{% query_param_link = page:2 %}">Page 2<a>
Output: <a href="https://www.domain.com/path/to/page?page=2">Page 2<a>

Search with query parameters and limit result length

Copy
{% var page = request.query_params.p | default:1 %}
{% var limit = request.query_params.l | default:25 %}
{% search set results request.query_params.q page:page limit:limit %}