Roosevelt

Teddy templating engine — Configuration

🔝 Scroll to top

API

  • teddy.getTemplates(): Get the internal cache of templates.

  • teddy.setTemplate(name, template): Add a new template to the template cache.

  • teddy.clearTemplates(): Clear the template cache.

  • teddy.render(template, model): Render a template by supplying either source code or a file name (in Node.js).

    • Returns fully rendered HTML.
    • Removes <!--! teddy comments --> and {! teddy comments !}
  • teddy.setTemplateRoot(path): Set the location of your templates directory. Default is the current directory.

  • teddy.compile(templateString): Takes a template string and returns a function which when given model data will render HTML from the template and model data.

    • Example:

      const templateFunction = teddy.compile('<p>{hello}</p>')
      templateFunction({ hello: 'world' }) // returns "<p>world</p>"
  • teddy.setVerbosity(n): Sets the level of verbosity in Teddy's console logs. Call teddy.setVerbosity(n) where n equals one of the below values to change the default:

    • 0 or 'none': No logging.
    • 1 or 'concise': The default. Concise logging. Will usually only log serious errors.
    • 2 or 'verbose': Verbose logging. Logs even minor errors.
    • 3, 'debug', or 'DEBUG': Debug mode. Very verbose.
  • teddy.setDefaultParams(): Reset all params to default.

  • teddy.setEmptyVarBehavior('hide'): Will make it possible for variables which don't resolve to display as empty strings instead of displaying the variable. Default: 'display'.

    • {varName|h} to force it to hide or {varName|d}
  • teddy.setIncludeNotFoundBehavior('hide'): Will make it possible for <include> tags which don't resolve to display as empty strings instead of displaying an error. Default: 'display'.

  • teddy.setCacheTemplates(true): Keep templates read from the filesystem in memory rather than reading them again on the next render. Default: false.

    • Caching is off by default so that editing a template takes effect without restarting, which is how ejs and pug treat their own caching too.
    • You can also set this per render by passing cache in the model, e.g. teddy.render(template, { cache: true }). That takes precedence over this setting.
    • When Teddy is used as an Express view engine you do not need to set any of this: Express supplies its own view cache setting, which it enables in production and disables in development, and Teddy uses it. An explicit cache in the model still wins.
    • Templates registered with teddy.setTemplate are not affected. Those are always kept, since registering one by hand is the only way to supply a template in the browser, where there is no filesystem to read from.