Here's how I do it in my projects:
Define a template in your HTML:
<script type="text/template" id="cardTemplate"> <div> <a href="{0}">{1}</a> </div> </script>
Use string.format to replace variables:
var cardTemplate = $("#cardTemplate").html(); var template = cardTemplate.format("http://example.com", "Link Title"); $("#container").append(template);
string.format :
String.prototype.format = function() { var args = arguments; return this.replace(/{(\d+)}/g, function(match, number) { return typeof args[number] != 'undefined' ? args[number] : match ; }); };
Pretty simple, you can even combine patterns.
sachleen Dec 27 '12 at 23:01 2012-12-27 23:01
source share