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. Callteddy.setVerbosity(n)wherenequals one of the below values to change the default:0or'none': No logging.1or'concise': The default. Concise logging. Will usually only log serious errors.2or'verbose': Verbose logging. Logs even minor errors.3,'debug', or'DEBUG': Debug mode. Very verbose.
teddy.setDefaultParams(): Reset all params to default.teddy.maxPasses(n): Sets the maximum number of passes the parser can execute over your template. If this maximum is exceeded, Teddy will stop attempting to render the template. The limit exists to prevent the possibility of teddy producing infinite loops due to improperly coded templates. Default:1000.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
cachein 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 cachesetting, which it enables in production and disables in development, and Teddy uses it. An explicitcachein the model still wins. - Templates registered with
teddy.setTemplateare 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.
teddy.setCache(params): Declare a template-level cache.- Params:
template: Name of the template to cache.key: Model variable to cache it by. Set to'none'to cache the first render for all model values.maxAge: How old the cache can be in milliseconds before it is invalidated and will be re-rendered. Default:0(no limit).maxCaches: The maximum number of caches that Teddy will be allowed to create for a given template/key combination. If the maximum is reached, Teddy will remove the oldest cache in the stack, where oldest is defined as the least recently created or accessed. Does not apply to caches wherekeyis not also set. Default:1000.
- Example:
teddy.setCache({ template: 'someTemplate.html', key: 'city', maxAge: 1000 })
- Params:
teddy.clearCache(name): Ifnameis a string, it will delete the whole cache at that name.teddy.clearCache(name, keyVal): Deletes just the value at thatkeyVal. Assumesnamewill be a string.teddy.clearCache(params): Ifparamsis an object, it will delete a whole template-level cache.- Params:
template: Name of the template to delete the cache of.key: Model variable cache index to delete it by. Ifkeyis not provided, it will delete all caches of that template.
- Params: