[es6-template][author-www-url] [![npmjs.com][npmjs-img]][npmjs-url] [![The MIT License][license-img]][license-url] [![npm downloads][downloads-img]][downloads-url]
September 22, 2016 ยท View on GitHub
Small, sync and async es6 template engine, built on top of gana and ES6/ES2015 Template Strings, working on
node@0.10too!
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
templatewithlocalsand optionally pass acbcallback. If no callback is passed, the renderedstringis returned. It is alias of and acts like.rendermethod.
Params
<template>{String}: string to be rendered.<locals>{Object}: data to be used intemplate.[cb]{Function}: callback withcb(err, res)signature.returns{String}: if nocbis 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
templateto a function that can acceptlocalsobject to render a string. Ifcbis passed, it pass acompileFnas 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 nocbis 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
templatewithlocals. If nocbis passed, returns a rendered string, otherwise pass the result tocbcallback 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 nocbis 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.