Skip to content

Instantly share code, notes, and snippets.

@Javiani
Last active September 26, 2015 09:17
Show Gist options
  • Save Javiani/1074272 to your computer and use it in GitHub Desktop.
Save Javiani/1074272 to your computer and use it in GitHub Desktop.
Replacing string variables
/*
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