Data and expressions
What a Sneferu template can read: scope, lookups, product fields, output, expressions, truthiness, filters, loops and translations.
On this page
Templates read data with {{ expression }} and control output with
{% if %}, {% for %} and {% render %}. There is no arithmetic, no
assignment and no function calls besides the lookups below: a template cannot
run code. The full grammar is in language.md.
What a template can read
| Name | Where | Contains |
|---|---|---|
store | everywhere | the store, e.g. store.name |
settings | everywhere | theme settings from theme.json settings, defaults filled in and coerced: settings.accent |
section | sections, their blocks and snippets | section.id, section.type, section.settings.*, section.blocks |
block | blocks | block.id, block.type, block.settings.* |
locale | everywhere | ar or en |
dir | everywhere | rtl (Arabic) or ltr |
forloop | inside {% for %} | forloop.index (from 1), forloop.first, forloop.last |
loop variable, render arguments | where you name them | e.g. p in {% for p in … %} |
Lookups
Lookups take literal arguments (collection('featured')). Some take more:
collection() also takes a collection setting, so the merchant can choose the
collection (see
Let the merchant choose a collection),
product() a product setting, menu() a menu setting (see
Menus), and url() takes any one expression (see Links). Any other argument,
such as collection(product.title), is SN1105.
| Lookup | Returns |
|---|---|
collection('featured') | the collection: .title, .products. A missing collection is { products: [] }, never an error |
collection('related') | on a product page, the other products of the current product's category (the product itself left out, at most 8); empty elsewhere. This is how a theme shows related products, since collection(product.category.handle) is SN1105 |
collection(section.settings.list) | the collection the merchant picked in a collection setting; an empty collection when none is picked |
collections() | the store's categories (at most 50), see List the store's categories |
product('handle') | the product, or nothing |
menu('main') | the store's menu: .title, .items (each { title, url, active, child_active, children }). Takes a handle or a menu setting: menu(section.settings.menu). A missing menu is { handle, title: '', items: [] }, never an error. See Menus |
t('shop_now') | the text from locales/<locale>.json, then the default locale, then the key itself |
settings('accent') | one theme setting (same as settings.accent) |
asset_url('logo.svg') | the URL of assets/logo.svg |
url('/products') | a link for this page: /en/products on an English page, /products on an Arabic one. Takes a literal or any expression: url(section.settings.link) |
Product fields
A product (from collection(…).products or product(…)) has:
| Field | Example |
|---|---|
id | "8f1c…" (stable; do not show it) |
handle | ps-card |
title | PlayStation gift card |
url | /products/ps-card |
price | 52000 (minor units: piastres; 52000 = 520.00 EGP) |
compare_at | 60000 or nil (the price before a discount) |
image | { url: "https://…" } |
available | true when it can be bought |
description | <p>The code arrives <strong>by email</strong>.</p>: sanitised HTML in the page's language. {{ product.description }} prints it escaped, as text; output it as HTML with <hk-rich-text html="{{ product.description }}" /> |
excerpt | The code arrives by email. (plain text, at most 200 characters) |
kind | physical or digital |
digital_kind | code, file or manual for a digital product; nil for a physical one |
tags | ["gift-card", "gaming"] (at most 20, each at most 40 characters) |
category | { handle: "gift-cards", title: "Gift cards", url: "/category/gift-cards" }, or nil |
<hk-product-card product="{{ p }}">
<hk-part name="badge">{% if p.kind == 'digital' %}<span class="tag">{{ 'instant' | t }}</span>{% endif %}</hk-part>
</hk-product-card>
<hk-rich-text html="{{ product.description }}" />
<p>{{ product.excerpt }}</p>In sneferu dev every sample product has all of these fields.
Output
<h2>{{ section.settings.title }}</h2>
<p>{{ store.name }} · {{ t('shop_now') }}</p>
<a class="card {{ section.settings.tone }}" href="{{ p.url }}">{{ p.title }}</a>- Output is escaped for where it lands: text, attribute or URL. You cannot inject HTML through a setting.
- URL attributes (
href,src, …) go through a safe-URL check:javascript:and other unsafe values become#. - Dynamic
classandidare reduced to letters, digits,-,_and spaces. - Strings, numbers and booleans print as text (
true); objects and arrays print nothing. Missing data prints nothing; it never throws. - An attribute can mix text and expressions:
class="tier {{ section.settings.size }}". - On
hk-*tags, an attribute that is exactly one{{ }}passes the raw value (a number, an array of products, an object). Mixed text makes a string.
Expressions
| Form | Example |
|---|---|
| Literals | 'text', "text", 42, true, false, nil |
| Property | section.settings.title, p.image.url |
| Index | collection('featured').products[0].title |
| Size | section.blocks.size, p.title.size |
| Compare | ==, != (strict), >, <, >=, <= (numbers only; false otherwise) |
| Logic | and, or, not, parentheses |
| Filters | value | filter, value | filter: arg1, arg2 |
{% if p.compare_at > p.price %}<hk-badge text="{{ t('sale') }}" tone="warning" />{% endif %}
{% if section.settings.layout == 'wide' and not section.settings.compact %}…{% endif %}>/< compare numbers only: '10' > 9 is false. Prices are numbers, so price
comparisons work.
Truthiness
In {% if %}, {% elsif %}, and, or, not:
| False | True |
|---|---|
nil (missing), false, 0, '' (empty string), [] (empty array) | everything else, including '0', ' ' and {} |
So {% if collection('featured').products %} is false for an empty or missing
collection, and {% if section.blocks %} is false when the merchant added no
blocks. Use that for empty states:
{% if collection('featured').products %}
<hk-product-grid products="{{ collection('featured').products }}" />
{% else %}
<hk-text text="{{ t('collection_empty') }}" align="center" />
{% endif %}{% else %} comes once, last (SN1109).
Filters
| Filter | Input → output |
|---|---|
money | price in minor units → formatted price in the store currency. {{ 129000 | money }} → EGP 1,290.00. Other currencies use the ISO code: USD 1,290.00. On an Arabic page the currency comes after the number. Western digits. Options: see Money formats |
img | image (object with url, or a URL) → image URL, optionally sized: {{ p.image | img: 400 }} |
date | ISO date → long date: {{ '2026-10-10' | date }} → 10 October 2026 |
t | key → translation: {{ 'shop_now' | t }} (same as t('shop_now')) |
upcase / downcase | {{ 'egp' | upcase }} → EGP |
truncate | {{ p.title | truncate: 20 }} → at most 20 characters, ending in … (default 50) |
default | {{ section.settings.eyebrow | default: 'New' }} → the fallback when the value is nil or '' |
size | {{ section.blocks | size }} → 3 (arrays and strings; 0 otherwise) |
first / last | first / last array item: {{ tags | first }}. A filter result cannot be followed by .property; for a field of the first product write collection('featured').products[0].title |
join | {{ tags | join: ' · ' }} → a · b (default separator , ). On a missing value it prints its argument, so guard it: {% if tags %}…{% endif %} |
escape | converts to a string (output is escaped anyway) |
json_attr | value → JSON for a data-* attribute: data-ids="{{ ids | json_attr }}" |
tag_values | tags → the values of the tags written prefix:value: {% for n in product.tags | tag_values: 'top' %}…{% endfor %} lists Bergamot for the tag top:Bergamot. The prefix match ignores case; values are trimmed; empty and repeated ones are dropped; at most 20. A value may carry its Arabic after a bar (top:Bergamot|برغموت): Arabic pages show the part after the bar, English pages the part before it |
Filters chain left to right: {{ p.title | truncate: 30 | upcase }}.
Prices are always integers in minor units. Never divide or format them yourself;
use money or <hk-price amount="{{ p.price }}" compare-at="{{ p.compare_at }}" />.
Dates and prices always use Western digits, in every locale, matching the rest of Hekana.
Money formats
money takes two optional arguments: the format and the unit.
| Expression | Arabic page | English page |
|---|---|---|
{{ 129000 | money }} | 1,290.00 ج.م | EGP 1,290.00 |
{{ 129000 | money: 'whole' }} | 1,290 ج.م | EGP 1,290 |
{{ 129050 | money: 'whole' }} | 1,290.50 ج.م | EGP 1,290.50 |
{{ 120 | money: 'whole', 'major' }} | 120 ج.م | EGP 120 |
- Format:
'decimal'(the default) always prints two decimals;'whole'drops.00when the amount is a whole pound and keeps two decimals otherwise. - Unit:
'minor'(the default) reads the input as piastres;'major'reads it as pounds. Product prices are always minor units, so'major'is for numbers you wrote yourself. The unit is the second argument, so'major'always needs a format before it:money: 'decimal', 'major'ormoney: 'whole', 'major'.
To make whole the default for the whole theme, set it in theme.json:
"money": { "format": "whole" }It applies to money without a format, to <hk-price> and to the price inside
<hk-product-card> and <hk-product-grid>. <hk-price format="decimal"> (or
"whole") overrides it for one tag, and money: 'decimal' for one expression.
With the whole format <hk-price> wraps the number in <bdi>, so it reads
correctly next to ج.م on Arabic pages; with the default format its markup is
unchanged. Anything other than decimal or whole in theme.json is SN3033.
Links
A link written in a template is used as written: href="/products" goes to the
Arabic page even when the shopper is on English. Wrap store links in url() so
they keep the page's language:
<a href="{{ url('/products') }}">{{ t('all_products') }}</a>
<a href="{{ url(section.settings.link) }}">{{ section.settings.label }}</a>| On an English page | Result |
|---|---|
url('/products') | /en/products |
url('/en/products') | /en/products (already prefixed) |
url('https://example.com'), url('mailto:…'), url('tel:…'), url('#reviews') | unchanged |
url('javascript:…'), url('//evil.example'), an empty value | # |
On an Arabic page store paths are unchanged (Arabic is the default language and
has no prefix). On the store, Hekana also adds whatever the store's address needs;
the result is always checked with the same URL rules as href (see
Security and limits). url() takes exactly one
argument.
<hk-button href="/products"> does this for you: its link always follows the
page's language. Product and category URLs (p.url, c.url) already include
the prefix, so they do not need url().
Loops
A short list of the first three products of a collection, numbered, with a "Sale" label on any product that has a lower price than before.
<hk-section padding-y="64" mobile-padding-y="40">
<hk-stack gap="16">
<hk-heading text="{{ section.settings.heading }}" level="h2" size="32" mobile-size="24" />
{% for p in collection('featured').products limit: 3 %}
<hk-card border="true" padding="16" radius="12">
<hk-stack direction="row" justify="between" align="center" gap="12">
<hk-text text="{{ forloop.index }}. {{ p.title }}" size="17" />
<hk-stack direction="row" gap="8" align="center">
{% if p.compare_at > p.price %}
<hk-badge text="{{ t('sale') }}" tone="accent" />
{% endif %}
<hk-price amount="{{ p.price }}" compare-at="{{ p.compare_at }}" />
</hk-stack>
</hk-stack>
</hk-card>
{% endfor %}
</hk-stack>
</hk-section>
<hk-schema>
{
"name": "Product list",
"version": 1,
"settings": {
"heading": {
"type": "text",
"label": "Heading",
"default": "Today's picks"
}
}
}
</hk-schema>What each line does
- Line 4
{% for p in collection('featured').products limit: 3 %}repeats lines 5-15 once for each of the first three products. Inside,pis the current product. - Line 7
{{ forloop.index }}is 1, 2, 3 and{{ p.title }}is the product's name. - Lines 9-11
p.compare_at > p.priceis true when the product had a higher price before; only then the "Sale" badge shows. Its words come from thesalekey in your locale files, and its look from the badge rules in the starter'sassets/theme.css. - Line 12
<hk-price>prints the price, and the old price crossed out when there is one. - Line 16
{% endfor %}ends the repeated part.
| Rule | Value |
|---|---|
Default limit (no limit:) | 50 items |
Largest limit: | 250 (a limit below 1 or above 250 is SN2007) |
| Iterations per page, all loops together | 2,000 |
| Looping over something that is not an array | renders nothing |
forloop.index | 1, 2, 3 … |
forloop.first / forloop.last | true on the first / last item |
There is no forloop.length; use {{ list | size }} or list.size.
Loop limits from settings
limit: may also read a number or range setting, so the merchant chooses
how many items show:
{% for p in collection('featured').products limit: section.settings.count %}The setting is section.settings.<name>, block.settings.<name> or
settings.<name>; anything else is SN1105, and a setting that does not exist
or is not a number or range is SN2012. At render time a value above 250
uses 250, and a value below 1 or not a whole number (empty, text, 2.5, 0,
-3) uses the default limit, 50.
Let the merchant choose a collection
A collection setting gives the merchant a collection picker in the builder.
Pass the setting to collection() and the section shows whatever they pick:
<hk-section padding-y="48">
<hk-stack gap="16">
<hk-heading text="{{ collection(section.settings.list).title }}" level="h2" />
{% for p in collection(section.settings.list).products limit: section.settings.count %}
<hk-product-card product="{{ p }}" />
{% endfor %}
</hk-stack>
</hk-section>
<hk-schema>
{
"name": "Collection shelf",
"version": 1,
"settings": {
"list": {
"type": "collection",
"label": "Collection",
"default": "featured"
},
"count": {
"type": "range",
"label": "Products",
"min": 1,
"max": 12,
"default": 4
}
}
}
</hk-schema>| Rule | Value |
|---|---|
| Argument | a handle in quotes, or section.settings.<name>, block.settings.<name>, settings.<name> |
| The setting | must exist and be of type collection (SN2012). In a section file block.settings.* is not checked, because the block type is only known on the page |
| Nothing picked (empty value) | an empty collection: the loop renders nothing, nothing is fetched |
| Before rendering | the store fetches every collection the page can show, the picked ones and their defaults included, up to 8 per page |
List the store's categories
collections() takes no arguments and returns the store's categories, at most
50, for a category menu or a grid of category tiles:
<nav>
{% for c in collections() %}
<a href="{{ c.url }}">{{ c.title }} ({{ c.count }})</a>
{% endfor %}
</nav>| Field | Example |
|---|---|
handle | gift-cards |
title | Gift cards (in the page's language) |
url | /en/category/gift-cards (with the language prefix of the page) |
image | { url: "https://…" }, or nil when the category has no image |
count | 6 (the number of products) |
parent | the handle of the parent category, or nil for a top-level one |
children | the sub-categories of this one (each with the same fields), empty when it has none |
collections() lists every category flat, sub-categories included. To show a
tree, list the top-level ones and walk their children:
{% for c in collections() %}
{% if not c.parent %}
<a href="{{ c.url }}">{{ c.title }}</a>
{% for s in c.children %}<a href="{{ s.url }}">{{ s.title }}</a>{% endfor %}
{% endif %}
{% endfor %}A store with no categories returns an empty list. In sneferu dev the sample
store has three categories: new, gift-cards and subscriptions.
Translations
<hk-button label="{{ t('shop_now') }}" href="/products" />
<span>{{ 'free_delivery' | t }}</span>Use t() for interface words and settings for merchant content. A page uses
locales/<locale>.json and falls back to the default locale for any missing key.
Snippet arguments
{% render 'plan-card', name: section.settings.plan1_name, price: section.settings.plan1_price, best: section.settings.best == '1' %}The subscription plans in the cookbook use this.
Arguments are key: expression, separated by commas (commas inside quotes are
fine: label: 'a, b'). The snippet sees them plus the caller's scope.
