Skip to content

Latest commit

 

History

History
57 lines (36 loc) · 1.62 KB

README.md

File metadata and controls

57 lines (36 loc) · 1.62 KB

Craft Query Strings

A Craft CMS plugin to provide twig filters and functions to help with query string management. This is especially helpful for adding query strings back to pagination.

version 1.0.7

preserveQueryStrings

The Preserve Query String filter allows you to add |preserveQueryStrings to any URL output in twig, and it will keep the query strings as they should appear in the URL.

E.g.

{% if pageInfo.prevUrl %}<a href="{{ pageInfo.prevUrl|preserveQueryStrings }}">Previous Page</a>{% endif %}
{% if pageInfo.nextUrl %}<a href="{{ pageInfo.nextUrl|preserveQueryStrings }}">Next Page</a>{% endif %}

getQueryStrings

Pull an array of query strings from Craft. This gets around the problem of duplicated query string keys being lost, turning them into an array you can loop through.

An array will be returned with objects. Use .key and .value.

Return all URL queries

{% for query in getQueryStrings() %}
  {{ query.key }} - {{ query.value }}
{% endfor %}

Return only URL queries that match a key

{% for query in getQueryStrings('lookForKey') %}
  {{ query.key }} - {{ query.value }}
{% endfor %}

getQueryFormFields

Sometimes you want to use query fields in a form, to preserve these values you can use the following in your templates.

{{ getQueryFormFields() }}

which is shorthand/equivalent to:

{% for query in getQueryStrings() %}
  <input type="hidden" name="{{ query.key }}" value="{{ query.value }}">
{% endfor %}