Skip to content

Instantly share code, notes, and snippets.

@ericzon
Created December 4, 2019 17:45
Show Gist options
  • Save ericzon/ec36c5551c4573d277bbcae18d6b331c to your computer and use it in GitHub Desktop.
Save ericzon/ec36c5551c4573d277bbcae18d6b331c to your computer and use it in GitHub Desktop.
Tagged template literals
function generaTemplate (strings, ...keys) {
return function(data) {
let temp = strings.slice();
keys.forEach((key, i) => {
temp[i] = temp[i] + data[key];
});
return temp.join('');
}
}
const userTpl = user => generaTemplate`<article>
<p>Name: ${'name'}</p>
<p>Age: ${'age'}</p>
</article>`(user);
const users = [{
name: 'Eric',
age: '10'
}, {
name: 'Rob',
age: '20'
}];
const usersList = users.reduce((acc, user) => {
return acc + userTpl(user);
}, '');
console.log(usersList);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment