-
Notifications
You must be signed in to change notification settings - Fork 10
NodeJs Usage
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.
If you haven't yet, install the toffee
module:
> npm install -g toffee
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.
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 = 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
.
You can access the default engine directly with toffee.render
toffee = require 'toffee'
toffee.render 'foo.toffee', {title: "bar"}, (err, res) -> console.log res