Skip to content

Until 28 October: open your store and get 3 months of Pro free.

Create your store →
Docs
All topics

Docs Custom themes

Functions, filters and limits

The functions a custom theme can call, the Twig tags and filters it may use, and the limits that keep every store fast.

Besides the variables, a template can use the functions below and most of Twig's own tags and filters.

Our functions

Function What it does
url('news') The address of a page of your store, like url('product/' ~ product.slug).
asset('theme.css') The address of a CSS or JS file of your theme. theme.css and theme.js are added to every page for you; use asset() for other files.
money(product.price_cents) A price in your currency. It also shows the estimate in the player's own currency, like in our themes.
t('Add to cart') Text in the player's language, when we have it, else the English. With values: t('From :date', { date: '3 Nov' }).
tc(':count vote|:count votes', 3) The same for text that depends on a number: the singular before the bar, the plural after it.
icon('chest') One of our 8 by 8 pixel icons, in the colour of the text: chest, crown, chat, globe, coin, heart and more. Give it a size: icon('crown', 'size-4').
cart_button(product) The button that puts a product in the cart. Give it your own text and class: cart_button(product, 'Buy now', 'my-button'). Without JavaScript it opens the checkout with that product.
open_cart() The button that opens the cart, with the number of items. Also open_cart('Basket', 'my-class').

Your own JavaScript

Put your JavaScript in theme.js. Our pages run on Alpine.js, so you can use its attributes in your templates as well, like x-data and x-on:click. A <script> in a template does not run: that keeps a template, or someone who gets into your account, from adding scripts you do not know about.

What Twig gives you

Tags: if, for, set, block, extends, include, embed, macro, import, from, apply, with, use and do.

Filters: abs, batch, capitalize, column, date, date_modify, default, escape (e), filter, find, first, join, json_encode, keys, last, length, lower, map, merge, nl2br, number_format, reduce, replace, reverse, round, slice, sort, split, striptags, title, trim, upper and url_encode.

Functions: attribute, block, cycle, date, include, max, min and parent.

Tests: defined, divisible by, empty, even, odd, iterable, mapping, sequence, null (none), same as, true and false.

What is not there, and why

Not allowed Why
raw and autoescape Everything is escaped, so nobody can put HTML in your page through a product name. HTML we made safe, like a news post, prints as HTML already.
range() and 1..10 Both make a list of any length. Loop over your data instead, and use slice to take part of it.
format It can build text of any size. Use ~ to join text, and number_format for numbers.
source, constant, random, invoke They reach outside the template or make a page different on every visit.
A method on a value, like product.save() Templates get plain values only.

Limits

Every page of every store has to be fast, so a template has a few limits. You only hit them when something runs away:

  • 20,000 loop passes per page. A shop of a hundred products in a few loops stays far below.
  • 40 levels deep: templates, blocks and macros inside each other. A template that includes itself stops here.
  • 2 MB per page, 100 kB per file, 30 files per theme.

A page that hits a limit shows in your built-in theme, and the dashboard says where.

Still stuck? Ask in our Discord or mail us at hello@minecraftstore.io, and a person answers.