---
updatedAt: 2026-10-14T07:30:17.000Z
agentTools:
  projectIndex: https://docs.clerk.io/llms.txt
---

# Template Language

How to style the results from Clerk.io.

Clerk.js's template language uses the full [Liquid](https://shopify.github.io/liquid/) template language. This page shows common use-cases for creating Clerk designs with the language.&#x20;

Here is a simple example of a template:

```liquid Template Example
<h1>{{ headline }}</h1>
  
<ul class="product-list">
  {% for product in products %}
    <li class="product">
      <h2 class="product-name">{{ product.name }}</h2>
 
      <img src="{{ product.image }}" title="{{ product.name }}" />
 
      {% if product.on_sale  %}
        <div class="list-price">${{ product.list_price | money }}</div>
        <div class="price">${{ product.price | money }}</div>
        <div class="discount">You Save: {{ product.price / product.list_price * 100 | round }}%</div>
      {% else %}
      	<div class="price">${{ product.price | money }}</div>
      {% endif %}
        
      <a href="{{ product.url }}">Buy Now</a>
    </li>
  {% endfor %}
</ul>
```

## Inserting Values

Everything within `{{` and `}}` is processed by the template engine as a variable to be inserted into the page eg. `{{ headline }}`.

If a variable contains a deeper structure, like a dictionary, it can be accessed by dot syntax like so<br />`{{ product.price }}`.

A value can also be a expression such as `{{ 100 - product.price / product.list_price * 100 }}`.

```liquid Variable Examples
<h1>{{ headline }}</h1>

<div class="price">{{ product.price }}</div>

<div class="list-price">{{ 100 - product.price / product.list_price * 100 }}</div>
```

## If-else statements

Flow control if supported via if-else statements denoted by curly braces and percent signs: `{%` and `%}`.

If-statements can be used to check simple booleans, numerical values and strings.

Some examples:

```jinja2 Simple boolean if-statement
{% if product.on_sale %}
  On Sale
{% endif %}
```

```jinja2 Numerical value if-statement
{% if product.stock_count < 10 %}
  Only few in stock!
{% else %}
  Lots in stock.
{% endif %}
```

```jinja2 String if-statement
{% if product.vendor == "Awesome Shoes" %}
  These shoes are awesome!
{% endif %}
```

```jinja2 String contains if-statement
{% if product.title contains 'Pack' %}
  This product's title contains the word Pack.
{% endif %}
```

Statements can also be combined for more advanced logics:

```jinja2 Combined if-statements
{% if product.type == "Shirt" or product.type == "Shoes" %}
  This is a shirt or a pair of shoes.
{% endif %}
```

### Nested if-else statements

If-statements can be nested inside other if-statements when you need to check multiple conditions in sequence.

```jinja2 Nested if-statements
{% if product.in_stock %}
  {% if product.on_sale %}
    This product is in stock and on sale.
  {% else %}
    This product is in stock at the regular price.
  {% endif %}
{% else %}
  This product is currently out of stock.
{% endif %}
```

The available operators in [Liquid](https://shopify.github.io/liquid/basics/operators/) are these:

| Operator | Description                                 |
| :------- | :------------------------------------------ |
| ==       | equals                                      |
| !=       | does not equal                              |
| >        | greater than                                |
| <        | less than                                   |
| > =      | greater than or equal to                    |
| <=       | less than or equal to                       |
| or       | Show if if x OR y is valid                  |
| and      | Show if x AND y is valid                    |
| contains | Show if attribute contains a specific value |

## For-loops

Iterating over an array of values is done as follows:

```jinja2 for-loop
{% for product in products %}
  {{ product.name }}
{% endfor %}
```

To make looping over a loop easier each loop has an associated `loop` object only available within the loop body with the following fields:

| Name          | Value                                                                                   |
| :------------ | :-------------------------------------------------------------------------------------- |
| `loop.index`  | The index of the loop iteration starting from 0.                                        |
| `loop.length` | The total length of the loop ie. number of elements in the list there is iterated over. |
| `loop.first`  | `true` if this is the first iteration of the loop else `false`.                         |
| `loop.last`   | `true` if this is the last iteration of the loop else `false`.                          |

Here is an example of using the `loop` object to write a comma-separated list of sizes for each product.

```liquid Using the loop-object
{% for size in product.sizes %}
 {{ size }}{% if not loop.last %}, {% endif %}
{% endfor %}
```
```text Output
Small, Medium, Large, X-Large
```

## Assigning Variables

You can assign variables in the template language `assign` keyword like this `{% assign myvar = 123 + 456 %}`.

Assigned variables will be available in the current scope (and all sub.-scopes) after the assign statement.

```liquid
{% for product in products %}
  {% assign is_on_sale = product.price < product.list_price %}
  <h1>{{ product.name }}</h1>
  {% if is_on_sale %}
    <div class="on-sale-badge">On Sale</div>
  {% endif %}
{% endfor %}
```

## Comments

Besides HTML comments (which will be inserted into the generated HTML) you can also use comments build into the template language that will only be visible in the template.

Comments are started with `{% comment %}` and ended with `{% endcomment %}`.

## Formatters

Formatters can be used to format values before they are inserted into the page. Formatters are applied to a value by using `|`.

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

Formatters can even be used with **several arguments**, if needed:

```liquid
{{ product.price | cost product.list_price }}
```

Multiple formatters can be used by being chained after each other. They will be applied in the order they are listed reading from left to right.

```liquid
{{ product.price | round | money }}
```

In addition to everything included in the full Liquid library, Clerk.js comes with extra built-in custom formatters:

<Table align={["left","left"]}>
  <thead>
    <tr>
      <th>
        Formatter
      </th>

      <th>
        Function
      </th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>
        `round`
      </td>

      <td>
        Rounds to the nearest integer, like so `{{ x | round }}`.
      </td>
    </tr>

    <tr>
      <td>
        `pct`
      </td>

      <td>
        Gives you the difference in percentage between two numbers. Usage:  `{{ x | pct y }}`.
      </td>
    </tr>

    <tr>
      <td>
        `money`
      </td>

      <td>
        Formats a decimal number as currency using the US formatting rules.

        - Optional\_: The amount of decimals to be displayed can be altered by adding a number to the function.<br />Usage: `{{ price | money 2 }}`\\

        Can also be configured for custom price-formatting with this syntax:<br />`{{price | money 2 "," "." }}`
      </td>
    </tr>

    <tr>
      <td>
        `money_eu`
      </td>

      <td>
        Formats a decimal number as currency using the EU formatting rules.

        - Optional\_: The amount of decimals to be displayed can be altered by adding a number to the function.<br />Usage: `{{ price | money_eu 2 }}`\\

        Can also be configured for custom price-formatting with this syntax:<br />`{{price | money_eu 2 "," "."}}`
      </td>
    </tr>

    <tr>
      <td>
        `replace`
      </td>

      <td>
        Replaces a string with another eg `{{ product.image | replace 'http://' 'https://' }}`.
      </td>
    </tr>

    <tr>
      <td>
        `highlight`
      </td>

      <td>
        Highlights part of a text by wrapping it in a span with a specified class. If no class is given then `clerk-highlight` will be used as the default class.<br />`{{ name | highlight query 'my-query-highlight-class' }}`
      </td>
    </tr>

    <tr>
      <td>
        `first`
      </td>

      <td>
        Returns the first element of a list `{{ xs | first }}`
      </td>
    </tr>

    <tr>
      <td>
        `last`
      </td>

      <td>
        Returns the last element of a list `{{ xs | last }}`
      </td>
    </tr>

    <tr>
      <td>
        `nth`
      </td>

      <td>
        Returns a value from a list `xs`, based on the index given `n`, like so `{{ xs | nth n }}`
      </td>
    </tr>

    <tr>
      <td>
        `min`
      </td>

      <td>
        Returns the lowest value from a list
      </td>
    </tr>

    <tr>
      <td>
        `max`
      </td>

      <td>
        Returns the highest value from a list
      </td>
    </tr>

    <tr>
      <td>
        `truncate`
      </td>

      <td>
        Returns a specified amount of characters for a string followed by a custom text.<br />E.g. `{{ product.name | truncate: 30 "..."}}` returns the first 30 characters followed by "..."
      </td>
    </tr>
  </tbody>
</Table>

You can add you own custom formatter functions in two ways:

* In my.clerk.io under **Settings > Formatters**
* As a [Clerk.js Configuration](https://docs.clerk.io/docs/clerkjs-configuration).

A formatter function takes one or more arguments and returns a string or number. Here is an example of our `round` formatter:

```javascript Example fo round formatter
Clerk('config', {
  formatters: {
    round: function(x) {
      return Math.round(x).toString();
    },
    cost: function(price, price2) {
      if (price > price2) {
        return "This is a premium product on sale";
      }
      else {
        return "This is is a regular product";
      }
    }
});
```

## Content Variables

When rendering the main template of a Content the following variables are always available:

| Variable     | Data                                                                                                                  |
| :----------- | :-------------------------------------------------------------------------------------------------------------------- |
| `products`   | The resulting products to be rendered in the content including all requested metadata.                                |
| `response`   | The full response object from the content API call.                                                                   |
| `content`    | Any values associated with the specific Content such as parameters or other data added later.                         |
| `formatters` | All formatters available for the content. To add custom formatters see [Configuration](https://docs.clerk.io/docs/clerkjs-configuration).    |
| `globals`    | All global variables avaliable for the Content. To add custom globals see [Configuration](https://docs.clerk.io/docs/clerkjs-configuration). |

To simplify the template language all the sub-keys of `response`, `content`, `formatters` and `globals` are merged into the root scope such that `{{ globals.is_mobile }}` can be accessed directly as `{{ is_mobile }}` or `{{ response.count | formatters.round }}` can be access directly as `{{ count | round }}`.

Thus  `response`, `content`, `formatters` and `globals` can be considered namespaces that are only explicitly needed in terms of a name clash.

## Global Variables

Any template will always have the globals configured under [Configuration](https://docs.clerk.io/docs/clerkjs-configuration) available as the value `globals`.

Clerk.js comes with these built-in Globals:

| Formatter    | Function                                                                               |
| :----------- | :------------------------------------------------------------------------------------- |
| `is_desktop` | Evaluates to `true` if the user browses using a screen size of **1024px or more**      |
| `is_tablet`  | Evaluates to `true` if the user browses using a screen size of  between 768px - 1024px |
| `is_mobile`  | Evaluates to `true` if the user browses using a screen size of 768px or less           |

## UI Component Variables

Each [UI Component](https://docs.clerk.io/docs/clerkjs-ui) may add more variables to the root scope. If so this will be documented under the specific UI component.

## JavaScript Methods

Some specific JavaScript methods are available directly in your Designs.

The following are currently supported:

<Table align={["left","left"]}>
  <thead>
    <tr>
      <th>
        Method
      </th>

      <th>
        Function
      </th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>
        `.split()`
      </td>

      <td>
        Splits a string into an array based on the string given as an argument.

        Usage: `{{ product.name.split("-") }}`

        A specific index from the list can be returned by adding the index to the line:<br />`{{ product.name.split("-")[0] }}`
      </td>
    </tr>

    <tr>
      <td>
        `.length`
      </td>

      <td>
        Checks the amount of values in a list or characters in a string.

        Usage: `{% if result.length > 10 %} CODE_HERE {% endif %}`
      </td>
    </tr>
  </tbody>
</Table>