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

Render Advanced Options

  • escape: function : If you pass your own escape function (and leave autoEscape=true in the engine), your escape will override the default.

Example

Let's say you want to bold every var published to the page, and you don't want to worry about HTML safety.

If you're using Express, just assign a variable, 'escape', when calling res.render:

# Express 3.x
exports.index = (req, res) ->
  res.render 'index', { 
    title: 'Express' 
    escape: (s) -> "<b>#{s}</b>"
  }

Or, if you've rolled your own Toffee engine and are calling render yourself:

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

Engine Advanced Options

When you're creating your own engine, you can pass the following attributes. You can also reassign them to an already existing engine, such as toffee.expressEngine.

  • 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.

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}"
}

See Also

Clone this wiki locally