2

With ES6 string templates polyfill I can do following

var template = require('es6-template-strings');

//external input
var templateStr = "Hello, ${name}!";

var data = {name: "John"};

console.log(template(templateStr, data)); //>>Hello, John!

Is it possible to do the same using native ES6 templates?

Note: I get templateStr dynamically (in my case that is from json file with error messages), so I cannot use template strings syntax directly (like var str = Hello, ${name}!;)

There was the same question and answer is it is not possible without using eval.

Yegor
  • 525
  • 6
  • 6
  • 2
    YES, [Tagged template strings](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/template_strings#Tagged_template_strings) – Tushar Jan 28 '16 at 04:08
  • except for the scope that object-fed templaters typically use. one can simulate that or be more specific with native templates: `data={name: "John"};with(data)alert(\`Hello, ${name}!\`);` or `data={name: "John"};alert(\`Hello, ${data.name}!\`);` – dandavis Jan 28 '16 at 04:44
  • @Tushar: i don't think OP wants tags, just template strings, which already work in modern browsers. – dandavis Jan 28 '16 at 04:46
  • 1
    There is no such thing as "ES6 templates". Template strings are *string interpolation*, nothing more. – Bergi Jan 28 '16 at 05:16

0 Answers0