3 * Copyright(c) 2006-2010 Ext JS, Inc.
5 * http://www.extjs.com/license
10 Ext.apply(Ext.Template.prototype, {
12 * @cfg {Boolean} disableFormats Specify <tt>true</tt> to disable format
13 * functions in the template. If the template does not contain
14 * {@link Ext.util.Format format functions}, setting <code>disableFormats</code>
15 * to true will reduce <code>{@link #apply}</code> time. Defaults to <tt>false</tt>.
17 var t = new Ext.Template(
18 '<div name="{id}">',
19 '<span class="{cls}">{name} {value}</span>',
22 compiled: true, // {@link #compile} immediately
23 disableFormats: true // reduce <code>{@link #apply}</code> time since no formatting
27 * For a list of available format functions, see {@link Ext.util.Format}.
29 disableFormats : false,
31 * See <code>{@link #disableFormats}</code>.
33 * @property disableFormats
37 * The regular expression used to match template variables
42 re : /\{([\w-]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?\}/g,
43 argsRe : /^\s*['"](.*)["']\s*$/,
45 compileBRe : /(\r\n|\n)/g,
49 * Returns an HTML fragment of this template with the specified values applied.
50 * @param {Object/Array} values The template values. Can be an array if your params are numeric (i.e. {0}) or an object (i.e. {foo: 'bar'})
51 * @return {String} The HTML fragment
54 applyTemplate : function(values){
56 useF = me.disableFormats !== true,
61 return me.compiled(values);
63 function fn(m, name, format, args){
65 if (format.substr(0, 5) == "this.") {
66 return tpl.call(format.substr(5), values[name], values);
69 // quoted values are required for strings in compiled templates,
70 // but for non compiled we need to strip them
71 // quoted reversed for jsmin
73 args = args.split(',');
74 for(var i = 0, len = args.length; i < len; i++){
75 args[i] = args[i].replace(re, "$1");
77 args = [values[name]].concat(args);
79 args = [values[name]];
81 return fm[format].apply(fm, args);
84 return values[name] !== undefined ? values[name] : "";
87 return me.html.replace(me.re, fn);
91 * Compiles the template into an internal function, eliminating the RegEx overhead.
92 * @return {Ext.Template} this
98 useF = me.disableFormats !== true,
99 sep = Ext.isGecko ? "+" : ",",
102 function fn(m, name, format, args){
104 args = args ? ',' + args : "";
105 if(format.substr(0, 5) != "this."){
106 format = "fm." + format + '(';
108 format = 'this.call("'+ format.substr(5) + '", ';
112 args= ''; format = "(values['" + name + "'] == undefined ? '' : ";
114 return "'"+ sep + format + "values['" + name + "']" + args + ")"+sep+"'";
117 // branched to use + in gecko and [].join() in others
119 body = "this.compiled = function(values){ return '" +
120 me.html.replace(me.compileARe, '\\\\').replace(me.compileBRe, '\\n').replace(me.compileCRe, "\\'").replace(me.re, fn) +
123 body = ["this.compiled = function(values){ return ['"];
124 body.push(me.html.replace(me.compileARe, '\\\\').replace(me.compileBRe, '\\n').replace(me.compileCRe, "\\'").replace(me.re, fn));
125 body.push("'].join('');};");
126 body = body.join('');
132 // private function used to call members
133 call : function(fnName, value, allValues){
134 return this[fnName](value, allValues);
137 Ext.Template.prototype.apply = Ext.Template.prototype.applyTemplate;