2

Is there an easy way to do a one time template string compile/one way data bind? I do not need dynamic components just something that would process template string e.g.My name is {{person.name}} and bind a supplied context e.g. {name: 'John'}.

Mosh Feu
  • 28,354
  • 16
  • 88
  • 135
sdev
  • 115
  • 1
  • 7

1 Answers1

0

If you use or similar (or specific browsers) you can use Template literals, like this:

var person = { name: 'john' };
var result = `My name is ${person.name}`;

console.log(result);

If not, you can use a regex like this:

var person = { name: 'john' };
var result = 'My name is {{person.name}}'.replace(/{{?.*}}/, function(a) {
  return eval(a);
});

console.log(result);

Of course that it's dummy demo but this is the principal.

Mosh Feu
  • 28,354
  • 16
  • 88
  • 135