Files
liquidjs/README.md
T
2016-06-20 01:10:43 +08:00

12 KiB

shopify-liquid

NPM version Build Status Coverage Status Dependency manager

Liquid implementation for Node.js. All tags and filters listed in Shopify Documentation shall be implemented.

Shopify liquid is used by Jekyll and Github Pages.

Usage

Install:

npm install --save shopify-liquid

Parse and Render:

var Liquid = require('shopify-liquid');
var engine = Liquid();

engine.parseAndRender('{{name | capitalize}}', {name: 'alice'});  // Alice

Caching templates:

var tpl = engine.parse('{{name | capitalize}}');
engine.render(tpl, {name: 'alice'}); // Alice

Register Filters:

// Usage: {{ name | uppper }}
engine.registerFilter('upper', function(v){
  return v.toUpperCase();
});

See existing filter implementations: https://github.com/harttle/shopify-liquid/blob/master/filters.js

Register Tags:

// Usage: {% upper name%}
engine.registerTag('upper', {
    parse: function(tagToken, remainTokens) {
        this.str = tagToken.args; // name
    },
    render: function(scope, hash) {
        var str = Liquid.evalValue(this.str, scope); // 'alice'
        return str.toUpperCase(); // 'Alice'
    }
});

See existing tag implementations: https://github.com/harttle/shopify-liquid/blob/master/tags/

Operators

Documentation: https://shopify.github.io/liquid/basics/operators/

==, !=, >, <, >=, <=, or, and, contains.

Tags

Documentation: https://shopify.github.io/liquid/basics/introduction/#tags

Filters

Documentation: https://shopify.github.io/liquid/basics/introduction/#filters

Differences from Shopify Liquid

  • url_encode is based on encodeURIComponent, will be converted to %20.

Async Support

harttle/shopify-liquid do NOT support async rendering, this is by design.

The primary principle of harttle/shopify-liquid is EASY TO EXTEND. Async rendering introduces extra complexity in both implementation and extension.

For template-driven projects, checkout these Liquid-like engines: