Skip to content

Instantly share code, notes, and snippets.

@milesplit
Created November 15, 2011 07:31
Show Gist options
  • Save milesplit/1366395 to your computer and use it in GitHub Desktop.
Save milesplit/1366395 to your computer and use it in GitHub Desktop.
Zepto Simple Template Plugin
// Template
// (c) 2011 Jason Byrne, MileSplit
// May be freely distributed under the MIT license, with attribution.
//
// ** Usage **
//
// HTML:
// <script type="text/html" id="tmplArticle"><h1>${Title}</h1></script>
//
// JavaScript:
// $('#tmplArticle').tmpl(data).appendTo($item);
//
(function($){
$.fn.tmpl = function(d) {
var s = $(this[0]).html().trim();
if (d) {
for (k in d) {
s = s.replace(new RegExp('\\${' + k + '}', 'g'), d[k]);
}
}
return $(s);
};
})(Zepto);
@milesplit
Copy link
Author

$.fn.tmpl=function(a){var b=$(this[0]).html().trim();if(a){for(k in a){b=b.replace(new RegExp("${"+k+"}","g"),a[k])}}return $(b)}

@jasonbyrne
Copy link

$.fn.tmpl = function(d) {
var s = $(this[0]).html().trim();
if (d) {
for (k in d) {
s = s.replace('${' + k + '}', d[k]);
}
}
return $(s);
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment