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)
wheren
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.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.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 wherekey
is not also set. Default:1000
.
- Example:
teddy.setCache({ template: 'someTemplate.html', key: 'city', maxAge: 1000 })
- Params:
teddy.clearCache(name)
: Ifname
is a string, it will delete the whole cache at that name.teddy.clearCache(name, keyVal)
: Deletes just the value at thatkeyVal
. Assumesname
will be a string.teddy.clearCache(params)
: Ifparams
is 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. Ifkey
is not provided, it will delete all caches of that template.
- Params: