Variables
Leave values in a block open so each injectable can fill them in, choose the right kind for each value, and fix what saving reports.
A variable is a blank that a block leaves open. Each injectable that uses the block fills the blank with its own value.
In the example block, Free Shipping Promise, the order amount is a variable. One banner says "Free shipping over $75". A banner for a sale says "Free shipping over $50". Both use the same block.
Only Custom HTML & CSS and Template blocks can have variables.
Add a variable
- Open the block and find the Variables card.
- Press + Add variable.
- Fill in the fields below.
- Put the cursor where the value belongs in the content and press Insert on the variable's row. This writes
{{name}}at that place. - Save the block.
| Field | What to enter |
|---|---|
| Name | The name used in the content, as {{name}}. Letters, numbers and underscores. It cannot start with a number. |
| Kind | What sort of value it is. See Kinds of variable. |
| Label | The name your team sees when they fill it in. Up to 200 characters. |
| Default | The value used when an injectable does not set its own. |
| Help | A hint shown next to the field. Up to 500 characters. |
| Required | Turn on if every injectable must give a value. |

A block can have up to 50 variables. Move up and Move down change their order, and Remove deletes one.
Kinds of variable
Each kind of value may be used only where it is safe. The table shows where, with an example for the block's HTML.
| Kind | Use it for | Where it may go | Example |
|---|---|---|---|
| Text | A few words | Text, or the value of an attribute | <strong>{{headline}}</strong> |
| Long text | A sentence or more | Text, or the value of an attribute | <p>{{message}}</p> |
| Number | An amount. Has Least and Most. | Text, an attribute, or a -- style value | over ${{minimum}} |
| Colour | A colour | A -- style value only | style="--accent: {{accent}}" |
| Link | A web address to open | The href of a link | <a href="{{link}}">Shop now</a> |
| Image | The address of one picture | The src of an image | <img src="{{picture}}" alt=""> |
| On/off | A yes or no | Text, or the value of an attribute | data-compact="{{compact}}" |
| Choice | One of a list. Has Choices (one per line), up to 50. | Text, or the value of an attribute | class="notice {{size}}" |
A -- style value is a CSS custom property. Your CSS reads it with var(--accent). It is the safe way to pass a colour or a size into a block's CSS.
Where a variable cannot go
StoreSpan refuses to save a block with a variable in one of these places, because a value there could break the page or run code:
- inside a
<script>or<style>element, or on the CSS tab, - in an attribute that runs code, such as
onclick, - directly in a
stylevalue, such asstyle="color: {{accent}}". Use a--style value instead, - inside an HTML comment,
- in an attribute value without quotes, or in place of an attribute's name,
- as only part of a web address, such as
href="/sale/{{page}}".
Show content only in some cases
Wrap content in a condition to show it only when the condition is met.
{{#if minimum}}
<p>Free shipping over ${{minimum}}</p>
{{else}}
<p>Free shipping on every order</p>
{{/if}}| You write | The content shows when |
|---|---|
{{#if minimum}} | minimum has a value |
{{#if minimum gt 50}} | minimum is greater than 50 |
{{#if size eq "large"}} | size is exactly large |
The comparisons are gt (greater than), gte (greater than or equal), lt (less than), lte (less than or equal), eq (equal) and ne (not equal). When a value is missing, a comparison is never met.
{{else}} is optional. Every {{#if}} needs a closing {{/if}}.
A condition around attributes must wrap whole attributes, not part of one.
Calculate a value
You can add, subtract, multiply and divide inside a variable:
<p>Add ${{minimum - cart_total}} more for free shipping</p>Use the variable on its own too
A variable gets its value only where it stands alone as {{ name }}. If you use a variable only
inside a calculation or a condition, StoreSpan warns you when you save. Such a variable shows
nothing.
What saving checks
When you save, StoreSpan compares the content with the variables. A finding in red stops the save. A finding in amber is a warning, and the block is saved.
| Finding in red | What to do |
|---|---|
| The name is not a valid variable name | Use letters, numbers and underscores, and do not start with a number. |
| Two variables have the same name | Rename one. |
The content uses a {{name}} that is not a variable or a visitor detail | Add the variable, or correct the spelling in the content. |
| A default does not suit its kind | Correct it: a number within its limits, a real colour, a valid address, one of the choices. |
| A Choice variable has no choices | Enter at least one choice. |
| A required variable has no default, and injectables already use the block | Give it a default, so those injectables keep a value. |
| A variable, or a visitor detail, is in a place that is not allowed | Move it. See Where a variable cannot go. |
The content uses three braces, {{{name}}} | Use two braces. |
| A condition is not written correctly | Check the {{#if}}, and that it has a closing {{/if}}. |
| Finding in amber | What it means |
|---|---|
| A variable is never used in the content | Remove it, or insert it in the content. |
| A variable has the same name as a visitor detail | The block's value is used, and the visitor detail is hidden. Rename it if you want the visitor detail. |
| A variable is used only in a calculation or a condition | Its value is not filled in. Use it on its own in the content as well. |
Visitor details are the variables StoreSpan fills in about the shopper, such as the country. See Variables you can insert.
Visitor details are filled in only when the injectable reaches the page through the loader or a BigCommerce widget. With Static Page and Server-rendered delivery they are empty. See Delivery.
Rename a variable
Change the Name and save. StoreSpan rewrites {{name}} everywhere in the block's content, and the injectables that use the block keep the values they had set.
Remove a variable or change its kind
When you remove a variable, the injectables that set a value for it lose that value. When you change its kind, a value that no longer suits the kind is lost too, and the injectable uses the block's default.
Blocks made before variables had kinds
A block created before variables had kinds shows its old values as Text variables. You can change their kind.
Create a block
Create a block, choose its content type, save it, make a copy of it, and understand what saving does to the injectables that use it.
Use a block in an injectable
Make a block the content of an injectable, see every injectable that uses a block, and bring their values back in line with the block.