1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
;(function ($) { var escapes = { "'": "'", '\\': '\\', '\r': 'r', '\n': 'n', '\u2028': 'u2028', '\u2029': 'u2029' }, escapeMap = { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''', '`': '`' }; var escapeChar = function(match) { return '\\' + escapes[match]; }; var createEscaper = function(map) { var escaper = function(match) { return map[match]; }; var source = "(?:&|<|>|\"|'|`)"; var testRegexp = RegExp(source); var replaceRegexp = RegExp(source, 'g'); return function(string) { string = string == null ? '' : '' + string; return testRegexp.test(string) ? string.replace(replaceRegexp, escaper) : string; }; }; var escape = createEscaper(escapeMap); $.extend({ templateSettings: { escape: /{{-([\s\S]+?)}}/g, interpolate : /{{=([\s\S]+?)}}/g, evaluate: /{{([\s\S]+?)}}/g }, escapeHtml: escape, template: function (text, settings) { var options = $.extend(true, {}, this.templateSettings, settings); var matcher = RegExp([options.escape.source, options.interpolate.source, options.evaluate.source].join('|') + '|$', 'g'); var index = 0; var source = "__p+='"; text.replace(matcher, function(match, escape, interpolate, evaluate, offset) { source += text.slice(index, offset).replace(/\\|'|\r|\n|\u2028|\u2029/g, escapeChar); index = offset + match.length; if (escape) { source += "'+\n((__t=(" + escape + "))==null?'':$.escapeHtml(__t))+\n'"; } else if (interpolate) { source += "'+\n((__t=(" + interpolate + "))==null?'':__t)+\n'"; } else if (evaluate) { source += "';\n" + evaluate + "\n__p+='"; } return match; }); source += "';\n"; if (!options.variable) source = 'with(obj||{}){\n' + source + '}\n'; source = "var __t,__p='',__j=Array.prototype.join," + "print=function(){__p+=__j.call(arguments,'');};\n" + source + 'return __p;\n'; try { var render = new Function(options.variable || 'obj', source); } catch (e) { e.source = source; throw e; } var template = function(data) { return render.call(this, data); }; var argument = options.variable || 'obj'; template.source = 'function(' + argument + '){\n' + source + '}'; return template; } }); })(jQuery);
|