Variables
{% set %}Stores a value or query result for reuse across your template.This page shows you how to use the Variable block to store values and reuse them across your template.
When to use a Variable block
Most of the time, you don't need one. A Loop block can run queries on its own and even expose query results by name after the loop.
The Variable block exists for one main case: you need query data before the loop — like showing a total count or pagination above the list. Since blocks render in order, the loop hasn't run yet, so you need the Variable block to define the query first.
{% set products = get_posts({'post_type': 'product', 'posts_per_page': 12}) %}
<p>{{ products.found_posts }} products</p>
{{ products.pagination }}
{% for post in products %}
<h2>{{ post.title }}</h2>
{% endfor %}
If you only need pagination after the loop, skip the Variable block — the Loop block handles it.
Some expressions — like loop counters or computed values that change per iteration — may not render correctly in the editor. The editor preview is an approximation. Always verify on the frontend to confirm your variables and expressions work as expected.
Other uses
Beyond query pre-fetching, variables can store any value:
Avoid repeating long expressions
Name: my_price
Source: post.meta('sale_price') ?? post.meta('regular_price')
{{ my_price|number_format(2) }}
Store computed values
Name: display_title
Source: post.meta('custom_title') ?? post.title
{{ display_title }}
Using Variables
Once created, variables work like any other data source:
{{ my_variable }}
{{ my_variable.title }}
{{ my_variable|upper }}
{{ my_variable ? 'exists' : 'empty' }}
Common Use Cases
Avoid repeating long expressions
Create a variable img with expression post.thumbnail:
{{ img.src('large') }}
{{ img.alt ?? post.title }}
{{ img.width }}
Store computed values
Create a variable display_title with expression post.meta('custom_title') ?? post.title:
{{ display_title }}
A variable must be declared before any block that references it. If you place a Variable block after a block that uses {{ my_var }}, the value won't be available yet. Order matters — put your Variable blocks early in the template.
Next steps
- Loop — use query variables as a loop data source
- Expressions & Syntax — full reference for operators and filters
- Common Patterns — ready-to-use expression examples