Created
November 14, 2011 17:10
-
-
Save gryzzly/1364474 to your computer and use it in GitHub Desktop.
renderTemplate from DoAT Touchy, basic JS templating
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
// Render JavaScript template (from http://git.io/B7KxZg DoAT Touchy) | |
// | |
// You can place your template in a script tag with type other than "text/javascript" | |
// such as "text/html", like this: | |
// <script type="text/html" id="item-template"> | |
// <li id="{{id}}"> | |
// <p>{{content}}</p> | |
// </li> | |
// You'd then render your template like this: | |
// var itemTemplate = document.getElementById('item-template').innerHTML; | |
// renderTemplate( itemTemplate, { | |
// id : "item1", | |
// content : "lorem ipsum dolor sit amet …" | |
// }); | |
var renderTemplate = function(str, attrArr){ | |
if (!attrArr){return str} | |
for (var key in attrArr){ | |
var keyRegex = new RegExp("{"+key+"}", "g"); | |
var value = attrArr[key]; | |
str = str.replace(keyRegex, value); | |
} | |
return str; | |
}; |
Author
gryzzly
commented
Jun 29, 2012
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment