template.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*!art-template - Template Engine | http://aui.github.com/artTemplate/*/ ! function() {
  2. function a(a) {
  3. return a.replace(t, "").replace(u, ",").replace(v, "").replace(w, "").replace(x, "").split(y)
  4. }
  5. function b(a) {
  6. return "'" + a.replace(/('|\\)/g, "\\$1").replace(/\r/g, "\\r").replace(/\n/g, "\\n") + "'"
  7. }
  8. function c(c, d) {
  9. function e(a) {
  10. return m += a.split(/\n/).length - 1, k && (a = a.replace(/\s+/g, " ").replace(/<!--[\w\W]*?-->/g, "")), a && (a = s[1] + b(a) + s[2] + "\n"), a
  11. }
  12. function f(b) {
  13. var c = m;
  14. if (j ? b = j(b, d) : g && (b = b.replace(/\n/g, function() {
  15. return m++, "$line=" + m + ";"
  16. })), 0 === b.indexOf("=")) {
  17. var e = l && !/^=[=#]/.test(b);
  18. if (b = b.replace(/^=[=#]?|[\s;]*$/g, ""), e) {
  19. var f = b.replace(/\s*\([^\)]+\)/, "");
  20. n[f] || /^(include|print)$/.test(f) || (b = "$escape(" + b + ")")
  21. } else b = "$string(" + b + ")";
  22. b = s[1] + b + s[2]
  23. }
  24. return g && (b = "$line=" + c + ";" + b), r(a(b), function(a) {
  25. if (a && !p[a]) {
  26. var b;
  27. b = "print" === a ? u : "include" === a ? v : n[a] ? "$utils." + a : o[a] ? "$helpers." + a : "$data." + a, w += a + "=" + b + ",", p[a] = !0
  28. }
  29. }), b + "\n"
  30. }
  31. var g = d.debug,
  32. h = d.openTag,
  33. i = d.closeTag,
  34. j = d.parser,
  35. k = d.compress,
  36. l = d.escape,
  37. m = 1,
  38. p = {
  39. $data: 1,
  40. $filename: 1,
  41. $utils: 1,
  42. $helpers: 1,
  43. $out: 1,
  44. $line: 1
  45. },
  46. q = "".trim,
  47. s = q ? ["$out='';", "$out+=", ";", "$out"] : ["$out=[];", "$out.push(", ");", "$out.join('')"],
  48. t = q ? "$out+=text;return $out;" : "$out.push(text);",
  49. u = "function(){var text=''.concat.apply('',arguments);" + t + "}",
  50. v = "function(filename,data){data=data||$data;var text=$utils.$include(filename,data,$filename);" + t + "}",
  51. w = "'use strict';var $utils=this,$helpers=$utils.$helpers," + (g ? "$line=0," : ""),
  52. x = s[0],
  53. y = "return new String(" + s[3] + ");";
  54. r(c.split(h), function(a) {
  55. a = a.split(i);
  56. var b = a[0],
  57. c = a[1];
  58. 1 === a.length ? x += e(b) : (x += f(b), c && (x += e(c)))
  59. });
  60. var z = w + x + y;
  61. g && (z = "try{" + z + "}catch(e){throw {filename:$filename,name:'Render Error',message:e.message,line:$line,source:" + b(c) + ".split(/\\n/)[$line-1].replace(/^\\s+/,'')};}");
  62. try {
  63. var A = new Function("$data", "$filename", z);
  64. return A.prototype = n, A
  65. } catch (B) {
  66. throw B.temp = "function anonymous($data,$filename) {" + z + "}", B
  67. }
  68. }
  69. var d = function(a, b) {
  70. return "string" == typeof b ? q(b, {
  71. filename: a
  72. }) : g(a, b)
  73. };
  74. d.version = "3.0.0", d.config = function(a, b) {
  75. e[a] = b
  76. };
  77. var e = d.defaults = {
  78. openTag: "<%",
  79. closeTag: "%>",
  80. escape: !0,
  81. cache: !0,
  82. compress: !1,
  83. parser: null
  84. },
  85. f = d.cache = {};
  86. d.render = function(a, b) {
  87. return q(a, b)
  88. };
  89. var g = d.renderFile = function(a, b) {
  90. var c = d.get(a) || p({
  91. filename: a,
  92. name: "Render Error",
  93. message: "Template not found"
  94. });
  95. return b ? c(b) : c
  96. };
  97. d.get = function(a) {
  98. var b;
  99. if (f[a]) b = f[a];
  100. else if ("object" == typeof document) {
  101. var c = document.getElementById(a);
  102. if (c) {
  103. var d = (c.value || c.innerHTML).replace(/^\s*|\s*$/g, "");
  104. b = q(d, {
  105. filename: a
  106. })
  107. }
  108. }
  109. return b
  110. };
  111. var h = function(a, b) {
  112. return "string" != typeof a && (b = typeof a, "number" === b ? a += "" : a = "function" === b ? h(a.call(a)) : ""), a
  113. },
  114. i = {
  115. "<": "&#60;",
  116. ">": "&#62;",
  117. '"': "&#34;",
  118. "'": "&#39;",
  119. "&": "&#38;"
  120. },
  121. j = function(a) {
  122. return i[a]
  123. },
  124. k = function(a) {
  125. return h(a).replace(/&(?![\w#]+;)|[<>"']/g, j)
  126. },
  127. l = Array.isArray || function(a) {
  128. return "[object Array]" === {}.toString.call(a)
  129. },
  130. m = function(a, b) {
  131. var c, d;
  132. if (l(a))
  133. for (c = 0, d = a.length; d > c; c++) b.call(a, a[c], c, a);
  134. else
  135. for (c in a) b.call(a, a[c], c)
  136. },
  137. n = d.utils = {
  138. $helpers: {},
  139. $include: g,
  140. $string: h,
  141. $escape: k,
  142. $each: m
  143. };
  144. d.helper = function(a, b) {
  145. o[a] = b
  146. };
  147. var o = d.helpers = n.$helpers;
  148. d.onerror = function(a) {
  149. var b = "Template Error\n\n";
  150. for (var c in a) b += "<" + c + ">\n" + a[c] + "\n\n";
  151. "object" == typeof console && console.error(b)
  152. };
  153. var p = function(a) {
  154. return d.onerror(a),
  155. function() {
  156. return "{Template Error}"
  157. }
  158. },
  159. q = d.compile = function(a, b) {
  160. function d(c) {
  161. try {
  162. return new i(c, h) + ""
  163. } catch (d) {
  164. return b.debug ? p(d)() : (b.debug = !0, q(a, b)(c))
  165. }
  166. }
  167. b = b || {};
  168. for (var g in e) void 0 === b[g] && (b[g] = e[g]);
  169. var h = b.filename;
  170. try {
  171. var i = c(a, b)
  172. } catch (j) {
  173. return j.filename = h || "anonymous", j.name = "Syntax Error", p(j)
  174. }
  175. return d.prototype = i.prototype, d.toString = function() {
  176. return i.toString()
  177. }, h && b.cache && (f[h] = d), d
  178. },
  179. r = n.$each,
  180. s = "break,case,catch,continue,debugger,default,delete,do,else,false,finally,for,function,if,in,instanceof,new,null,return,switch,this,throw,true,try,typeof,var,void,while,with,abstract,boolean,byte,char,class,const,double,enum,export,extends,final,float,goto,implements,import,int,interface,long,native,package,private,protected,public,short,static,super,synchronized,throws,transient,volatile,arguments,let,yield,undefined",
  181. t = /\/\*[\w\W]*?\*\/|\/\/[^\n]*\n|\/\/[^\n]*$|"(?:[^"\\]|\\[\w\W])*"|'(?:[^'\\]|\\[\w\W])*'|\s*\.\s*[$\w\.]+/g,
  182. u = /[^\w$]+/g,
  183. v = new RegExp(["\\b" + s.replace(/,/g, "\\b|\\b") + "\\b"].join("|"), "g"),
  184. w = /^\d[^,]*|,\d[^,]*/g,
  185. x = /^,+|,+$/g,
  186. y = /^$|,+/;
  187. e.openTag = "{{", e.closeTag = "}}";
  188. var z = function(a, b) {
  189. var c = b.split(":"),
  190. d = c.shift(),
  191. e = c.join(":") || "";
  192. return e && (e = ", " + e), "$helpers." + d + "(" + a + e + ")"
  193. };
  194. e.parser = function(a) {
  195. a = a.replace(/^\s/, "");
  196. var b = a.split(" "),
  197. c = b.shift(),
  198. e = b.join(" ");
  199. switch (c) {
  200. case "if":
  201. a = "if(" + e + "){";
  202. break;
  203. case "else":
  204. b = "if" === b.shift() ? " if(" + b.join(" ") + ")" : "", a = "}else" + b + "{";
  205. break;
  206. case "/if":
  207. a = "}";
  208. break;
  209. case "each":
  210. var f = b[0] || "$data",
  211. g = b[1] || "as",
  212. h = b[2] || "$value",
  213. i = b[3] || "$index",
  214. j = h + "," + i;
  215. "as" !== g && (f = "[]"), a = "$each(" + f + ",function(" + j + "){";
  216. break;
  217. case "/each":
  218. a = "});";
  219. break;
  220. case "echo":
  221. a = "print(" + e + ");";
  222. break;
  223. case "print":
  224. case "include":
  225. a = c + "(" + b.join(",") + ");";
  226. break;
  227. default:
  228. if (/^\s*\|\s*[\w\$]/.test(e)) {
  229. var k = !0;
  230. 0 === a.indexOf("#") && (a = a.substr(1), k = !1);
  231. for (var l = 0, m = a.split("|"), n = m.length, o = m[l++]; n > l; l++) o = z(o, m[l]);
  232. a = (k ? "=" : "=#") + o
  233. } else a = d.helpers[c] ? "=#" + c + "(" + b.join(",") + ");" : "=" + a
  234. }
  235. return a
  236. }, "function" == typeof define ? define(function() {
  237. return d
  238. }) : "undefined" != typeof exports ? module.exports = d : this.template = d
  239. }();