Skip to main content

Overview

SellAuth renders templates with Nunjucks, a Jinja-style template language for JavaScript. It is used in two places:
  • Storefront themes, in every .njk file of a theme.
  • Custom email templates, which use the same language with a different set of variables and helpers.
This page covers the language itself, then the parts specific to each. For the SellAuth-specific filters and functions, see Filters and Globals.

Language basics

Output

Missing values render as an empty string rather than erroring, so a typo in a variable name fails silently.

Conditionals

There is also an inline form, which is how almost every setting fallback in the official themes is written:

Loops

Inside a loop you get loop.index (1-based), loop.index0, loop.first, loop.last, and loop.length. Objects can be iterated as key and value pairs:

Variables

The block form captures rendered output into a variable:
A {% set %} inside a {% for %} or {% if %} block does not leak out of it. If you need a value after the block, set it before and reassign inside.

Macros

Macros are reusable markup within a single file. They are file-local, you cannot call a macro defined in another file.
To share markup across files, use a snippet instead.

Comments

Use these rather than HTML comments for anything a visitor should not see. HTML comments end up in the response.

Filters

Filters transform a value with | and can be chained and given arguments:
Built-in filters worth knowing:

The dictionary switch

Nunjucks has no switch. The official themes map a setting value to a class or a token with a dictionary lookup plus a fallback, and it is worth adopting because it keeps components readable:

Escaping

Autoescaping is on. Everything you output with {{ }} is HTML-escaped unless you explicitly mark it safe.
Use | safe only on values you generate or that the platform documents as HTML. Never apply it to free-text a merchant or customer typed in. That is how a stored cross-site scripting hole gets built.

Storefront themes

Rendering components and snippets

Themes compose pages with two SellAuth-specific tags.
If the target file does not exist, the literal text Component not found or Snippet not found is rendered in its place. The page still loads, which makes this easy to miss.

Scope rules

The two tags scope differently, and this catches people out. Components get a fresh context. A component receives its own properties, componentId, the page data, and global. It does not see variables you set in the file that rendered it.
Pass configuration to a component through its properties in schema.json, not through template variables. Snippet arguments are merged into the calling context and stay set. After a render_snippet call, its arguments are still in scope for whatever comes next in the same file.
Two conventions handle this, both used throughout the official themes:
  1. Prefix snippet arguments with the snippet’s name, for example header_title, header_align, so they cannot collide with anything else.
  2. Pass every argument explicitly on every call, including the ones you want empty, rather than relying on them being unset.
Document the arguments in a comment at the top of the snippet:
snippets/section-header.njk

Email templates

Custom email templates use the same Nunjucks language, with two differences. They are structured as named blocks rather than a layout plus a body:
And they have a different helper set. Email templates get no SellAuth theme filters. They have helpers.date and helpers.price only, listed in Filters and Globals. See Email Templates for the templates you can override, the variables each one receives, and how the shared layout works.