Skip to content
malgorithms edited this page Oct 25, 2012 · 16 revisions

You can pass or assign the following options to a Toffee engine:

Engine Options

  • prettyPrintErrors: bool (default = true): by default, if Toffee hits an error while rendering, it will pretty print a stack trace and overlay it on the resulting page. And it will fake a "good" result, calling back with null, res. Setting this to false calls back with (err) instead.
  • prettyLogHtml: bool (default = true): since prettyPrintErrors=true replies without an error, your software (say, Express), won't log errors to the console. You probably want to set this to false if you set prettyPrintHtml to false, too.
  • verbose: bool (default = false): Toffee will log some information on what it's doing during renders
  • additionalErrorHandler: function: If Toffee encounters a render error, it will call your function with three parameters: (plaintext stack trace, html stack trace, and filename (if available). We use that setting instead of prettyLogHtml to customize how its logged. You could use have this email your staff when in production, for example. Whatever.
  • minimize: bool (default = false): Toffee will minimize the JavaScript using uglify-js inside the renderer. This typically isn't something you want to do, as it's more computationally intensive to compile a template with no performance gains. However it exists because the command line toffee program can use it for generating smaller browser-side templates.
  • autoEscape: bool (default = true): If you set this to false, in Toffee mode, #{} regions won't be escaped for HTML safety.

Render Options

Passed as vars to a template:

  • escape: function : If you pass your own escape function (and leave autoEscape on in the engine), it will be used instead of the default. Your escape function should expect and return a string. Examples follow.

Examples

Express 3:

This tells Express to use Toffee, but when an error is hit to return an error. Instead of faking a good result:

toffee    = require 'toffee'
app.set 'view engine', 'toffee'
toffee.expressEngine.prettyPrintErrors = false
toffee.expressEngine.prettyLogErrors   = false # Express will log them for you

Express 3: custom logging

This tells express to use Toffee, turns off Toffee's logging of errors, and does its own logging.

toffee    = require 'toffee'
app.set 'view engine', 'toffee'
toffee.expressEngine.prettyLogErrors   = false # we'll fake a good result and do our own logging
toffee.expressEngine.additionalErrorHandler = (pretty_text, pretty_html, file_name) ->
  for err_line in pretty_text.split "\n"
    console.log "Oh shit: #{err_line}"

General NodeJS

This is the same as above, but using a new engine from scratch

toffee = require 'toffee'
engine = new toffee.engine {
   prettyLogErrors: false
   additionalErrorHandler: (pretty_text, pretty_html, file_name) ->
      for err_line in pretty_text.split "\n"
        console.log "Oh shit: #{err_line}"
}

Overriding the escape function in a call to render

For some classy reason, let's say you want to bold every var published to the page, and you don't want to worry about HTML safety:

engine.render 'foo.toffee', {
  title: "My test page"
  escape: (s) -> "<b>#{s}</b>"
}, (err, res) ->
  # do something with err, res

See Also

Clone this wiki locally