# Filters

Every filter a theme can use: ShopDepot filters for prices, images, assets and translations, plus the allowed Twig filters.

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

Filters change a value: `{{ product.price|money }}`. Arguments go in parentheses, by position or by name - `{{ image|image_url(width: 800) }}`. A filter that is not listed here does not exist in a theme: the template fails to save.

## ShopDepot

### `money`

Formats an amount in minor units (grosze, cents) as a price in the store language - `12900` becomes `129,00 zł`. Every price on an object is in minor units.

```twig
{{ amount|money(currency = shop.currency) }}
```

Example:

```twig
{{ product.price|money }}
```

### `money_with_currency`

Like `money`, with the ISO currency code appended - for stores that sell in several currencies.

```twig
{{ amount|money_with_currency(currency = shop.currency) }}
```

Example:

```twig
{{ cart.totalPrice|money_with_currency }}
```

### `image_url`

Address of an image at a width. Accepts a media object, a file from a setting or a plain address. The platform keeps WebP renditions from 320 to 2400 pixels wide and returns the smallest that covers the width; the heavy original is never sent to visitors.

```twig
{{ image|image_url(width = 800) }}
```

Example:

```twig
{{ product.featuredMedia|image_url(width: 1600) }}
```

### `image_tag`

An `<img>` tag with `decoding="async"`. Images are lazy by default; give the first visible image `loading: 'eager'` so it does not wait, and the main image above the fold (the LCP image) `fetchpriority: 'high'` so the browser fetches it before everything else. For an address from `image_url` the tag also gets a `srcset` with every rendition of the image, so give it an honest `sizes` (the width the image takes on the page) and the browser downloads only what the screen needs.

```twig
{{ url|image_tag(alt = '', loading = 'lazy', sizes = '100vw', class = null, width = null, height = null, fetchpriority = null) }}
```

Example:

```twig
{{ product.featuredMedia|image_url(width: 800)|image_tag(alt: product.title, loading: 'eager') }}
```

### `asset_url`

Address of a file in the theme's `assets/` directory, by its path inside `assets/`. The address contains the theme fingerprint, so browsers cache it for a year and a new theme version is fetched right away. A PNG or JPG gets a WebP copy when its file is saved (install, update, upload) and the address points at that copy whenever it is smaller - keep writing `'img/photo.jpg'|asset_url`.

```twig
{{ 'path/in/assets'|asset_url }}
```

Example:

```twig
{{ 'css/base.css'|asset_url }}
```

### `stylesheet_tag`

A `<link rel="stylesheet">` tag for an address.

```twig
{{ url|stylesheet_tag }}
```

Example:

```twig
{{ 'css/base.css'|asset_url|stylesheet_tag }}
```

### `script_tag`

A deferred `<script>` tag for an address, a module by default.

```twig
{{ url|script_tag(type = 'module') }}
```

Example:

```twig
{{ 'js/theme.js'|asset_url|script_tag }}
```

### `t`

A translation from the theme's `locales/` in the store language, falling back to `*.default.json`. Parameters replace `{{ name }}` placeholders in the text.

```twig
{{ 'key.path'|t(params = {}) }}
```

Example:

```twig
{{ 'cart.items'|t({ count: cart.itemCount }) }}
```

### `handleize`

Lowercase, with every run of other characters replaced by a hyphen - `Summer Sale!` becomes `summer-sale`.

```twig
{{ text|handleize }}
```

Example:

```twig
<div id="tab-{{ group.title|handleize }}">
```

### `strip_html`

Removes HTML tags and leaves the text.

```twig
{{ html|strip_html }}
```

Example:

```twig
{{ product.shortDescription|strip_html|truncate(140) }}
```

### `truncate`

Cuts text to a number of characters and appends `end` when it was cut.

```twig
{{ text|truncate(length = 120, end = '…') }}
```

Example:

```twig
{{ article.excerpt|truncate(160) }}
```

## Math

### `plus`

Adds a number. Kept for templates ported from Liquid; `+` works too.

```twig
{{ number|plus(other) }}
```

Example:

```twig
{{ forloop_index|plus(1) }}
```

### `minus`

Subtracts a number.

```twig
{{ number|minus(other) }}
```

Example:

```twig
{{ product.compareAtPrice|minus(product.price)|money }}
```

### `times`

Multiplies by a number.

```twig
{{ number|times(other) }}
```

Example:

```twig
{{ item.unitPrice|times(item.quantity)|money }}
```

### `divided_by`

Divides by a number; dividing by zero gives 0 instead of an error.

```twig
{{ number|divided_by(other) }}
```

Example:

```twig
{{ (product.compareAtPrice - product.price)|times(100)|divided_by(product.compareAtPrice)|round }}%
```

## Twig filters

### `abs`

Absolute value.

```twig
{{ number|abs }}
```

### `batch`

Splits a list into chunks of a size - rows of a grid.

```twig
{{ list|batch(size, fill = null) }}
```

### `capitalize`

First letter uppercase, the rest lowercase.

```twig
{{ text|capitalize }}
```

### `column`

Values of one key from a list of hashes.

```twig
{{ list|column(name) }}
```

### `date`

Formats a date - `{{ article.publishedAt|date('j F Y') }}`.

```twig
{{ date|date(format = 'd.m.Y H:i') }}
```

### `default`

The value, or the fallback when it is empty or undefined.

```twig
{{ value|default(fallback = '') }}
```

### `e`

Alias of `escape`.

```twig
{{ value|e(strategy = 'html') }}
```

### `escape`

Escapes a value - for `html`, `html_attr`, `js`, `css` or `url`. Output is escaped as HTML automatically.

```twig
{{ value|escape(strategy = 'html') }}
```

### `filter`

Keeps the items for which the arrow function is true.

```twig
{{ list|filter(v => condition) }}
```

### `first`

The first item of a list or the first character of a string.

```twig
{{ list|first }}
```

### `format`

sprintf-style formatting.

```twig
{{ '%s of %d'|format(a, b) }}
```

### `join`

Joins a list into a string.

```twig
{{ list|join(glue = '', and = null) }}
```

### `json_encode`

JSON for a value - pass data to a script with `|json_encode|e('html_attr')` in a data attribute.

```twig
{{ value|json_encode }}
```

### `keys`

Keys of a hash.

```twig
{{ hash|keys }}
```

### `last`

The last item.

```twig
{{ list|last }}
```

### `length`

Number of items or characters.

```twig
{{ list|length }}
```

### `lower`

Lowercase.

```twig
{{ text|lower }}
```

### `map`

Transforms every item with an arrow function.

```twig
{{ list|map(v => expression) }}
```

### `merge`

Merges lists or hashes.

```twig
{{ hash|merge(other) }}
```

### `nl2br`

Line breaks to `<br>` - for plain text fields shown on several lines.

```twig
{{ text|nl2br }}
```

### `number_format`

Formats a number. Use `money` for prices.

```twig
{{ number|number_format(decimals, point, thousands) }}
```

### `raw`

Prints without escaping. Only for HTML the platform already sanitised: descriptions, page content, rich text settings.

```twig
{{ html|raw }}
```

### `reduce`

Folds a list into one value.

```twig
{{ list|reduce((carry, v) => expression, initial) }}
```

### `replace`

Replaces substrings.

```twig
{{ text|replace({ from: to })) }}
```

### `reverse`

Reverses a list or a string.

```twig
{{ list|reverse }}
```

### `round`

Rounds a number.

```twig
{{ number|round(precision = 0, method = 'common') }}
```

### `slice`

A part of a list or a string.

```twig
{{ list|slice(start, length) }}
```

### `sort`

Sorts a list.

```twig
{{ list|sort((a, b) => a <=> b) }}
```

### `split`

Splits a string into a list.

```twig
{{ text|split(delimiter, limit) }}
```

### `striptags`

Twig's own tag stripping - same purpose as `strip_html`.

```twig
{{ html|striptags }}
```

### `title`

Every word capitalised.

```twig
{{ text|title }}
```

### `trim`

Removes whitespace from both ends.

```twig
{{ text|trim }}
```

### `upper`

Uppercase.

```twig
{{ text|upper }}
```

### `url_encode`

Encodes a value for a URL, a hash becomes a query string.

```twig
{{ value|url_encode }}
```

