janl / mustache.js

Minimal templating with {{mustaches}} in JavaScript

Home Page:https://mustache.github.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

providing fallback value if no match found.

ehxxn opened this issue · comments

I am trying to use mustache to replace all placeholder inside JSON with the actual value and then parse them.

but if there is no match then mustache render an empty space with is an invalid JSON.
can I render my own custom value as fallback in no match found.

here is an example

let template =  "{name: {{name}}, age: {{age}}}"
let invalidJSON = Mustache.render(template, {name: "Ehsan"});
console.log(json)
// {name: Ehsan, age: }

the result is an invalid JSON

if I call JSON.parse(json) it throws error.

If there is a way that mustache render a fallback string instead of empty space then everything is working find