Created
October 1, 2013 09:39
-
-
Save johnsardine/6776067 to your computer and use it in GitHub Desktop.
Simple templating javascript using jquery
This file contains hidden or 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
| // 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