I'm trying to put together a small demonstration that uses client-side templating for a short talk. My regexp dynamically gets created and aims to replace instances of itself within a string, but it's not working for some odd reason, I've console logged the vital parts, can anyone advise?
My HTML:
<script type="text/template" id="link">
<a href="{href}">
{title}
</a>
</script>
And my JavaScript in a jsFiddle and below:
var linkTemplate = document.querySelector('#link').innerHTML.replace(/^\s+|\s+$/g, '');
var data = [{
"title": "Google",
"href": "//google.com"
},{
"title": "Yahoo",
"href": "//yahoo.com"
}];
for (var i = 0; i < data.length; i++) {
var obj = data[i];
for (key in obj) {
if (obj.hasOwnProperty(key)) {
var regexp = new RegExp('\{' + key + '\}', 'i');
console.log(regexp);
linkTemplate.replace(regexp, 'l');
console.log(obj[key]);
}
}
document.body.innerHTML += linkTemplate;
}