Page request data
Access the URL, GET and POST information for the page request
The {{ _page.request }} object holds information about the current page request.
GET parameters
If there are any parameters in the URL then they will be held in the {{ _page.request.get }} array. (You can alternately use the {{ _page.request.query }} variable.)
An example use could be to pass parameters in the URL to set a form field value. In this example, if the URL is:
http://www.mysite.com/contact?subject=Contact+Support
In the contact form there is a field called "subject". In the form template you could do the following:
{% set form.fields.subject.value = _page.request.get.subject %}
You can also access the GET parameters as a string at {{ _page.request.queryString }}.
POST parameters
If the page request is a form submission the post values are available in the {{ _page.request.post }} array.
IP address
The current IP address for the request is held in the {{ _page.request.ip }} variable.v
Current URL
The current URL for the page request is held in the {{ _page.request.url }} variable. The _page.request.url value includes the URL path and the query string if it exists.
If you want to ensure that you are getting just the URL path then use {{ _page.request.path }}.
The base URL (the scheme and host) is held in the {{ _page.request.baseUrl }} variable. For example, for this current page, the base URL would be "https://developer.aptuitiv.com". There is no trailing slash with the base URL value.
The host portion of the URL is held in the {{ _page.request.host }} variable. For example, for this current page, the host value would be "developer.aptuitiv.com".
Referring URL
The URL that the visitor came from (if available) is held in the {{ _page.request.referrer }} variable.
Access the request headers
You can access the request headers from the browser in your templates. Use the {{ _page.request.headers }} variable to do so.
The {{ _page.request.headers }} variable holds an array of request headers. They are identical to the original request headers with one exception, any "-" characters are removed from the header name so that the data can be more easily used with Twig syntax.
For example, the "User-Agent" header is accessed using "UserAgent" instead of "User-Agent".
{{ _page.request.headers.UserAgent }}
Note that the case of the header name is important.
Most requests will probably include the following header values. Depending on the client making the request other headers could be included.
Array
(
[Host] => www.aptuitiv.com
[Connection] => keep-alive
[CacheControl] => max-age=0
[UserAgent] => Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36
[Accept] => text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
[Referer] => https://www.aptuitiv.com/contact
[AcceptEncoding] => gzip, deflate, br
[AcceptLanguage] => en-US,en;q=0.9,la;q=0.8
[Cookie] => cookieName=cookieValue
)
An example use case could be to detect if the user agent is a mobile device to redirect to another page or to show/hide certain content.
NOTE - User agent detection is brittle. However, this is a decent example of how to use the request headers.
The code below makes use of the regex_match filter. The code is based on a StackOverflow solution for Javascript user agent testing. It is by no means complete and you'd want to do some testing with specific user agents in order to confirm completeness.
{% if _page.request.headers.UserAgent|regex_match('/windows phone/i')|length > 0 %}
<p>Windows</p>
{% elseif _page.request.headers.UserAgent|regex_match('/android/i')|length > 0 %}
<p>Android</p>
{% elseif _page.request.headers.UserAgent|regex_match('/iphone|ipad|ipod/i')|length > 0 %}
<p>IOS</p>
{% else %}
<p>UNKNOWN Device</p>
{% endif %}
Test whether or not the request is an AJAX request
You can test and see if the request is an AJAX request with the following code.
{% if _page.request.isAjax %}
{# Do something here such as output the content as JSON instead of HTML #}
{% else %}
{# Do something else here such as output the content as HTML #}
{% endif %}
Test whether or not the page is a secure page
You can test whether or not HTTPS is being used on the current page with the {{ _page.request.isSecure }} variable.
{% if _page.request.isSecure %}
{# Do something here #}
{% else %}
{# Do something else here #}
{% endif %}
Test whether or not the request is a POST request
You can test whether or not the request type for the current page is a POST request with the {{ _page.request.isPost }} variable.
An example use case could be to show additional help information on a login page if the user is having trouble logging in.
{% if _page.request.isPost %}
{# Do something here #}
{% else %}
{# Do something else here #}
{% endif %}