Basic example:
var template = require('es6-template-strings');
// Hello WORLD!
console.log(template('Hello ${place.toUpperCase()}!', { place: "World" }));
// You can reuse same templates:
var compile = require('es6-template-strings/compile')
, resolveToString = require('es6-template-strings/resolve-to-string')
, compiled = compile('Welcome to ${siteName}, you are visitor number ${visitorNumber}!');
// Welcome to MySite, you are visitor number 137!
console.log(resolveToString(compiled, { siteName: "MySite", visitorNumber: 137 }));
// Welcome to OtherSite, you are visitor number 777!
console.log(resolveToString(compiled, { siteName: "OtherSite", visitorNumber: 777 }));
// You may prepare custom tag functions
var resolve = require('es6-template-strings/resolve');
var customTag = function (literals/*, …substitutions*/) {
// Process input and return result string
};
// Output template processed by customTag:
customTag.apply(null, resolve(compiled, {/* context */}));
With partial: true
option, it's possible to resolve just some variables from a template.
It's useful when we want to resolve template in more than one pass.
var partialTemplate = template(
'Hello ${place.toUpperCase()}! Today is ${day}',
{ place: "World" },
{ partial: true }
); // Hello WORLD! Today is ${day}
template(partialTemplate, { day: "Tuesday" }); // Hello WORLD! Today is Tuesday
// Same way `partial` option can go to any resolve* util, e.g.:
resolveToString(compiled, { place: "World" }, { partial: true });
In your project path:
$ npm install es6-template-strings
You can easily bundle es6-template-strings for browser with modules-webmake
$ npm test