Skip to content

Instantly share code, notes, and snippets.

@matthewp
Created June 19, 2012 19:08
Show Gist options
  • Save matthewp/2955953 to your computer and use it in GitHub Desktop.
Save matthewp/2955953 to your computer and use it in GitHub Desktop.
Synchronous loading of templates in a link tag.
(function() {
"use strict";
Object.defineProperty(HTMLLinkElement.prototype, 'template', {
get: function() {
if(!/template/i.test(this.rel)) {
return "";
}
var req = new XMLHttpRequest();
req.open('GET', this.href, false);
req.setRequestHeader('Accept', this.type || "*/*");
req.send();
if(req.status !== 200) {
throw "Unable to retrieve the template.";
} else {
return req.responseText;
}
}
});
})();
@matthewp
Copy link
Author

This is template engine agnostic, intentionally. You can you any style you prefer, all this does is fetch the template from a link tag.

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