Skip to content

tonyheupel/liquid-node

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

52 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

LiquidNode - The Liquid template engine on Node.js

LiquidNode is a port of the Liquid template engine (originally written in Ruby) to Node.js. It uses Promises to support non-blocking, asynchronous variables/filters/blocks. Most code has been translated from Ruby to CoffeeScript, with a few adjustments (casing) to make it feel more Coffee-/JavaScripty.

How LiquidNode differs from Liquid (or: Why this fork?)

The power of Node.js lies in its non-blocking nature. This presents a problem when using sequential/synchronous Liquid-expressions like {{ store.items | count }} which hide one or multiple blocking SQL-queries.

LiquidNode solves that problem by using Futures and Promises. The programmer just return Promises from asynchronous functions - the designer doesn't have to care about it.

Introduction to the Liquid template engine

Liquid is a template engine which was written with very specific requirements:

  • It has to have beautiful and simple markup. Template engines which don't produce good looking markup are no fun to use.
  • It needs to be non evaling and secure. Liquid templates are made so that users can edit them. You don't want to run code on your server which your users wrote.
  • It has to be stateless. Compile and render steps have to be seperate so that the expensive parsing and compiling can be done once and later on you can just render it passing in a hash with local variables and objects.

Why you should use Liquid

  • You want to allow your users to edit the appearance of your application but don't want them to run insecure code on your server.
  • You want to render templates directly from the database
  • You like smarty (PHP) style template engines
  • You need a template engine which does HTML just as well as emails
  • You don't like the markup of your current templating engine

What does it look like?

<ul id="products">
  {% for product in products %}
    <li>
      <h2>{{ product.name }}</h2>
      Only {{ product.price | price }}

      {{ product.description | prettyprint | paragraph }}
    </li>
  {% endfor %}
</ul>

Howto use Liquid

Liquid supports a very simple API based around the Liquid.Template class. For standard use you can just pass it the content of a file and call render with an object.

Liquid = require "liquid-node"

template = Liquid.Template.parse("hi {{name}}") # Parses and compiles the template
promise = template.render 'name': 'tobi'        # => [Promise Object]
promise.done console.log                        # >> "hi tobi"

Implementation of Promises (LiquidNode specific)

LiquidNode uses a custom Promise implementation. It's loosely based on jQuery's Deferred, Promises/A, and Q. Its API is designed to prevent callback-pyramids (spaghetti-code 2.0).

fs        = require "fs"
{async}   = require "liquid-node"

class Server
  name: ->
    "Falkor"

  # A deferred can either be resolved (no error) or rejected (error).
  think: ->
    async.promise (deferred) ->
      later = -> deferred.resolve(42)
      setTimeout(later, 1000)

  # This is an example of how to wait for a Promise:
  patientMethod: ->
    deepThought = @think()

    deepThought
      .done (answer) -> console.log "The answer is: %s.", answer
      .fail (e) -> console.log "Universe reset: %s.", e
      .always (e) -> console.log "Look on the bright side of life."

    # By the way: the left-hand side of async.promise returns a
    # read-only view (Promise) to the Deferred. This means an
    # Illuminati can't interfere with it on this side of the
    # Promise.
    #
    # deepThought.resolve(23) isn't available.

  # For node-ish callbacks you can use `deferred.node`. This
  # will automatically resolve/reject based on the first argument.
  accounts: ->
    async.promise (deferred) ->
      fs.readFile "/etc/passwd", "utf-8", deferred.node

  # If you don't want to check, whether an object is a Promise or not
  # just use `async.when`. It will build a Promise around it if necessary.
  unsure: (promiseWannabe) ->
    async.when(promiseWannabe)

  # You can chain Promises using `promise.when().when().when()...`.
  # A `when` will be called with the resolution of the previous `when`.
  recipe: ->
    gotoStore()
      .when (store) -> store.getEggs()
      .when (eggs) ->
        # or nest the when-calls
        eggs.split().when (yolk) -> bakeWith(yolk)
      .done (pancake) ->
        console.log "Pancake is ready!"

State of development

I'm developing this project alongside a different project. I translated a few basic tests from the original Liquid codebase - but there are hundreds of them. So if you find a bug-fix or have some time to translate further tests I'll be happy to pull them in.

License

LiquidNode is released under the MIT license.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published