3 * Copyright(c) 2006-2010 Ext JS, LLC
5 * http://www.extjs.com/license
8 * @class Ext.Template
\r
10 Ext.apply(Ext.Template.prototype, {
\r
12 * @cfg {Boolean} disableFormats Specify <tt>true</tt> to disable format
\r
13 * functions in the template. If the template does not contain
\r
14 * {@link Ext.util.Format format functions}, setting <code>disableFormats</code>
\r
15 * to true will reduce <code>{@link #apply}</code> time. Defaults to <tt>false</tt>.
\r
17 var t = new Ext.Template(
\r
18 '<div name="{id}">',
\r
19 '<span class="{cls}">{name} {value}</span>',
\r
22 compiled: true, // {@link #compile} immediately
\r
23 disableFormats: true // reduce <code>{@link #apply}</code> time since no formatting
\r
27 * For a list of available format functions, see {@link Ext.util.Format}.
\r
29 disableFormats : false,
\r
31 * See <code>{@link #disableFormats}</code>.
\r
33 * @property disableFormats
\r
37 * The regular expression used to match template variables
\r
42 re : /\{([\w-]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?\}/g,
\r
45 * Returns an HTML fragment of this template with the specified values applied.
\r
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'})
\r
47 * @return {String} The HTML fragment
\r
50 applyTemplate : function(values){
\r
52 useF = me.disableFormats !== true,
\r
53 fm = Ext.util.Format,
\r
57 return me.compiled(values);
\r
59 function fn(m, name, format, args){
\r
60 if (format && useF) {
\r
61 if (format.substr(0, 5) == "this.") {
\r
62 return tpl.call(format.substr(5), values[name], values);
\r
65 // quoted values are required for strings in compiled templates,
\r
66 // but for non compiled we need to strip them
\r
67 // quoted reversed for jsmin
\r
68 var re = /^\s*['"](.*)["']\s*$/;
\r
69 args = args.split(',');
\r
70 for(var i = 0, len = args.length; i < len; i++){
\r
71 args[i] = args[i].replace(re, "$1");
\r
73 args = [values[name]].concat(args);
\r
75 args = [values[name]];
\r
77 return fm[format].apply(fm, args);
\r
80 return values[name] !== undefined ? values[name] : "";
\r
83 return me.html.replace(me.re, fn);
\r
87 * Compiles the template into an internal function, eliminating the RegEx overhead.
\r
88 * @return {Ext.Template} this
\r
91 compile : function(){
\r
93 fm = Ext.util.Format,
\r
94 useF = me.disableFormats !== true,
\r
95 sep = Ext.isGecko ? "+" : ",",
\r
98 function fn(m, name, format, args){
\r
100 args = args ? ',' + args : "";
\r
101 if(format.substr(0, 5) != "this."){
\r
102 format = "fm." + format + '(';
\r
104 format = 'this.call("'+ format.substr(5) + '", ';
\r
108 args= ''; format = "(values['" + name + "'] == undefined ? '' : ";
\r
110 return "'"+ sep + format + "values['" + name + "']" + args + ")"+sep+"'";
\r
113 // branched to use + in gecko and [].join() in others
\r
115 body = "this.compiled = function(values){ return '" +
\r
116 me.html.replace(/\\/g, '\\\\').replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn) +
\r
119 body = ["this.compiled = function(values){ return ['"];
\r
120 body.push(me.html.replace(/\\/g, '\\\\').replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn));
\r
121 body.push("'].join('');};");
\r
122 body = body.join('');
\r
128 // private function used to call members
\r
129 call : function(fnName, value, allValues){
\r
130 return this[fnName](value, allValues);
\r
133 Ext.Template.prototype.apply = Ext.Template.prototype.applyTemplate;