Accept URL query parameters as TT variables in a page.
Declare which query parameters a page accepts in front matter. Declared
parameters are URL-decoded, HTML-escaped, and made available as TT
variables via the query hash. Undeclared parameters are silently
ignored.
---
title: Search
query_params:
- q
- page
---
[% IF query.q %]
Searching for: [% query.q %]
[% END %]
Use a YAML list under query_params: in front matter:
---
query_params:
- name
- color
---
Parameters appear in the query hash:
[% query.name %]
[% query.color %]
---
title: Greeting
query_params:
- name
---
[% IF query.name %]
Hello, [% query.name %]!
[% ELSE %]
Hello, visitor!
[% END %]
Request /greeting?name=Alice renders "Hello, Alice!".
&, <, >, " and
' arrive as HTML entities, and a line break as . So
[% query.name %] is safe as text and inside a quoted attribute, with
no filter.| html. It escapes the value a second time: an & in
the URL then shows as & on the page, and a hidden field posts it
back that way. The same applies to params.*, which is the same values.prefill: - and that
feature escapes it where it writes the field.| url_safe. Escaping keeps
a value inside its attribute; it does not change javascript: into
anything else. Write <a href="[% query.next | url_safe %]"> (and the
same for src and action): a value with no scheme - a path, ?query,
#fragment or //host - or one starting http:, https:, mailto:
or tel: passes unchanged, and anything else becomes #. The filter
reads the scheme through character references and percent-encoding, so
javascript: and java%73cript: become # too. It does not
escape the value again.%XX hex sequences and + for spaces)Pages with active query parameters (a declared parameter is present in the request URL) are rendered dynamically on each request and the result is not cached. Requests without query parameters still serve from cache normally.
query_params does not
disable caching for requests without parametersapi: true for JSON endpoints that accept parameters