# Additional filters

Additional filters serve many purposes, including formatting, conversion and applying CSS classes.

Additional filters serve many purposes, including formatting, conversion and applying CSS classes.

## date

Converts a timestamp into another date format.

```liquid
{{ article.published_at | date: "%a, %b %d, %y" }}
```

```text
Tue, Apr 22, 14
```

`date` accepts the same parameters as Ruby's `strftime` method. See the Ruby documentation or a site like [strfti.me](https://www.strfti.me/) for the available format codes.

## format

Pass a `format` parameter to the `date` filter to show the date in a format that matches the storefront language. VersaCommerce looks the formats up in the theme's locale files (`locales/`) under `date_formats`, for example `date_formats.abbreviated_date`. You can define your own formats there.

```liquid
{{ order.created_at | date: format: 'abbreviated_date' }}
```

```text
Dec 31, 2018
```

## default

Sets a default value for any variable that has no value. Works with strings, arrays and hashes.

The default value is returned if the variable resolves to `nil`, `false` or an empty string `""`. A string that contains only spaces does not resolve to the default value.

```liquid
Dear {{ customer.name | default: "customer" }}
```

```html
<!-- If customer.name is nil -->
Dear customer

<!-- If customer.name is "" -->
Dear customer

<!-- If customer.name is "   " -->
Dear
```

To return `false` instead of the default value, use the `allow_false` parameter.

```liquid
{{ settings.flag | default: true, allow_false: true }}
```

```html
 <!-- If settings.flag is false -->
false
```

## default_errors

Outputs default error messages for the [form.errors](https://developer.versacommerce.de/articles/liquid-objects-form#form-errors) variable. The messages depend on the strings returned by `form.errors`.

```liquid
{% if form.errors %}
  {{ form.errors | default_errors }}
{% endif %}
```

```html
<!-- if form.errors returned "email" -->
Please enter a valid email address.
```

## default_pagination

Creates a set of links for paginated results. Use it with the [paginate](https://developer.versacommerce.de/articles/liquid-objects-paginate) variable.

```liquid
{{ paginate | default_pagination }}
```

```html
<span class="page current">1</span>
<span class="page">/collections/all?page=2</span>
<span class="page">/collections/all?page=3</span>
<span class="deco">&hellip;</span>
<span class="page">/collections/all?page=17</span>
<span class="next">/collections/all?page=2</span>
```

The default pagination uses the labels `Next »` and `« Previous` for the links to the next and previous page. You can override them by passing new words to the `default_pagination` filter:

```liquid
{{ paginate | default_pagination: next: 'Older', previous: 'Newer' }}
```

```html
<span class="page current">1</span>
<span class="page">/collections/all?page=2</span>
<span class="page">/collections/all?page=3</span>
<span class="next">/collections/all?page=2</span>
```

## format_address

Use the `format_address` filter on an address to print its parts in the order of its locale. The filter only prints the parts of the address that are filled in. It works on the address page for customers with an account and on the address of your shop:

```liquid
{{ address | format_address }}
```

```html
<p>
  Jane Doe<br>
  Example Street 123<br>
  Koblenz<br>
  DE<br>
  30419<br>
  Germany
</p>
```

```liquid
{{ address | format_address }}
```

```html
<p>
  John Doe<br>
  Sample Road 40<br>
  Belgian Quarter<br>
  10225 Berlin<br>
  Germany
</p>
```

## highlight

Wraps words in search results in an HTML `<strong>` tag with the class `highlight` if they match the entered [search.terms](https://developer.versacommerce.de/articles/liquid-objects-search#search-terms).

```liquid
{{ item.content | highlight: search.terms }}
```

```html
<!-- If the search term was "Yellow" -->
<strong class="highlight">Yellow</strong> shirts are the best!
```

## highlight_active_tag

Wraps a tag link in a `<span>` with the class `active` if that tag is used to filter a collection.

```liquid
<!-- collection.tags = ["Cotton", "Crew Neck", "Jersey"] -->
{% for tag in collection.tags %}
  {{ tag | highlight_active_tag | link_to_tag: tag }}
{% endfor %}
```

```html
<a title="Show products matching tag Cotton" href="/collections/all/cotton"><span class="active">Cotton</span></a>
<a title="Show products matching tag Crew Neck" href="/collections/all/crew-neck">Crew Neck</a>
<a title="Show products matching tag Jersey" href="/collections/all/jersey">Jersey</a>
```

## json

Converts a string into JSON format.

```liquid
var content = {{ pages.page-handle.content | json }};
```

```json
var content = "\u003Cp\u003E\u003Cstrong\u003EYou made it! Congratulations on starting your own online store!\u003C/strong\u003E\u003C/p\u003E\n\u003Cp\u003EThis is your shop\u0026#8217;s \u003Cstrong\u003Efrontpage\u003C/strong\u003E, and it\u0026#8217;s the first thing your customers will see when they arrive.\u003C/p\u003E";
```

You do not need to put the Liquid output in quotes. The `json` filter adds them and escapes quotes in the output where needed.

The `json` filter can also make Liquid objects readable for JavaScript:

```liquid
var json_product = {{ collections.featured.products.first | json }};
var json_cart = {{ cart | json }};
```

## Product inventory and the JSON filter

Do not rely on the `json` output for stock levels. To access inventory information, use [variant.inventory_quantity](https://developer.versacommerce.de/articles/liquid-objects-variant#variant-inventoryquantity) and [variant.inventory_policy](https://developer.versacommerce.de/articles/liquid-objects-variant).

## t (translation)

Uses a translation key to look up the locale file of the active language and returns the matching translated string.

## Translation key

The following example outputs a string from a locale file in a Liquid template, section or snippet. Here the translation key outputs "Sold out" on a product form.

The translation key must be wrapped in single quotes:

```liquid
<span>{{ 'products.product.sold_out' | t }}</span>
```

## Locale files

Say your theme is available in English, French and Spanish. The following examples show the matching translation key for `'products.product.sold_out'` in each locale file:

```json
{
  "products": {
    "product": {
      "sold_out": "Sold out"
    }
  }
}
```

```json
{
  "products": {
    "product": {
      "sold_out": "Épuisé"
    }
  }
}
```

```json
{
  "products": {
    "product": {
      "sold_out": "Agotado"
    }
  }
}
```

## HTML

The returned HTML for the three locale files:

```html
<span>Sold out</span>
```

```html
<span>Épuisé</span>
```

```html
<span>Agotado</span>
```

## Section locales vs theme locales

The `t` filter can also refer to keys defined in the `locales` object of a section's `schema` tag. Content you set under `locales` in `schema` is only available to that section. This is useful for a self-contained section you want to reuse in different themes.

Content that is global to a theme belongs in the theme's `locales` directory. For example, add the phrase "See more" to `locales` once and use the translation in a blog post and on the product page.

Translations in the section schema that are not part of the `locales` object are used for the merchant-facing text in the theme editor. They do not use the `t` filter.

## weight_with_unit

Formats the weight of a product variant. The weight unit is set in the general settings.

```liquid
{{ product.variants.first.weight | weight_with_unit }}
```

```text
24.0 kg
```

You can override the unit by passing it to the filter. This is useful for variants that each have their own unit.

```liquid
{{ variant.weight | weight_with_unit: variant.weight_unit }}
```

```text
52.9 lb
```

## placeholder_svg_tag

Takes a placeholder name and outputs a placeholder SVG illustration. An optional argument adds a custom class attribute to the SVG tag.

```liquid
{{ 'collection-1' | placeholder_svg_tag }}
```

```html
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 525.5 525.5">
  ...omitted for brevity...
</svg>
```

## Custom class attribute

Pass a class parameter to add a custom class attribute to the SVG tag:

```liquid
{{ 'collection-1' | placeholder_svg_tag: 'custom' }}
```

```html
<svg class="custom" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 525.5 525.5">
  ...omitted for brevity...
</svg>
```

Available placeholder names:

| Placeholder name |
|---|
| `collection-1` |
| `collection-2` |
| `collection-3` |
| `collection-4` |
| `collection-5` |
| `collection-6` |
| `image` |
| `lifestyle-1` |
| `lifestyle-2` |
| `product-1` |
| `product-2` |
| `product-3` |
| `product-4` |
| `product-5` |
| `product-6` |
