Variables Overview
Context-aware variables are always available in your views, giving you access to dynamic information about the current URL, user, loaded entry, site settings, and more.
Overview#
Where appropriate, Statamic will inject data automatically for you to use in your views.
For example, when a view is loaded, you get automatic access to the variables applicable to it. You don't need to use a tag to "get" the data. If you're viewing an entry's URL, all of the entry variables will just be there.
If you use a tag that does supply some data, it will typically make those variables available.
The collection tag will loop over entries, again giving you access to entry variables within the loop. The assets tag gives you asset variables, the taxonomy tag gives you term variables, and so on.
The same is true for within tag pairs of augmented values. Looping through a field configured to use an assets fieldtype? You'll be getting asset variables.
Reaching into the cascade#
Let's say you're on an entry's URL and you're looping through related entries. Within the loop, you'd have a {{ title }} which would be for the entry in that loop. But what if you want to get the {{ title }} from further up your view?
The current page scope#
Variables for the current page will be aliased into a page array. You can access this any time by prefixing a variable with page:.
{{ related_posts }}
{{ title }} // The title of the entry in the loop.
{{ page:title }} // The title of the entry used when loading the URL.
{{ /related_posts }}
Explicitly defined scopes#
You aren't limited to the page scope. You can use the {{ scope }} tag to take a "snapshot" of the variable context at any point of the template and use it for reference elsewhere.
For example, we can create a scope named stuff.
{{ scope:stuff }}
{{ title }}
{{ collection:blog }}
{{ title }} // The title of the entry in the loop.
{{ stuff:title }} // The title variable at the time the scope tag was used.
{{ /collection:blog }}
{{ /scope:stuff }}
Globals#
You can create your own Global variables, which all get injected into the variable cascade, ready to be used in your views.
View front-matter#
Inside Antlers views, you may define YAML front-matter. This may be a handy way to define variables without needing to add anything to content or blueprints.
To access this data, prefix the variables with view:.
---
foo: bar
---
{{ view:foo }}
bar
You must define any front-matter variables at the top of the view file, even before things like Antlers comments.
Available variables#
The following groups of variables are available in your views, depending on their context.
| Type | Description |
|---|---|
| Basename |
The basename of the asset, which is the filename with the extension. |
| Collection |
Get the name of the collection the entry belongs to. |
| Config |
Access configuration values from Statamic and Laravel config files. |
| CSRF Field |
Outputs the CSRF token inside a hidden field named |
| CSRF Token |
Output the CSRF token from the session. |
| Current Layout |
The name of the layout currently in use. |
| Current Template |
The name of the template currently in use. |
| Current Uri |
The current URI (URL without domain). |
| Current Url |
The current URL. |
| Current User |
The current user. |
| Date |
Get the date of the entry as a Carbon instance, formatted according to system settings. |
| Datestamp |
Get the timestamp of the entry as an integer. Alias of |
| Datestring |
Get the pre-formatted date of the entry as a string. |
| Edit Url |
Get the URL to edit the current page or entry in the Control Panel. |
| Entries Count |
Get the number of entries that use this taxonomy term. |
| Environment |
Outputs the current environment (the value of |
| Extension |
The file extension of the asset. |
| Filename |
The filename of the asset, without the extension. |
| Focus |
The focal point of the asset, defaulting to center (50-50). |
| Focus Css |
The focal point of the asset in a format suitable for the background-position CSS property. |
| Get |
An array of GET variables from query strings in the current URL. |
| Get Post |
Combines both |
| Has Timestamp |
A boolean indicating whether an entry is time-based. |
| Height |
The height of an image asset, in pixels. |
| Homepage |
The URL of the homepage. Usually the same as |
| Id |
The unique identifier of the content. |
| Is Asset |
A boolean indicating whether the current content is an asset. |
| Is Entry |
A boolean indicating whether the current content is an entry. |
| Is Homepage |
A boolean indicating whether you're on the homepage. |
| Is Image |
A boolean indicating whether the asset is an image. |
| Is Term |
A boolean indicating whether the current content is a term. |
| Is Video |
A boolean indicating whether the asset is a video. |
| Last Modified |
The last modified time for the content file or asset. |
| Last Segment |
Get the last segment of the current URL. |
| Live Preview |
A boolean indicating whether the current page is being viewed in Live Preview. |
| Logged In |
A boolean indicating whether the visitor is logged in. |
| Now |
The current date/time, formatted according to your display timezone and default time format. |
| Old |
An array of sanitized variables POSTed from the previous request, useful for form validation errors. |
| Order |
Get the order key of the content (the value at the beginning of the filename). |
| Order Type |
Get the order type of an entry (date, alphabetical, or number). |
| Path |
The path to the file, relative to the asset container. |
| Permalink |
Get the absolute URL to the content, including your site URL. |
| Post |
An array of sanitized POST variables from form data submitted to the current URL. |
| Published |
A boolean indicating whether the content is published (not a draft). |
| Response Code |
The HTTP response code of the request (200 for existing URLs, 404 for non-existent ones). |
| Segment X |
Get a specific URL segment by number (e.g., |
| Site |
The current site being targeted in the request, available as a single tag or tag pair. |
| Sites |
A collection containing all configured sites as Site objects that can be looped over. |
| Size |
The file size of the asset in a human-readable format. |
| Size Bytes |
The file size of the asset in bytes. Also available as |
| Size Gigabytes |
The file size of the asset in gigabytes. Also available as |
| Size Kilobytes |
The file size of the asset in kilobytes. Also available as |
| Size Megabytes |
The file size of the asset in megabytes. Also available as |
| Slug |
The string that identifies your content, usually appearing at the end of the URL. |
| Taxonomy |
Get the name of the taxonomy the term belongs to. |
| Timestamp |
Get the timestamp of the entry as an integer. Alias of |
| Url |
Get the relative URL to the content (does not include the site URL). |
| Width |
The width of an image asset, in pixels. |
| Xml Header |
Outputs an XML header tag, useful when Statamic's template parser encodes PHP tags. |