1 | /* |
---|
2 | * JavaScript Templates 2.1.0 |
---|
3 | * https://github.com/blueimp/JavaScript-Templates |
---|
4 | * |
---|
5 | * Copyright 2011, Sebastian Tschan |
---|
6 | * https://blueimp.net |
---|
7 | * |
---|
8 | * Licensed under the MIT license: |
---|
9 | * http://www.opensource.org/licenses/MIT |
---|
10 | * |
---|
11 | * Inspired by John Resig's JavaScript Micro-Templating: |
---|
12 | * http://ejohn.org/blog/javascript-micro-templating/ |
---|
13 | */ |
---|
14 | |
---|
15 | /*jslint evil: true, regexp: true */ |
---|
16 | /*global document, define */ |
---|
17 | |
---|
18 | (function ($) { |
---|
19 | "use strict"; |
---|
20 | var tmpl = function (str, data) { |
---|
21 | var f = !/[^\w\-\.:]/.test(str) ? tmpl.cache[str] = tmpl.cache[str] || |
---|
22 | tmpl(tmpl.load(str)) : |
---|
23 | new Function( |
---|
24 | tmpl.arg + ',tmpl', |
---|
25 | "var _e=tmpl.encode" + tmpl.helper + ",_s='" + |
---|
26 | str.replace(tmpl.regexp, tmpl.func) + |
---|
27 | "';return _s;" |
---|
28 | ); |
---|
29 | return data ? f(data, tmpl) : function (data) { |
---|
30 | return f(data, tmpl); |
---|
31 | }; |
---|
32 | }; |
---|
33 | tmpl.cache = {}; |
---|
34 | tmpl.load = function (id) { |
---|
35 | return document.getElementById(id).innerHTML; |
---|
36 | }; |
---|
37 | tmpl.regexp = /([\s'\\])(?![^%]*%\})|(?:\{%(=|#)([\s\S]+?)%\})|(\{%)|(%\})/g; |
---|
38 | tmpl.func = function (s, p1, p2, p3, p4, p5) { |
---|
39 | if (p1) { // whitespace, quote and backspace in interpolation context |
---|
40 | return { |
---|
41 | "\n": "\\n", |
---|
42 | "\r": "\\r", |
---|
43 | "\t": "\\t", |
---|
44 | " " : " " |
---|
45 | }[s] || "\\" + s; |
---|
46 | } |
---|
47 | if (p2) { // interpolation: {%=prop%}, or unescaped: {%#prop%} |
---|
48 | if (p2 === "=") { |
---|
49 | return "'+_e(" + p3 + ")+'"; |
---|
50 | } |
---|
51 | return "'+(" + p3 + "||'')+'"; |
---|
52 | } |
---|
53 | if (p4) { // evaluation start tag: {% |
---|
54 | return "';"; |
---|
55 | } |
---|
56 | if (p5) { // evaluation end tag: %} |
---|
57 | return "_s+='"; |
---|
58 | } |
---|
59 | }; |
---|
60 | tmpl.encReg = /[<>&"'\x00]/g; |
---|
61 | tmpl.encMap = { |
---|
62 | "<" : "<", |
---|
63 | ">" : ">", |
---|
64 | "&" : "&", |
---|
65 | "\"" : """, |
---|
66 | "'" : "'" |
---|
67 | }; |
---|
68 | tmpl.encode = function (s) { |
---|
69 | return String(s || "").replace( |
---|
70 | tmpl.encReg, |
---|
71 | function (c) { |
---|
72 | return tmpl.encMap[c] || ""; |
---|
73 | } |
---|
74 | ); |
---|
75 | }; |
---|
76 | tmpl.arg = "o"; |
---|
77 | tmpl.helper = ",print=function(s,e){_s+=e&&(s||'')||_e(s);}" + |
---|
78 | ",include=function(s,d){_s+=tmpl(s,d);}"; |
---|
79 | if (typeof define === "function" && define.amd) { |
---|
80 | define(function () { |
---|
81 | return tmpl; |
---|
82 | }); |
---|
83 | } else { |
---|
84 | $.tmpl = tmpl; |
---|
85 | } |
---|
86 | }(this)); |
---|