npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nakedcreativity-liquid

v1.2.6

Published

Liquid template engine for JavaScript, Node.js and Browser

Downloads

7

Readme

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

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/

Contribution Guide

  1. Make a fork.
  2. Write a test to demonstrate your feature is not supported yet.
  3. Optionaly, change source files to make all test pass.
  4. Create a pull request.