Clerk.io Developer Documentation

Welcome to the Clerk.io Developer Documentation.

Here you find our developer documentation for our API and all our SKDs.

Get Started    

Template Language

How to style the results from Clerk.io.

Clerk.js's template language is a subset of the Liquid template language.

Here is a simple example of a template:

<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.price < product.list_price %}
        <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
{{ product.price }}.

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

<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:

{% if product.on_sale %}
  On Sale
{% endif %}
{% if product.stock_count < 10 %}
  Only few in stock!
{% else %}
  Lots in stock.
{% endif %}
{% if product.vendor == "Awesome Shoes" %}
  These shoes are awesome!
{% endif %}
{% if product.title contains 'Pack' %}
  This product's title contains the word Pack.
{% endif %}

Statements can also be combined for more advanced logics:

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

The available operators in Liquid 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:

{% 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.

{% for size in product.sizes %}
 {{ size }}{% if not loop.last %}, {% endif %}
{% endfor %}
Small, Medium, Large, X-Large

Formatters

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

{{ product.price | money }}

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

{{ 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.

{{ product.price | round | money }}

Clerk.js comes with these built-in formatters:

Formatter
Function

round

Rounds to the nearest integer, like so {{ x | round }}.

pct

Gives you the percentage of two numbers, like so {{ x | pct y }}.

money

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. Usage: {{ price | money 2 }}

money_eu

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. Usage: {{ price | money_eu 2 }}

nth

Returns a value from a list (x), based on the index given (y), like so {{ x | nth y }}

min

Returns the lowest value from a list

max

Returns the highest value from a list

You can add you own formatter functions in the Clerk.js Configuration.
A formatter function takes one or more arguments and returns a string or number. Here is an example of our 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.

globals

All global variables avaliable for the Content. To add custom globals see 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 available as the value globals.

UI Component Variables

Each UI Component may add more variables to the root scope. If so this will be documented under the specific UI component.

Template Language


How to style the results from Clerk.io.

Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.