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,
45 * Returns an HTML fragment of this template with the specified values applied.
46 * @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'})
47 * @return {String} The HTML fragment
50 applyTemplate : function(values){
52 useF = me.disableFormats !== true,
57 return me.compiled(values);
59 function fn(m, name, format, args){
61 if (format.substr(0, 5) == "this.") {
62 return tpl.call(format.substr(5), values[name], values);
65 // quoted values are required for strings in compiled templates,
66 // but for non compiled we need to strip them
67 // quoted reversed for jsmin
68 var re = /^\s*['"](.*)["']\s*$/;
69 args = args.split(',');
70 for(var i = 0, len = args.length; i < len; i++){
71 args[i] = args[i].replace(re, "$1");
73 args = [values[name]].concat(args);
75 args = [values[name]];
77 return fm[format].apply(fm, args);
80 return values[name] !== undefined ? values[name] : "";
83 return me.html.replace(me.re, fn);
87 * Compiles the template into an internal function, eliminating the RegEx overhead.
88 * @return {Ext.Template} this
94 useF = me.disableFormats !== true,
95 sep = Ext.isGecko ? "+" : ",",
98 function fn(m, name, format, args){
100 args = args ? ',' + args : "";
101 if(format.substr(0, 5) != "this."){
102 format = "fm." + format + '(';
104 format = 'this.call("'+ format.substr(5) + '", ';
108 args= ''; format = "(values['" + name + "'] == undefined ? '' : ";
110 return "'"+ sep + format + "values['" + name + "']" + args + ")"+sep+"'";
113 // branched to use + in gecko and [].join() in others
115 body = "this.compiled = function(values){ return '" +
116 me.html.replace(/\\/g, '\\\\').replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn) +
119 body = ["this.compiled = function(values){ return ['"];
120 body.push(me.html.replace(/\\/g, '\\\\').replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn));
121 body.push("'].join('');};");
122 body = body.join('');
128 // private function used to call members
129 call : function(fnName, value, allValues){
130 return this[fnName](value, allValues);
133 Ext.Template.prototype.apply = Ext.Template.prototype.applyTemplate;