mustache-render Render Mustache template with JSON data
Examples
Render a Mustache template with JSON data
Usage
"Hello, {{name}}! You have {{count}} messages.
---
{"name":"A..." | mustache-render Preview a personalized order confirmation email
Usage
"Hi {{first_name}},
Your order #{{order_id}} has shipped!
T..." | mustache-render Generate a blog post header from template + metadata
Usage
"# {{title}}
By {{author}} | {{date}}
{{summary}}
---
{"tit..." | mustache-render View source
async (input)=>{
const sep = input.indexOf("\n---\n");
if (sep === -1) return "Error: separate template and JSON data with a line containing only ---";
const template = input.slice(0, sep);
const dataStr = input.slice(sep + 5);
try {
const { default: Mustache } = await import('mustache').then(async (m)=>{
await m.__tla;
return m;
});
const data = JSON.parse(dataStr);
return Mustache.render(template, data);
} catch (e) {
return `Error: ${e.message}`;
}
}