Skip to content

Instantly share code, notes, and snippets.

@kn9ts
Forked from cowboy/interpolate.js
Last active August 29, 2015 14:20
Show Gist options
  • Save kn9ts/e516fe9337cb5bca1c39 to your computer and use it in GitHub Desktop.
Save kn9ts/e516fe9337cb5bca1c39 to your computer and use it in GitHub Desktop.
// Uber-basic templating.. good? bad? ugly?
function interpolate( str, data ) {
data = data || window;
return !str
? ''
: str.replace( /{([^}]*)}/g, function(a,b){
return data[ b ] || '';
});
};
var data = { foo: 123, bar: 456 };
interpolate( 'http://example.com/{foo}/{bar}/', data ); // "http://example.com/123/456/"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment