Last active
September 26, 2015 09:17
-
-
Save Javiani/1074272 to your computer and use it in GitHub Desktop.
Replacing string variables
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
/* | |
Use case: | |
template = $('#template'); | |
merge({ name :'My name'}, template.html()); // <div>My name</div> | |
<script type="text/html"> | |
<div>#{name}</div> | |
</script> | |
*/ | |
var merge = function( json, template ){ | |
var pattern = /.{(\w*)\}/g; | |
var html = template.replace( pattern, function(key){ | |
return json[ key.slice(2, -1) ]; | |
}); | |
return html; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment