You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

35 line
1.1 KiB

  1. // Simple JavaScript Templating
  2. // Adapted from John Resig - http://ejohn.org/ - MIT Licensed
  3. frappe.template = {compiled: {}, debug:{}};
  4. frappe.template.compile = function(str) {
  5. if(str.indexOf("'")!==-1) {
  6. console.log("Warning: Single quotes (') may not work in templates");
  7. }
  8. if(!frappe.template.compiled[str]) {
  9. fn_str = "var p=[],print=function(){p.push.apply(p,arguments)};" +
  10. // Introduce the data as local variables using with(){}
  11. "with(obj){p.push('" +
  12. // Convert the template into pure JavaScript
  13. str
  14. .replace(/[\r\t\n]/g, " ")
  15. .split("{%").join("\t")
  16. .replace(/((^|%})[^\t]*)'/g, "$1\r")
  17. .replace(/\t=(.*?)%}/g, "',$1,'")
  18. .split("\t").join("');")
  19. .split("%}").join("p.push('")
  20. .split("\r").join("\\'")
  21. + "');}return p.join('');";
  22. frappe.template.debug[str] = fn_str;
  23. frappe.template.compiled[str] = new Function("obj", fn_str);
  24. }
  25. return frappe.template.compiled[str];
  26. };
  27. frappe.render = function(str, data) {
  28. return frappe.template.compile(str)(data);
  29. };