//http://stanlemon.net/projects/jquery-templates.html
(function($) { $.template = function(a, b) { return new $.template.instance(a, b) }; $.template.instance = function(a, b) { if (b && b['regx']) b.regx = this.regx[b.regx]; this.options = $.extend({ compile: false, regx: this.regx.standard }, b || {}); this.html = a; if (this.options.compile) { this.compile() } this.isTemplate = true }; $.template.regx = $.template.instance.prototype.regx = { jsp: /\$\{([\w-]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?\}/g, ext: /\{([\w-]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?\}/g, jtemplates: /\{\{([\w-]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?\}\}/g }; $.template.regx.standard = $.template.regx.jsp; $.template.helpers = $.template.instance.prototype.helpers = { substr: function(a, b, c) { return String(a).substr(b, c) } }; $.extend($.template.instance.prototype, { apply: function(e) { if (this.options.compile) { return this.compiled(e) } else { var f = this; var g = this.helpers; var h = function(m, a, b, c) { if (b) { if (b.substr(0, 5) == "this.") { return f.call(b.substr(5), e[a], e) } else { if (c) { var d = /^\s*['"](.*)["']\s*$/; c = c.split(','); for (var i = 0, len = c.length; i < len; i++) { c[i] = c[i].replace(d, "$1") } c = [e[a]].concat(c) } else { c = [e[a]] } return g[b].apply(g, c) } } else { return e[a] !== undefined ? e[a] : "" } }; return this.html.replace(this.options.regx, h) } }, compile: function() { var d = $.browser.mozilla ? "+" : ","; var e = this.helpers; var f = function(m, a, b, c) { if (b) { c = c ? ',' + c : ""; if (b.substr(0, 5) != "this.") { b = "fm." + b + '(' } else { b = 'this.call("' + b.substr(5) + '", '; c = ", values" } } else { c = ''; b = "(values['" + a + "'] == undefined ? '' : " } return "'" + d + b + "values['" + a + "']" + c + ")" + d + "'" }; var g; if ($.browser.mozilla) { g = "this.compiled = function(values){ return '" + this.html.replace(/\\/g, '\\\\').replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.options.regx, f) + "';};" } else { g = ["this.compiled = function(values){ return ['"]; g.push(this.html.replace(/\\/g, '\\\\').replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.options.regx, f)); g.push("'].join('');};"); g = g.join('') } eval(g); return this } }); var j = { domManip: $.fn.domManip, text: $.fn.text, html: $.fn.html }; $.fn.domManip = function(a, b, c, d) { if (a[0].isTemplate) { a[0] = a[0].apply(a[1]); delete a[1] } var r = j.domManip.apply(this, arguments); return r }; $.fn.html = function(a, o) { if (a && a.isTemplate) var a = a.apply(o); var r = j.html.apply(this, [a]); return r }; $.fn.text = function(a, o) { if (a && a.isTemplate) var a = a.apply(o); var r = j.text.apply(this, [a]); return r } })(jQuery);
