# The Twig sandbox

What a theme template can do, what it cannot, and how to write Twig that works in it.

Theme templates run in a Twig sandbox. A theme is code from outside the platform rendered for every visitor of a store, so the sandbox decides exactly what it can reach: the data of **this** store, through read-only objects, and nothing else.

## What is allowed

- The [tags](../reference/tags.md): `if`, `for`, `set`, `apply`, `with`, `verbatim`, `do`, plus the theme tags `schema`, `stylesheet`, `javascript` and `doc`.
- The [filters](../reference/filters.md): common Twig filters and the ShopDepot ones - `money`, `image_url`, `asset_url`, `t` and others.
- The [functions](../reference/functions.md): rendering (`content_for`, `render`, `sections`, `render_widget`), platform components, data queries and `url_for`.
- Reading [objects](../reference/objects/index.md) and calling their methods.

Everything else is refused. A template that uses something outside these lists **does not save** when you write it with the code editor, the [CLI](cli.md) or the [theme API](theme-api.md) - the error comes back with the file name and line, not on a live page. A ZIP upload is checked for syntax and the file contract; run `shopdepot theme check` before you upload one, so sandbox errors do not wait until a page renders.

## What is not allowed, and what to use instead

| Not available | Use instead |
| --- | --- |
| `{% extends %}`, `{% block %}` | One layout (`layout/theme.twig`) around JSON templates. |
| `{% include %}` tag, `{% macro %}`, `{% import %}`, `{% embed %}` | Snippets: `{{ render('snippet-name', { … }) }}`. |
| Reading object properties directly | Getters through the dot: `product.title`. |
| PHP functions, files, the network, `{% sandbox %}`, `{% use %}` | The [data functions](../reference/functions.md#data) for store data, the [JSON APIs](javascript-apis.md) in the browser. |
| Filters and functions not in the reference | Ask for them - if a theme needs something often, it belongs in the platform. |

## Objects only

The sandbox allows method calls on platform objects and on nothing else. Arrays and hashes - `section.settings`, `cart.discounts` entries, `pagination` - are read with the dot as usual. Entities, services and repositories never reach a template, so a theme cannot read another store's data or call anything that writes.

## Names are camelCase

Object fields are getters, and Twig maps `product.featuredMedia` to `getFeaturedMedia()`. Write the camelCase name from the reference. `product.featured_media` does not exist and prints nothing.

## Escaping

Output is escaped as HTML automatically. Use `|raw` only for HTML the platform already sanitised - offer descriptions, page and post content, `richtext` and `html` settings, `panelHtml` of a menu link. Never put `|raw` on text from a setting of another type, a URL or a translation that takes parameters.

To pass data to a script, build a hash of the values you need - objects themselves do not serialise - and escape it for the attribute:

```twig
<div data-product="{{ { id: product.id, price: product.price, available: product.available }|json_encode|e('html_attr') }}"></div>
```

## Undefined values

Variables that do not exist are empty, not errors - `{{ product.subtitle }}` on a field that does not exist prints nothing. That keeps a page alive when data is missing, and it also hides typos. Check names against the reference.

## Snippets have their own scope

`render()` gives a snippet only what you pass, plus `shop`, `settings` and `cart`. Variables of the calling section do not leak in, so a snippet behaves the same wherever it is used:

```twig
{# in a section #}
{% for item in products %}
  {{ render('product-card', { product: item, show_rating: section.settings.show_rating }) }}
{% endfor %}
```

A missing snippet renders nothing instead of breaking the page.

## When a section fails

An error while rendering one section - a division by zero, a broken loop - is logged and that section renders empty. The rest of the page is served. Watch for empty bands in preview; the code editor shows syntax errors before they get that far.
