Skip to content
malgorithms edited this page Oct 11, 2012 · 10 revisions

In any NodeJs program, you can use Toffee to publish any template and vars into a string.

Prerequisite

If you haven't yet, install the toffee module:

> npm install -g toffee

If you're using Express

Hold on! Just read Using Toffee in Express, which probably has what you need. Then come back here if you ever want direct access to publishing, independent of Express's calls to Toffee.

Usage

Creating an engine and using it.

toffee = require 'toffee'
engine = new toffee.engine()

engine.render 'some-file-1.toffee', { var1: "hello", var2: "world" }, (err, res) ->
   console.log res

engine.render 'some-file-2.toffee', {}, (err, res) -> console.log res

Important: The engine is responsible for managing preferences and caching your templates, so don't create a new engine every time you render.

Engine options:

engine = new toffee.engine {
   prettyPrintErrors: false # defaults to true 
   verbose: true            # defaults to false
}

When prettyPrintErrors is true (the default), template compiling and runtime errors are packaged up into pretty html and put into res instead of err.

Skipping the engine creation

You can access the default engine directly with toffee.render

toffee = require 'toffee'
toffee.render 'foo.toffee', {title: "bar"}, (err, res) -> console.log res

See Also

Clone this wiki locally