Skip to content

Instantly share code, notes, and snippets.

@Steamforge
Last active July 2, 2021 12:43
Show Gist options
  • Save Steamforge/5bbcf400aea394846d12350bce956d32 to your computer and use it in GitHub Desktop.
Save Steamforge/5bbcf400aea394846d12350bce956d32 to your computer and use it in GitHub Desktop.
A Template Literal Funciton
// an array of colors
let colors = ['red', 'green', 'blue'];
// a function to build a list
let makeTemplate = function (data) {
let newList = '';
data.forEach(function(element) {
newList += `<li>${element}</li>`;
});
return newList;
};
// build a container template
let template = `<ul>
${makeTemplate(colors)}
</ul>`;
// add the template to the page
document.getElementById('result').innerHTML = template;
/** output
* <ul>
* <li>red</li>
* <li>green</li>
* <li>blue</li>
* </ul>
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment