Created
December 4, 2019 17:45
-
-
Save ericzon/ec36c5551c4573d277bbcae18d6b331c to your computer and use it in GitHub Desktop.
Tagged template literals
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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