# Functions

Every function a theme can call: rendering blocks, snippets and widgets, platform components, data queries and store addresses.

<!-- Generated by `bin/console app:dev:reference` from the platform code and docs/dev/_data/. Do not edit by hand. -->

Functions are called on their own: `{{ content_for('blocks') }}`. Data functions always read the current store, so a section never sees another store's data.

## Rendering

### `content_for`

Renders the blocks of the current section or block, in the merchant's order. Use it where the blocks go; the platform adds the editor attributes and the block wrappers.

```twig
{{ content_for('blocks') }}
```

Example:

```twig
<div class="hero__content">{{ content_for('blocks') }}</div>
```

### `render`

Renders a snippet by file name. The snippet sees only the variables you pass plus the globals `shop`, `settings` and `cart` - variables of the calling file do not leak in. A missing snippet renders nothing.

```twig
{{ render('snippet-name', variables = {}) }}
```

Example:

```twig
{{ render('product-card', { product: item, show_rating: true }) }}
```

### `render_widget`

Renders a widget from a `widget` setting, or by its handle or placement name. An empty placement renders nothing.

```twig
{{ render_widget(widget) }}
```

Example:

```twig
{{ render_widget(section.settings.testimonials) }}
```

### `sections`

Renders a section group - `sections/<name>.json`. Used in the layout for the header and the footer.

```twig
{{ sections('header-group') }}
```

Example:

```twig
{{ sections('footer-group') }}
```

### `placeholder_svg`

A neutral SVG placeholder for an empty image slot in the editor.

```twig
{{ placeholder_svg(name = 'product', class = null) }}
```

Example:

```twig
{{ placeholder_svg('image', 'card__placeholder') }}
```

## Platform components

### `cart_summary`

The cart summary of the platform: coupon, discounts, delivery from the cheapest method, total, VAT line and the checkout button. Use it on the cart page and style its `sd-cart-summary__*` classes.

```twig
{{ cart_summary() }}
```

Example:

```twig
<aside class="cart__summary">{{ cart_summary() }}</aside>
```

### `form`

A platform form. Today `contact`: fields, consents, validation and sending are the platform's; the theme passes CSS classes, the button label, and optionally a fixed subject, which optional fields to show and extra fields. See [Platform pages and components](../themes/platform-pages.md#the-contact-form).

```twig
{{ form('contact', options = {}) }}
```

Example:

```twig
{{ form('contact', { submit: 'Send message' }) }}
```

## Data

### `query_products`

Offers of the current store for a section that is not given a list by the page. Sources - `newest` (the default), `bestsellers` (units sold in 90 days, filled up with newest offers), `top_selling` (only offers that sold - for a "Bestseller" badge), `sale` and `preorder` (offers with a variant in pre-order, the nearest shipping date first). `category` narrows any source to a store category (an object from a setting, an id or a handle).

```twig
{{ query_products(source = 'newest', category = null, limit = 8) }}
```

Example:

```twig
{% for product in query_products('bestsellers', null, 4) %}{{ render('product-card', { product: product }) }}{% endfor %}
```

### `query_posts`

The latest published blog posts, optionally from a blog category path, without a post handle.

```twig
{{ query_posts(limit = 3, category = null, exclude = null) }}
```

Example:

```twig
{% for article in query_posts(3) %}{{ render('article-card', { article: article }) }}{% endfor %}
```

### `query_post`

One published post by its handle, or null.

```twig
{{ query_post(handle) }}
```

### `query_terms`

Values of a dictionary feature by the address of its feature page - all, or only the listed handles; `with_image` keeps values with a picture in this store; `order` is `name` or `offers`.

```twig
{{ query_terms(feature, limit = 12, handles = null, with_image = false, order = 'name', exclude = null) }}
```

Example:

```twig
{% for brand in query_terms('brands', 12, null, true) %}…{% endfor %}
```

### `query_categories`

Subcategories of a category, or the top-level categories of the store without a parent.

```twig
{{ query_categories(parent = null, limit = 12) }}
```

Example:

```twig
{% for category in query_categories(null, 6) %}…{% endfor %}
```

### `product_terms`

The values of a dictionary feature on one offer - authors under a title, for example.

```twig
{{ product_terms(product, feature) }}
```

Example:

```twig
{% for author in product_terms(product, 'authors') %}<a href="{{ author.url }}">{{ author.title }}</a>{% endfor %}
```

### `query_term_products`

Other offers sharing a dictionary value with this offer - "More from this author".

```twig
{{ query_term_products(product, feature, limit = 8) }}
```

## Addresses

### `url_for`

A store address, with the trailing slash every store address has. Routes - `home`, `cart`, `search`, `account`, `wishlist`, `blog`, and with a `handle` - `product`, `category`, `article`, `page`.

```twig
{{ url_for(route, params = {}) }}
```

Example:

```twig
<a href="{{ url_for('cart') }}">Cart</a>
```

## Twig functions

### `date`

A date object to compare with or format.

```twig
{{ date(value = 'now') }}
```

### `include`

Includes another theme file with the current variables. Prefer `render()`, which isolates variables.

```twig
{{ include(template, variables = {}) }}
```

### `max`

The largest value.

```twig
{{ max(a, b, …) }}
```

### `min`

The smallest value.

```twig
{{ min(a, b, …) }}
```

### `random`

A random item. Avoid it in cached markup: the page is built once and shown to everyone.

```twig
{{ random(values) }}
```

### `range`

A list of numbers - `{% for i in range(1, 5) %}`.

```twig
{{ range(low, high, step = 1) }}
```

