'JavaScript template engine - extract placeholder name from template
Is there any JavaScript template engine which lets you extract placeholder names? For example:
const template = 'Hi {{ name }}!';
const compiledTemplate = SomeTemplateEngine.compile(template);
console.log(compiledTemplate.extractPlaceholders()); // ['name']
Solution 1:[1]
I wrote a template engine called olojs that may do what you need.
Installation:
npm install @onlabsorg/olojs
Usage:
const olojs = require('@onlabsorg/olojs');
// or require('@onlabsorg/olojs/browser') if you want to use it in the browser
const evaluate = olojs.document.parse("My name is <% name: 'Marcello' %>!");
const context = olojs.document.createContext({/* preset variables here */});
const {text, data} = await evaluate(context);
// data: {name: 'Marcello'}
// text: "My name is Marcello!"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Marcello Del Buono |