Advertisement
Guest User

Untitled

a guest
Jun 30th, 2015
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. (function (doT) {
  2. var templates = {};
  3. var scripts = Array.prototype.slice.call(document.getElementsByTagName('script')); // load all scripts
  4. for (var i = 0; i < scripts.length; i++) { // filter out template script tags
  5. var script = scripts[i];
  6. if (script.type == "text/dot-template") {
  7. var name = script.id || script.getAttribute('name') || script.getAttribute('data-name');
  8. templates[name] = script.innerHTML; // store template for later use
  9. script.parentNode.removeChild(script); // remove template from DOM
  10. }
  11. }
  12. var cache = {};
  13. /**
  14. * Get template function by name.
  15. */
  16. doT.tmpl = function (name) {
  17. if (!templates[name])
  18. throw new Error("template \"" + name + "\" not found!");
  19. if (!cache[name])
  20. cache[name] = doT.template(templates[name]);
  21. return cache[name];
  22. };
  23. /**
  24. * Render template with provided data.
  25. */
  26. doT.render = function (name, data) {
  27. var tmpl = doT.tmpl(name);
  28. if (!tmpl)
  29. return undefined;
  30. return tmpl(data);
  31. };
  32. })(window.doT);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement