Skip to content

Liquid Engine

binja supports Liquid template syntax, commonly used in Shopify themes and Jekyll static sites.

Installation

Liquid support is included with binja:

Terminal window
bun add binja

Basic Usage

import * as liquid from 'binja/engines/liquid'
const html = await liquid.render('Hello {{ name }}!', { name: 'World' })
// Output: Hello World!

Syntax

Variables

{{ name }}
{{ user.email }}
{{ items[0] }}

Filters

{{ name | upcase }}
{{ text | truncate: 20 }}
{{ price | money }}

Comments

{% comment %}
This is a comment
{% endcomment %}

Control Structures

if / elsif / else

{% if user %}
Hello {{ user.name }}!
{% elsif guest %}
Hello Guest!
{% else %}
Hello Visitor!
{% endif %}

unless

{% unless is_admin %}
<p>You don't have admin access.</p>
{% endunless %}

case / when

{% case status %}
{% when 'active' %}
<span class="badge-success">Active</span>
{% when 'pending' %}
<span class="badge-warning">Pending</span>
{% else %}
<span class="badge-secondary">Unknown</span>
{% endcase %}

for

{% for item in items %}
<li>{{ item }}</li>
{% endfor %}

With else (empty):

{% for item in items %}
<li>{{ item }}</li>
{% else %}
<li>No items found</li>
{% endfor %}

Loop Variables

VariableDescription
forloop.index1-based index
forloop.index00-based index
forloop.firstTrue if first
forloop.lastTrue if last
forloop.lengthTotal items
{% for item in items %}
<li class="{% if forloop.first %}first{% endif %}">
{{ forloop.index }}. {{ item }}
</li>
{% endfor %}

Loop Parameters

{% for item in items limit: 5 %}
{% for item in items offset: 2 %}
{% for item in items reversed %}

Variable Assignment

assign

{% assign greeting = "Hello" %}
{{ greeting }}

capture

{% capture full_name %}
{{ first_name }} {{ last_name }}
{% endcapture %}
{{ full_name }}

Raw Output

{% raw %}
{{ this will not be processed }}
{% endraw %}

Filters

Built-in Liquid Filters

binja supports standard Liquid filters plus all 84+ binja filters:

{{ "hello" | upcase }} {# HELLO #}
{{ "HELLO" | downcase }} {# hello #}
{{ "hello" | capitalize }} {# Hello #}
{{ "hello world" | truncate: 8 }} {# hello... #}
{{ items | size }} {# 5 #}
{{ items | first }}
{{ items | last }}
{{ items | join: ", " }}
{{ items | sort }}
{{ items | reverse }}

Comparison with Jinja2

FeatureLiquidJinja2
Variables{{ var }}{{ var }}
Filters{{ var | filter }}{{ var|filter }}
If{% if %}{% if %}
Loop{% for %}{% for %}
Assign{% assign %}{% set %}
Comments{% comment %}{# #}
Raw{% raw %}{% raw %}

Migration from LiquidJS

// Before: LiquidJS
import { Liquid } from 'liquidjs'
const engine = new Liquid()
const html = await engine.parseAndRender(source, context)
// After: binja
import * as liquid from 'binja/engines/liquid'
const html = await liquid.render(source, context)

Shopify Theme Development

binja’s Liquid engine is compatible with Shopify theme syntax:

{% for product in collection.products %}
<div class="product">
<h2>{{ product.title }}</h2>
<p>{{ product.price | money }}</p>
{% if product.available %}
<button>Add to Cart</button>
{% else %}
<span>Sold Out</span>
{% endif %}
</div>
{% endfor %}

Performance

Liquid templates in binja benefit from:

  • Same optimized runtime as Jinja2
  • Same 84+ built-in filters
  • Template caching
  • AOT compilation support