Created
March 18, 2014 14:07
-
-
Save nkmdk-007/9620770 to your computer and use it in GitHub Desktop.
renderItem(template, object) - A little method to switch out fields in a template from an object
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 renderItem(param_renItem, item) { | |
var renItem = param_renItem; | |
var tp = renItem.match(/{{[\d\w]*}}/g); | |
var indx; | |
var reg; | |
for (indx = 0; indx < tp.length; indx++) { | |
var p = tp[indx].replace(/[{}]/g, ''); | |
reg = new RegExp(tp[indx], "g"); | |
if (item.hasOwnProperty(p)) { | |
renItem = renItem.replace(reg, item[p]); | |
} | |
else { | |
renItem = renItem.replace(reg, ''); | |
} | |
} | |
return renItem; | |
}; | |
/* | |
example usage... | |
<script id="test"> | |
<div class="js-component"> | |
<img src="{{imageSource}}"> | |
<p>{{theCaption}}<p> | |
</div> | |
</script> | |
var template = $('#test').html(), | |
object = { | |
imageSource : 'https://www.google.co.uk/images/srpr/logo11w.png', | |
theCaption : 'Web-based media often present multiple...' | |
}; | |
alert( renderItem(template, object) ); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment