Home

Awesome

es6-template npmjs.com The MIT License npm downloads

Small, sync and async es6 template engine, built on top of gana and ES6/ES2015 Template Strings, working on node@0.10 too!

code climate standard code style travis build status coverage status dependency status

Background

Behind the scenes es6-template uses gana which in turns use gana-compile. So the footprint and codebase is very small (~1-2kb minified and not gzipped), easy (just sync and async .compile and .render methods) and very well tested (this one has ~25 tests).

Works well on browsers and even in node@0.10. Read more on gana readme.

Install

npm i es6-template --save

Usage

For more use-cases see the tests

const es6template = require('es6-template')

es6template

Render template with locals and optionally pass a cb callback. If no callback is passed, the rendered string is returned. It is alias of and acts like .render method.

Params

Example

var es6template = require('es6-template')

var template = 'Hello ${ucfirst(author.name)} and have a ${mood} day!'
var locals = {
  author: {
    name: 'charlike'
  },
  mood: 'nice',
  ucfirst: function ucfirst (val) {
    return val.charAt(0).toUpperCase() + val.slice(1)
  }
}

// synchronous
var str = es6template(template, locals)
console.log(str)
// => 'Hello Charlike and have a nice day!'

// async
es6template(template, locals, function cb (err, res) {
  if (err) return console.error(err)

  console.log(res)
  // => 'Hello Charlike and have a nice day!'
})

.compile

Compile a template to a function that can accept locals object to render a string. If cb is passed, it pass a compileFn as result. It's a gana mirror, so if there's a problem, so please report it in the gana's issue tracker.

Params

Example

var es6template = require('es6-template')

var template = 'You, ${uppercase(name)}, are awesome ${person}!'
var locals = {
  name: 'charlike',
  person: 'developer',
  uppercase: function uppercase (val) {
    return val.toUpperCase()
  }
}

// sync
var compileFn = es6template.compile(template)
var result = compileFn(locals)
console.log(result)
// => 'You, CHARLIKE, are awesome developer!'

// asynchronous, gives you `compileFn` in the callback
es6template(template, function cb (err, compileFn) {
  if (err) return console.error(err)

  var result = compileFn(locals)
  console.log(result)
  // => 'You, CHARLIKE, are awesome developer!'
})

.render

Renders a template with locals. If no cb is passed, returns a rendered string, otherwise pass the result to cb callback function. Acts like a es6template() which is mirror of this one. If there are some problems, please report them to the gana or gana-compile issue trackers, because this basically is gana(template)(locals).

Params

Example

var es6template = require('es6-template')

var str = es6template.render('Hello ${name}.', { name: 'Charlike' })
console.log(str)
// => 'Hello Charlike.'

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
But before doing anything, please read the CONTRIBUTING.md guidelines.

Charlike Make Reagent new message to charlike freenode #charlike

tunnckoCore.tk keybase tunnckoCore tunnckoCore npm tunnckoCore twitter tunnckoCore github