1.3.0 • Published 4 years ago

felid-handlebars v1.3.0

Weekly downloads
1
License
MIT
Repository
github
Last release
4 years ago

felid-handlebars

npm version Node.js CI JavaScript Style Guide codecov

A Felid plugin for rendering Handlebars templates.

Install

npm install felid-handlebars

or

yarn add felid-handlebars

Usage

const Felid = require('felid')
const handlebars = require('felid-handlebars')

const app = new Felid()
app.plugin(handlebars, options)

app.get('/', (req, res) => {
  res.render('index.hbs', { foo: 'bar' })
})

Options

  • root: String: The directory where your template files locates. Default to the directory where node runs.
  • charset: String: The charset your templates use. Default is: utf-8.
  • production: Boolean: If true, an LRU cache will be used to caching templates. Default is depend on the value of process.env.NODE_ENV.
  • initCache: Boolean: Whether to initialize the template caches when the plugin is being loaded. Default is: false.
  • cacheOptions: Object: The options passed to lru-cache. Where max has a default value of 100.
  • decorator Object: Customize the decorator names. Default is:
{
  render: 'render'
}
  • onInit Function(engine HandlebarsInstance): If you need to customize the environment of Handlebars, just do it here! Read more about what you can do with Handlebars.
app.plugin(handlebars, {
  onInit (hbs) {
    // hbs.registerPartial(name, partial)
    // hbs.registerHelper(name, helper)
  }
})
  • onProcess Function(html String) => String: A function used to process the HTML string before it is sent to the client.
app.plugin(handlebars, {
  onProcess (html) {
    // Do something with html here. For example: minify.
    return html
  }
})
  • onError Function(error: Error, response: FelidResponse): A function invoked when an error occured in res.render().

API

  • response.render(template: String, context?: Object): Render the template using the context.

License

MIT