Skip to content

Instantly share code, notes, and snippets.

@johnsardine
Created October 1, 2013 09:39
Show Gist options
  • Select an option

  • Save johnsardine/6776067 to your computer and use it in GitHub Desktop.

Select an option

Save johnsardine/6776067 to your computer and use it in GitHub Desktop.
Simple templating javascript using jquery
// Add this to runtime
if (!String.prototype.format) {
String.prototype.format = function() {
var args = arguments;
return this.replace(/{(\d+)}/g, function(match, number) {
return typeof args[number] != 'undefined'
? args[number]
: match
;
});
};
}
// Usage
var template = 'Today, {0}, is {1} degrees';
template.format('Monday', '24');
// Today, Monday, is 24 degrees
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment