SovLabs Template Engine

Tags

Creates the logic and control flow for templates and does not produce any visible text.

Assign variables and create conditions and loops without showing any of the logic on the page.

Denoted by curly braces and percent signs: {% and %}

Example
  • user.name = 'John Smith'
  • Input
    {% if user %}Hello {{ user.name }}{% endif %}
  • Output
    Hello John Smith!

assign

Assigns some value to a variable

Example
  • {% assign foo = 'Hello world' %}
  • Input
    {{ foo }}
  • Output
    Hello world

capture

Block tag that captures text into a variable

Example
  • {% capture foo %}Hello world{% endcapture %}
  • Input
    {{ foo }}
  • Output
    Hello world

case

Creates a switch statement to compare a variable with different values.

case initializes the switch statement, and when compares its values.

Example
  • {% assign foo = 'banana' %}
  • Input
    {% case foo %}{% when 'apple' %}Susy had an apple{% when 'banana' %}Susy had a banana{% else %}Susy did not have an apple or a banana{% endcase %}
  • Output
    Susy had a banana

comment

Any content that you put between {% comment %} and {% endcomment %} tags is turned into a comment.

Example
  • Input
    We made 1 million dollars {% comment %}in loses {% endcomment %} this year
  • Output
    We made 1 million dollars this year

cycle

Loops through a group of strings and outputs them in the order that they were passed as parameters.
Each time cycle is called, the next string that was passed as a parameter is output.

cycle must be used within a for loop block.

Example
  • Input
    {% cycle 'one', 'two', 'three' %}
    {% cycle 'one', 'two', 'three' %}
    {% cycle 'one', 'two', 'three' %}
    {% cycle 'one', 'two', 'three' %}
  • Output
    one
    two
    three
    one

Group name

If no name is supplied for the cycle group, then it's assumed that multiple calls with the same parameters are one group.

To have total control over cycle groups, optionally specify the name of the group. This can even be a variable.

Example
  • Input
    {% cycle 'group 1': 'one', 'two', 'three' %}
    {% cycle 'group 1': 'one', 'two', 'three' %}
    {% cycle 'group 2': 'one', 'two', 'three' %}
    {% cycle 'group 2': 'one', 'two', 'three' %}
  • Output
    one
    two
    one
    two

if/else

Executes a block of code only if a certain condition is true. elsif and else add more conditions within an if block.

Example
  • user.name = 'John Doe'
  • Input
    {% if user.name == 'John Smith' %}Hello John Smith{% elsif user.name == 'Jane Smith' %}Hello Jane Smith{% else %}Hello stranger{% endif %}
  • Output
    Hello stranger

and logical operator

In an if statement, use the and logical operator to return only if all operands return true

Example
  • user.name = 'John Smith'
    user.birthday = '01/01/80'
  • Input
    {% if user.name == 'John Smith' and user.birthday == '01/01/80' %}Hello John Smith{% else %}ERROR: Wrong John Smith{% endif %}
  • Output
    Hello John Smith

or logical operator

In an if statement, use the or logical operator to return if one of the operands return true

Example
  • user.name = 'John Smith'
    user.birthday = '01/01/80'
  • Input
    {% if user.name == 'John Smith' or user.birthday == '12/12/80' %}Hello John Smith or person born on 12/12/80{% else %}ERROR: Wrong John Smith or not born in 12/12/80{% endif %}
  • Output
    Hello John Smith or person born on 12/12/80

unless

The opposite of if and executes a block of code only if a certain condition is not met.

Example
  • user.name = 'John Doe'
  • Input
    {% unless user.name == 'John Smith' %}Hello Jane Smith{% endunless %}
  • Output
    Hello Jane Smith

include

Includes another template; useful for partials


for

Loop over collections. for loops can iterate over arrays, hashes, and ranges of integers.

Example
  • foo_array = ['apple', 'banana']
  • Input
    {% for item in foo_array %}{{ item }}{% endfor %}
  • Output
    apple
    banana

Iterating a hash

When iterating a hash, item[0] contains the key, and item[1] contains the value

Example
  • Input
    {% for item in hash %}{{ item[0] }}:{{ item[1] }}{% endfor %}
  • Output
    key: value

.length

The following helper variable is available for extra styling needs

Example
  • Input
    {{ foo_array.length }}
  • Output
    Length of the entire for loop

.index

The following helper variable is available for extra styling needs

Example
  • Input
    {{ foo_array.index }}
  • Output
    Index of the current iteration

.index0

The following helper variable is available for extra styling needs

Example
  • Input
    {{ foo_array.index0 }}
  • Output
    Index of the current iteration (zero based)

.rindex

The following helper variable is available for extra styling needs

Example
  • Input
    {{ foo_array.rindex }}
  • Output
    How many items are still left?

.rindex0

The following helper variable is available for extra styling needs

Example
  • Input
    {{ foo_array.rindex0 }}
  • Output
    How many items are still left? (zero based)

.first

The following helper variable is available for extra styling needs

Example
  • Input
    {{ foo_array.first }}
  • Output
    Is this the first iteration?

.last

The following helper variable is available for extra styling needs

Example
  • Input
    {{ foo_array.last }}
  • Output
    Is this the last iteration?

limit:#

Optional argument to the for tag. Restrict how many items are received.

Example
  • foo_array = [1, 2, 3, 4, 5, 6]
  • Input
    {% for item in foo_array limit:2 %}{{ item }}{% endfor %}
  • Output
    1, 2

offset:#

Optional argument to the for tag. Start the collection with the n item.

Example
  • foo_array = [1, 2, 3, 4, 5, 6]
  • Input
    {% for item in foo_array offset:2 %}{{ item }}{% endfor %}
  • Output
    3, 4, 5, 6

reversed

Optional argument to the for tag. Start the collection with the n item.

Example
  • foo_array = [1, 2, 3, 4, 5, 6]
  • Input
    {% for item in foo_array reversed %}{{ item }}{% endfor %}
  • Output
    6, 5, 4, 3, 2, 1

range

Optional argument to the for tag. Define a range of numbers to loop through - can be defined by both literal and variable numbers.

Example
  • Input
    {% for i in (1..4) %}{{ i }}{% endfor %}
  • Output
    1, 2, 3, 4

else

Optional argument to the for tag. Display a block of text when there are no items in the collection

Example
  • foo_array = []
  • Input
    {% for item in foo_array %}{{ item.title }}{% else %}There are no items!{% endfor %}
  • Output
    There are no items!

break

Exit the loop

Example
  • foo_array = [1, 2, 3, 4, 5, 6]
  • Input
    {% for item in foo_array %}{% if item == 4 %}{% break %}{% else %}{{ item }}{% endif %}{% endfor %}
  • Output
    1, 2, 3

continue

Skips the remaining code in the current for loop and continues with the next.

Example
  • foo_array = [1, 2, 3, 4, 5, 6]
  • Input
    {% for item in foo_array %}{% if item == 4 %}{% continue %}{% else %}{{ item }}{% endif %}{% endfor %}
  • Output
    1, 2, 3, 5, 6

raw

Temporarily disables tag processing and is useful for generating content (e.g Mustache, Handlebars) which uses conflicting syntax.

Example
  • Input
    {% raw %}In Handlebars, {{ this }} will be HTML-escaped, but {{{ that }}} will not.{% endraw %}

Go to SovLabs Template Engine Filters