Skip to content

Instantly share code, notes, and snippets.

@afitiskin
Last active December 17, 2015 10:18
Show Gist options
  • Save afitiskin/5593282 to your computer and use it in GitHub Desktop.
Save afitiskin/5593282 to your computer and use it in GitHub Desktop.
Now you can use DOM-element contents as a template. Just pass DOM-element id (like '#dom-element-id') and all those element's contents (innerHTML) will used as a template string.
(function () {
'use strict';
var cache = {};
_.templateDefault = _.template;
_.template = function (str, data) {
var match, elem;
if (match = str.match(/^#(\S+)$/)) {
if (!_.has(cache, str)) {
elem = document.getElementById(match[1]);
cache[str] = elem ? elem.innerHTML : str;
}
str = cache[str];
}
return _.templateDefault(str, data);
};
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment