es6-dynamic-template
v2.0.0
Published
Dynamically build ES6 templates
Downloads
8,602
Readme
ES6 Dynamic Templates
Simple dynamic ES6 templates
Actual real ES6 templates. Just dynamic.
Why would I want to make ES6 templates dynamically?
Because you have a whole bunch of templates, and don't want to write a function for every possible template string. The purpose of the module is that variables are resolved later, which makes it easier to do dynamic templating. This is surprisingly tricky as this
for TemplateStrings is a bit weird.
Changes since version 1
Version 1 used eval
, requiring you to sanitise user input before use. While that's a commopn expectation for web development, the new version uses a regex based approach while keeping the same API.
Usage
Load the module:
const fillTemplate = require('es6-dynamic-template');
Use it (use regular quotes, not backticks, for template string):
const greeting = fillTemplate('Hi ${firstName}', {firstName: 'Joe'});
Returns:
'Hi Joe'
Tests
mocha
Issues
Everything works, AFAIK, if it doesn't, send a PR rather than complaining otherwise I will mock you.