Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

shopify-liquid

Package Overview
Dependencies
Maintainers
1
Versions
50
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

shopify-liquid

Liquid template engine in Node.js (Shopify compliant)

  • 1.2.3
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
20
increased by81.82%
Maintainers
1
Weekly downloads
 
Created
Source

shopify-liquid

NPM version Build Status Coverage Status Dependency manager

A feature-rich Liquid implementation for Node.js, with compliance with Jekyll and Github Pages.

Installation:

npm install --save shopify-liquid

Render from String

Parse and Render:

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

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

Caching templates:

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

Render from File

var engine = Liquid({
    root: path.resolve(__dirname, 'views/'),  // for layouts and partials
    extname: '.liquid',
    cache: false
});
engine.renderFile("hello.liquid", {name: 'alice'})
    .then(function(html){
       // html === 'Alice'
    });
// equivalent to: 
engine.renderFile("hello", {name: 'alice'})
    .then(function(html){
       // html === 'Alice'
    });

cache default to false, extname default to .liquid, root default to "".

Strict Rendering

Undefined filters and variables will be rendered as empty string by default. Enable strict rendering to throw errors upon undefined variables/filters:

var opts = {
    strict_variables: true, 
    strict_filters: true
};
engine.parseAndRender("{{ foo }}", {}, opts).catch(function(err){
    // err.message === undefined variable: foo
});
engine.parseAndRender("{{ 'foo' | filter1 }}", {}, opts).catch(function(err){
    // err.message === undefined filter: filter1
});
// Note: the below opts also work:
// engine.render(tpl, ctx, opts)
// engine.renderFile(path, ctx, opts)

Use with Express.js

// register liquid engine
app.engine('liquid', engine.express({
    strict_variables: true,         // Default: fasle
    strict_filters: true            // Default: false
})); 
app.set('views', './views');            // specify the views directory
app.set('view engine', 'liquid');       // set to default

There's an Express demo here.

Use in Browser

Download the dist files and import into your HTML. And window.Liquid is what you want.

<html lang="en">
<head>
  <script src="dist/liquid.min.js"></script>
</head>
<body>
  <script>
    var engine = window.Liquid();
    var src = '{{ name | capitalize}}';
    var ctx = {
      name: 'welcome to Shopify Liquid'
    };
    engine.parseAndRender(src, ctx)
      .then(function(html) {
        // html === Welcome to Shopify Liquid 
      });
  </script>
</body>
</html>

There's also a demo.

Includes

// file: color.liquid
color: '{{ color }}' shape: '{{ shape }}'

// file: theme.liquid
{% assign shape = 'circle' %}
{% include 'color' %}
{% include 'color' with 'red' %}
{% include 'color', color: 'yellow', shape: 'square' %}

The output will be:

color: '' shape: 'circle'
color: 'red' shape: 'circle'
color: 'yellow' shape: 'square'

Layouts

// file: default-layout.liquid
Header
{% block content %}My default content{% endblock %}
Footer

// file: page.liquid
{% layout "default-layout" %}
{% block content %}My page content{% endblock %}

The output of page.liquid:

Header
My page content
Footer
  • It's possible to define multiple blocks.
  • block name is optional when there's only one block.

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 Promise.resolve(str.toUpperCase()); // 'Alice'
    }
});

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

All Tags

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

TagDocumentSourceTest
case/whenDocumentSourceTest
ifDocumentSourceTest
unlessDocumentSourceTest
elsif/elseDocumentSourceTest
forDocumentSourceTest
breakDocumentSourceTest
continueDocumentSourceTest
for: limit,offset,range,reversedDocumentSourceTest
cycleDocumentSourceTest
cycle: groupDocumentSourceTest
tablerowDocumentSourceTest
tablerow: cols,limit,offset,rangeDocumentSourceTest
assignDocumentSourceTest
captureDocumentSourceTest
incrementDocumentSourceTest
decrementDocumentSourceTest
rawDocumentSourceTest
commentDocumentSourceTest
includeDocumentSourceTest
layout, blockDocumentSourceTest

All Filters

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

FilterDocumentSourceTest
absDocumentSourceTest
appendDocumentSourceTest
capitalizeDocumentSourceTest
ceilDocumentSourceTest
dateDocumentSourceTest
defaultDocumentSourceTest
divided_byDocumentSourceTest
downcaseDocumentSourceTest
escapeDocumentSourceTest
escape_onceDocumentSourceTest
firstDocumentSourceTest
floorDocumentSourceTest
joinDocumentSourceTest
lastDocumentSourceTest
lstripDocumentSourceTest
mapDocumentSourceTest
minusDocumentSourceTest
moduloDocumentSourceTest
newline_to_brDocumentSourceTest
plusDocumentSourceTest
prependDocumentSourceTest
removeDocumentSourceTest
remove_firstDocumentSourceTest
replaceDocumentSourceTest
replace_firstDocumentSourceTest
reverseDocumentSourceTest
roundDocumentSourceTest
rstripDocumentSourceTest
sizeDocumentSourceTest
sliceDocumentSourceTest
sortDocumentSourceTest
splitDocumentSourceTest
stripDocumentSourceTest
strip_htmlDocumentSourceTest
strip_newlinesDocumentSourceTest
timesDocumentSourceTest
truncateDocumentSourceTest
truncatewordsDocumentSourceTest
uniqDocumentSourceTest
upcaseDocumentSourceTest
url_encodeDocumentSourceTest

Operators

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

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

Keywords

FAQs

Package last updated on 05 Oct 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc