Awesome
es6-template
Small, sync and async es6 template engine, built on top of gana and ES6/ES2015 Template Strings, working on
node@0.10
too!
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
withlocals
and optionally pass acb
callback. If no callback is passed, the renderedstring
is returned. It is alias of and acts like.render
method.
Params
<template>
{String}: string to be rendered.<locals>
{Object}: data to be used intemplate
.[cb]
{Function}: callback withcb(err, res)
signature.returns
{String}: if nocb
is passed.
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 acceptlocals
object to render a string. Ifcb
is passed, it pass acompileFn
as result. It's a gana mirror, so if there's a problem, so please report it in the gana's issue tracker.
Params
<template>
{String}: string to be compile to a function.[cb]
{Function}: callback withcb(err, compileFn)
signature.returns
{Function}: if nocb
is passed.
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
withlocals
. If nocb
is passed, returns a rendered string, otherwise pass the result tocb
callback function. Acts like aes6template()
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 isgana(template)(locals)
.
Params
<template>
{String}: string to be rendered.<locals>
{Object}: data to be used intemplate
.[cb]
{Function}: callback withcb(err, res)
signature.returns
{String}: if nocb
is passed.
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.