We have standard way: Template literals.
const str = `${0} ${'foo'}!`;
fn('Hello', {foo: 'World'});
i18n Vue has another way: Linked locale messages
We have standard way: Template literals.
const str = `${0} ${'foo'}!`;
fn('Hello', {foo: 'World'});
i18n Vue has another way: Linked locale messages
Also you can use render function in Vue with template format such as Hello, {{foo}}!
.
Tiny Mustache template renderer:
const json = {
template: 'by formula: {{formula}}',
formula: 'SP+{{gamma}}*25',
gamma: '({{delta}}+4)',
delta: 2,
};
function render (tmpl, obj) {
const regex = new RegExp('{{([^}]+)}}', 'g');
var rendered;
const regReplacerFn = function (match, prop) {
var retVal = obj[prop];
if (typeof retVal !== 'string' ||
typeof retVal === 'string' && retVal.indexOf('{{') == -1) {
return retVal;
}
return retVal.replace(regex, regReplacerFn);
};
rendered = tmpl.replace(regex, regReplacerFn);
return rendered;
}
console.log(render(json.template, json));
But Template literals are not templates! They can't be stored separately of declarations.
Use simple strings for template instead & custom interpolate function.