Liquid v1.0.0-alpha.1 API Reference

Modules

A module to assign context for Liquid.Variable

Sets variables in a template

  {% assign foo = 'monkey' %}

User can then use the variables later in the page.

  {{ foo }}

Builds the AST processing with Nimble, only liquid valid tags and variables. It uses Tokenizer to send to Nimble only tags and variables, without literals. Literals (any markup which is not liquid variable or tag) are slow to be processed by Nimble thus this module improve performance between 30% and 100% depending how much text is processed

Stores the result of a block into a variable without rendering it inplace.

  {% capture heading %}
    Monkeys!
  {% endcapture %}
  ...
  <h1>{{ heading }}</h1>

Capture is useful for saving content for use later in your template, such as in a sidebar or footer

Creates a switch statement to compare a variable against different values. case initializes the switch statement, and when compares its values. Input:

  {% assign handle = 'cake' %}
  {% case handle %}
  {% when 'cake' %}
    This is a cake
  {% when 'cookie' %}
    This is a cookie
  {% else %}
    This is not a cake nor a cookie
  {% endcase %}

Output:

  This is a cake

General purpose combinators used by almost every other combinator

String with an assigned and thus identified meaning such as

  • Punctuator
  • Number
  • String
  • Boolean
  • List
  • Object

Helper to create tags

Sets variables in a template.

  {% assign foo = 'monkey' %}

User can then use the variables later in the page.

  {{ foo }}

Stores the result of a block into a variable without rendering it in place.

  {% capture heading %}
    Monkeys!
  {% endcapture %}
  ...
  <h1>{{ heading }}</h1> <!-- then you can use the `heading` variable -->

Capture is useful for saving content for use later in your template, such as in a sidebar or footer

Creates a switch statement to compare a variable against different values. case initializes the switch statement, and when compares its values. Input:

  {% assign handle = 'cake' %}
  {% case handle %}
  {% when 'cake' %}
    This is a cake
  {% when 'cookie' %}
    This is a cookie
  {% else %}
    This is not a cake nor a cookie
  {% endcase %}

Output:

  This is a cake

Allows you to leave un-rendered code inside a Liquid template. Any text within the opening and closing comment blocks will not be output, and any Liquid code within will not be executed Input:

  Anything you put between {% comment %} and {% endcomment %} tags
  is turned into a comment.

Output:

  Anything you put between  tags
  is turned into a comment

Implementation of custom tag. "Tags" are tags that take any number of arguments, but do not contain a block of template code. To create a new tag, Use Liquid.Register module and register your tag with Liquid.Register.register/3. The register tag takes three arguments: the user-facing name of the tag, the module where code of parsing/rendering is located and the type that implements it (tag or block)

Implementation of cycle tag. Can be named or anonymous, rotates through pre-set values Cycle is usually used within a loop to alternate between values, like colors or DOM classes.

  {% for item in items %}
  <div class="{% cycle 'red', 'green', 'blue' %}"> {{ item }} </div>
  {% end %}
  <div class="red"> Item one </div>
  <div class="green"> Item two </div>
  <div class="blue"> Item three </div>
  <div class="red"> Item four </div>
  <div class="green"> Item five</div>

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

  {% cycle 'one', 'two', 'three' %}
  {% cycle 'one', 'two', 'three' %}
  {% cycle 'one', 'two', 'three' %}
  {% cycle 'one', 'two', 'three' %}

Output:

  one
  two
  three
  one

Creates a new number variable, and decreases its value by one every time it is called. The initial value is -1. Decrement is used in a place where one needs to insert a counter into a template, and needs the counter to survive across multiple instantiations of the template. NOTE: decrement is a pre-decrement, -i, while increment is post: i+. (To achieve the survival, the application must keep the context)

Verifies when block is closed and send the AST to end the block

"for" tag iterates over an array or collection. Several useful variables are available to you within the loop

Secondary tags used inside primary tags. We defined a tag as secondary when it needs a primary tag to work. For example, else tag is used by if, for and cycle but id doesn't work alone

Executes a block of code only if a certain condition is true. If this condition is false executes else block of code. Input:

  {% if product.title == 'Awesome Shoes' %}
    These shoes are awesome!
  {% else %}
    These shoes are ugly!
  {% endif %}

Output:

  These shoes are ugly!

The block contained within ifchanged will only be rendered to the output if the last call to ifchanged returned different output

Include enables the possibility to include and render other liquid templates. Templates can also be recursively included

Creates a new number variable, and increases its value by one every time it is called. The initial value is 0. Increment is used in a place where one needs to insert a counter into a template, and needs the counter to survive across multiple instantiations of the template. (To achieve the survival, the application must keep the context) if the variable does not exist, it is created with value 0. Input:

  Hello: {% increment variable %}

Output:

  Hello: 0
  Hello: 1
  Hello: 2

Temporarily disables tag processing. This is useful for generating content (eg, Mustache, Handlebars) which uses conflicting syntax. Input:

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

Output:

In Handlebars, {{ this }} will be HTML-escaped, but {{{ that }}} will not.

Iterates over an array or collection splitting it up to a table with pre-set columns number Several useful variables are available to you within the loop. Generates an HTML table. Must be wrapped in opening

and closing
HTML tags. Input:

  <table>
    {% tablerow product in collection.products %}
      {{ product.title }}
    {% endtablerow %}
  </table>

Output:

  <table>
  <tr class="row1">
    <td class="col1">
      Cool Shirt
    </td>
    <td class="col2">
      Alien Poster
    </td>
    <td class="col3">
      Batman Poster
    </td>
    <td class="col4">
      Bullseye Shirt
    </td>
    <td class="col5">
      Another Classic Vinyl
    </td>
    <td class="col6">
      Awesome Jeans
    </td>
  </tr>
  </table>

Implementation of cycle tag. Can be named or anonymous, rotates through pre-set values Cycle is usually used within a loop to alternate between values, like colors or DOM classes

Allows to set up the file system and read the template file from it

Applies a chain of filters passed from Liquid.Variable

Applies a chain of 'Additionals' filters passed from Liquid.Variable

Applies a chain of 'HTML' filters passed from Liquid.Variable

Applies a chain of 'List' filters passed from Liquid.Variable

Applies a chain of 'Math' filters passed from Liquid.Variable

Applies a chain of 'String' filters passed from Liquid.Variable

Like in Shopify's liquid: "For" iterates over an array or collection. Several useful variables are available to you within the loop

Conveniences for generating HTML

Translate NimbleParser AST to old AST

Transform a valid liquid markup in an AST to be executed by render

Supervisor for Liquid processes (currently empty)

tablerow tag iterates over an array or collection splitting it up to a table with pre-set columns number

Main Liquid module, all further render and parse processing passes through it

Prepares markup to be parsed. Tokenizer splits the code between starting literal and rest of markup. When called recursively, it allows to process only liquid part (tags and variables) and bypass the slower literal

General purpose functions used by multiple translators

Transform AST to String

Translate new AST to old AST for Assign tag

Translate new AST to old AST for the Break tag, this tag is only present inside For tag

Translate new AST to old AST for the Capture tag

Translate new AST to old AST for the Case tag

Translate new AST to old AST for the Comment tag

Translate new AST to old AST for the continue tag, this tag is only present inside the For body tag

Translates new AST to old AST for the Custom tag

Translate new AST to old AST for the Cycle tag

Translate new AST to old AST for the Decrement tag

Translate new AST to old AST for the For tag

Translate new AST to old AST for the If tag

Translate new AST to old AST for the Ifchanged tag

Translate new AST to old AST for the Include tag

Translate new AST to old AST for the Increment tag

Translate new AST to old AST for liquid variables

Translate new AST to old AST for Raw tag

Translate new AST to old AST for the Tablerow tag

A number of useful utils for liquid parser/filters

Module to create and lookup for Variables

Defines When implementations (sub-component of Case). Case creates a switch statement to compare a variable with different values. Case initializes the switch statement, and When compares its values

Exceptions

Error module to hold file system errors

Error module to hold wrong syntax states