How to read query parameters and POST data and branch template logic on the request.
{%- 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 boolean
{% var show_buttons = request.query_params['show_btns'] | to_boolean | default:true %}Sets the show_buttons variable to true unless the show_btns query parameter is present and set to false.
Convert a query parameter to integer
{% var limit = request.query_params['limit'] | to_int | default: 20 %}Sets the limit variable to 20 unless the limit query parameter is present and set to a valid integer.
Convert a query parameter to number
{% var average = request.query_params['average'] | to_number | default: 2.5 %}Sets the average variable to 2.5 unless the average query parameter is present and set to a valid number.
{%- if request.post_params.is_valid -%}
{%- assign var username = request.post_params['username'] -%}
{%- assign var password = request.post_params['password'] -%}
{%- if username is_valid and password is_valid -%}
{%- auth login username password -%}
{%- endif -%}
{%- endif -%}
{%- unless auth.logged_in -%}
<p>Could not log in</p>
{%- endunless -%}{{ request.query_params }}alpha=abc&beta=b&&emptyvariable&animals=cat&animals=dog&animals=fish
{{ request.query_params.count }}4
There are 4 distinct query parameters in the URL: alpha, beta, emptyvariable, and animals. Note that the empty query parameter is NOT included in the count.
{{ request.query_params.length }}7
There are total of 7 query parameters in the URL, including one that does not have a value, one that does not have a key or a value, and one that is repeated three times
{{ request.query_params.alpha }}abc
The value of the alpha query parameter is abc
{{ request.query_params.emptyvariable }}The value of the emptyvariable query parameter is empty
{{ request.query_params['animals'] }}cat,dog,fish
The value of the animals query parameter is a comma-separated list of the values: cat, dog, and fish
{{ request.query_params[1] }}beta=b
The value of the second query parameter is beta=b
{% if request.query_params has_key 'alpha' %}has alpha{% else %}no alpha{% endif %}has alpha
The alpha query parameter is present in the URL
{% if request.query_params contains 'animals=cat' %}cat{% else %}no cat{% endif %}cat
The animals=cat query parameter is present in the URL. Note that the contains condition checks for both the key AND the value of the query parameter.
{%- for param in request.query_params %} {% comment %}same as {% for param in request.query_params.by_index %} {% endcomment -%}
{%- unless forloop.first %}, {% endunless %}{{param-}}
{%- endfor -%}alpha=abc, beta=b, , emptyvariable, animals=cat, animals=dog, animals=fish
The query parameters can be iterated using a {% for %} loop. Note that this includes empty query parameters that do not have keys or values.
{%- 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
The query parameter keys can be iterated using a {% for %} loop. Note that this includes keys that do not have values, but does NOT include query parameters that do not have keys.
{% var page = request.query_params['page'] | to_int | default: 1 %}{%- 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 -%}{%- unless request.query_params contains "show_sidebar=false" -%}
{%- if request.query_params has_key "show_sidebar" -%}
{%- include "_custom_sidebar" type:request.query_params.show_sidebar -%}
{%- else -%}
{%- include "_default_sidebar" type:request.query_params.show_sidebar -%}
{%- endif -%}
{%- endunless -%}