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
Available Variables
The following groups of variables are available in your views, depending on their context.
Content
"Content" encompasses data types that can be mapped to a URL. These include Entries and Taxonomy Terms. Any time you encounter one of these, the following variables will be available, as well as any fields you've defined in the blueprint.
current_layout | |
current_template | |
edit_url | |
id | |
last_modified | |
permalink | |
published | |
slug | |
url |
Entry
Entries have access to all the Content variables, and these:
collection | |
date | |
datestamp | |
datestring | |
has_timestamp | |
is_entry | |
order | |
order_type | |
timestamp |
Term
Taxonomy terms have access to all the Content variables, and these:
entries_count | |
is_term | |
taxonomy |
System
System variables are available at any time, regardless of whether the URL you are on is for content (eg. an Entry or Term) or if it has been created using a route.
Asset
Assets have access to all the Content variables, and these:
basename | |
extension | |
filename | |
focus | |
focus_css | |
height | |
is_asset | |
is_image | |
path | |
size | |
size_bytes | |
size_gigabytes | |
size_kilobytes | |
size_megabytes | |
width | |
is_video |