2 * Ext JS Library 2.2.1
\r
3 * Copyright(c) 2006-2009, Ext JS, LLC.
\r
4 * licensing@extjs.com
\r
6 * http://extjs.com/license
\r
10 * @class Ext.XTemplate
\r
11 * @extends Ext.Template
\r
12 * <p>A template class that supports advanced functionality like autofilling arrays, conditional processing with
\r
13 * basic comparison operators, sub-templates, basic math function support, special built-in template variables,
\r
14 * inline code execution and more. XTemplate also provides the templating mechanism built into {@link Ext.DataView}.</p>
\r
15 * <p>XTemplate supports many special tags and built-in operators that aren't defined as part of the API, but are
\r
16 * supported in the templates that can be created. The following examples demonstrate all of the supported features.
\r
17 * This is the data object used for reference in each code example:</p>
\r
20 name: 'Jack Slocum',
\r
21 title: 'Lead Developer',
\r
22 company: 'Ext JS, LLC',
\r
23 email: 'jack@extjs.com',
\r
24 address: '4 Red Bulls Drive',
\r
28 drinks: ['Red Bull', 'Coffee', 'Water'],
\r
41 * <p><b>Auto filling of arrays and scope switching</b><br/>Using the <tt>tpl</tt> tag and the <tt>for</tt> operator,
\r
42 * you can switch to the scope of the object specified by <tt>for</tt> and access its members to populate the template.
\r
43 * If the variable in <tt>for</tt> is an array, it will auto-fill, repeating the template block inside the <tt>tpl</tt>
\r
44 * tag for each item in the array:</p>
\r
46 var tpl = new Ext.XTemplate(
\r
47 '<p>Name: {name}</p>',
\r
48 '<p>Title: {title}</p>',
\r
49 '<p>Company: {company}</p>',
\r
51 '<tpl for="kids">',
\r
52 '<p>{name}</p>',
\r
55 tpl.overwrite(panel.body, data);
\r
57 * <p><b>Access to parent object from within sub-template scope</b><br/>When processing a sub-template, for example while
\r
58 * looping through a child array, you can access the parent object's members via the <tt>parent</tt> object:</p>
\r
60 var tpl = new Ext.XTemplate(
\r
61 '<p>Name: {name}</p>',
\r
63 '<tpl for="kids">',
\r
64 '<tpl if="age &gt; 1">', // <-- Note that the > is encoded
\r
65 '<p>{name}</p>',
\r
66 '<p>Dad: {parent.name}</p>',
\r
70 tpl.overwrite(panel.body, data);
\r
72 * <p><b>Array item index and basic math support</b> <br/>While processing an array, the special variable <tt>{#}</tt>
\r
73 * will provide the current array index + 1 (starts at 1, not 0). Templates also support the basic math operators
\r
74 * + - * and / that can be applied directly on numeric data values:</p>
\r
76 var tpl = new Ext.XTemplate(
\r
77 '<p>Name: {name}</p>',
\r
79 '<tpl for="kids">',
\r
80 '<tpl if="age &gt; 1">', // <-- Note that the > is encoded
\r
81 '<p>{#}: {name}</p>', // <-- Auto-number each item
\r
82 '<p>In 5 Years: {age+5}</p>', // <-- Basic math
\r
83 '<p>Dad: {parent.name}</p>',
\r
87 tpl.overwrite(panel.body, data);
\r
89 * <p><b>Auto-rendering of flat arrays</b> <br/>Flat arrays that contain values (and not objects) can be auto-rendered
\r
90 * using the special <tt>{.}</tt> variable inside a loop. This variable will represent the value of
\r
91 * the array at the current index:</p>
\r
93 var tpl = new Ext.XTemplate(
\r
94 '<p>{name}\'s favorite beverages:</p>',
\r
95 '<tpl for="drinks">',
\r
96 '<div> - {.}</div>',
\r
99 tpl.overwrite(panel.body, data);
\r
101 * <p><b>Basic conditional logic</b> <br/>Using the <tt>tpl</tt> tag and the <tt>if</tt>
\r
102 * operator you can provide conditional checks for deciding whether or not to render specific parts of the template.
\r
103 * Note that there is no <tt>else</tt> operator — if needed, you should use two opposite <tt>if</tt> statements.
\r
104 * Properly-encoded attributes are required as seen in the following example:</p>
\r
106 var tpl = new Ext.XTemplate(
\r
107 '<p>Name: {name}</p>',
\r
109 '<tpl for="kids">',
\r
110 '<tpl if="age &gt; 1">', // <-- Note that the > is encoded
\r
111 '<p>{name}</p>',
\r
115 tpl.overwrite(panel.body, data);
\r
117 * <p><b>Ability to execute arbitrary inline code</b> <br/>In an XTemplate, anything between {[ ... ]} is considered
\r
118 * code to be executed in the scope of the template. There are some special variables available in that code:
\r
120 * <li><b><tt>values</tt></b>: The values in the current scope. If you are using scope changing sub-templates, you
\r
121 * can change what <tt>values</tt> is.</li>
\r
122 * <li><b><tt>parent</tt></b>: The scope (values) of the ancestor template.</li>
\r
123 * <li><b><tt>xindex</tt></b>: If you are in a looping template, the index of the loop you are in (1-based).</li>
\r
124 * <li><b><tt>xcount</tt></b>: If you are in a looping template, the total length of the array you are looping.</li>
\r
125 * <li><b><tt>fm</tt></b>: An alias for <tt>Ext.util.Format</tt>.</li>
\r
127 * This example demonstrates basic row striping using an inline code block and the <tt>xindex</tt> variable:</p>
\r
129 var tpl = new Ext.XTemplate(
\r
130 '<p>Name: {name}</p>',
\r
131 '<p>Company: {[values.company.toUpperCase() + ", " + values.title]}</p>',
\r
133 '<tpl for="kids">',
\r
134 '<div class="{[xindex % 2 === 0 ? "even" : "odd"]}">',
\r
139 tpl.overwrite(panel.body, data);
\r
141 * <p><b>Template member functions</b> <br/>One or more member functions can be defined directly on the config
\r
142 * object passed into the XTemplate constructor for more complex processing:</p>
\r
144 var tpl = new Ext.XTemplate(
\r
145 '<p>Name: {name}</p>',
\r
147 '<tpl for="kids">',
\r
148 '<tpl if="this.isGirl(name)">',
\r
149 '<p>Girl: {name} - {age}</p>',
\r
151 '<tpl if="this.isGirl(name) == false">',
\r
152 '<p>Boy: {name} - {age}</p>',
\r
154 '<tpl if="this.isBaby(age)">',
\r
155 '<p>{name} is a baby!</p>',
\r
157 '</tpl></p>', {
\r
158 isGirl: function(name){
\r
159 return name == 'Sara Grace';
\r
161 isBaby: function(age){
\r
165 tpl.overwrite(panel.body, data);
\r
168 * @param {String/Array/Object} parts The HTML fragment or an array of fragments to join(""), or multiple arguments
\r
169 * to join("") that can also include a config object
\r
171 Ext.XTemplate = function(){
\r
172 Ext.XTemplate.superclass.constructor.apply(this, arguments);
\r
175 s = ['<tpl>', s, '</tpl>'].join('');
\r
177 var re = /<tpl\b[^>]*>((?:(?=([^<]+))\2|<(?!tpl\b[^>]*>))*?)<\/tpl>/;
\r
179 var nameRe = /^<tpl\b[^>]*?for="(.*?)"/;
\r
180 var ifRe = /^<tpl\b[^>]*?if="(.*?)"/;
\r
181 var execRe = /^<tpl\b[^>]*?exec="(.*?)"/;
\r
185 while(m = s.match(re)){
\r
186 var m2 = m[0].match(nameRe);
\r
187 var m3 = m[0].match(ifRe);
\r
188 var m4 = m[0].match(execRe);
\r
189 var exp = null, fn = null, exec = null;
\r
190 var name = m2 && m2[1] ? m2[1] : '';
\r
192 exp = m3 && m3[1] ? m3[1] : null;
\r
194 fn = new Function('values', 'parent', 'xindex', 'xcount', 'with(values){ return '+(Ext.util.Format.htmlDecode(exp))+'; }');
\r
198 exp = m4 && m4[1] ? m4[1] : null;
\r
200 exec = new Function('values', 'parent', 'xindex', 'xcount', 'with(values){ '+(Ext.util.Format.htmlDecode(exp))+'; }');
\r
205 case '.': name = new Function('values', 'parent', 'with(values){ return values; }'); break;
\r
206 case '..': name = new Function('values', 'parent', 'with(values){ return parent; }'); break;
\r
207 default: name = new Function('values', 'parent', 'with(values){ return '+name+'; }');
\r
217 s = s.replace(m[0], '{xtpl'+ id + '}');
\r
220 for(var i = tpls.length-1; i >= 0; --i){
\r
221 this.compileTpl(tpls[i]);
\r
223 this.master = tpls[tpls.length-1];
\r
226 Ext.extend(Ext.XTemplate, Ext.Template, {
\r
228 re : /\{([\w-\.\#]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?(\s?[\+\-\*\\]\s?[\d\.\+\-\*\\\(\)]+)?\}/g,
\r
230 codeRe : /\{\[((?:\\\]|.|\n)*?)\]\}/g,
\r
233 applySubTemplate : function(id, values, parent, xindex, xcount){
\r
234 var t = this.tpls[id];
\r
235 if(t.test && !t.test.call(this, values, parent, xindex, xcount)){
\r
238 if(t.exec && t.exec.call(this, values, parent, xindex, xcount)){
\r
241 var vs = t.target ? t.target.call(this, values, parent) : values;
\r
242 parent = t.target ? values : parent;
\r
243 if(t.target && Ext.isArray(vs)){
\r
245 for(var i = 0, len = vs.length; i < len; i++){
\r
246 buf[buf.length] = t.compiled.call(this, vs[i], parent, i+1, len);
\r
248 return buf.join('');
\r
250 return t.compiled.call(this, vs, parent, xindex, xcount);
\r
254 compileTpl : function(tpl){
\r
255 var fm = Ext.util.Format;
\r
256 var useF = this.disableFormats !== true;
\r
257 var sep = Ext.isGecko ? "+" : ",";
\r
258 var fn = function(m, name, format, args, math){
\r
259 if(name.substr(0, 4) == 'xtpl'){
\r
260 return "'"+ sep +'this.applySubTemplate('+name.substr(4)+', values, parent, xindex, xcount)'+sep+"'";
\r
265 }else if(name === '#'){
\r
267 }else if(name.indexOf('.') != -1){
\r
270 v = "values['" + name + "']";
\r
273 v = '(' + v + math + ')';
\r
275 if(format && useF){
\r
276 args = args ? ',' + args : "";
\r
277 if(format.substr(0, 5) != "this."){
\r
278 format = "fm." + format + '(';
\r
280 format = 'this.call("'+ format.substr(5) + '", ';
\r
284 args= ''; format = "("+v+" === undefined ? '' : ";
\r
286 return "'"+ sep + format + v + args + ")"+sep+"'";
\r
288 var codeFn = function(m, code){
\r
289 return "'"+ sep +'('+code+')'+sep+"'";
\r
293 // branched to use + in gecko and [].join() in others
\r
295 body = "tpl.compiled = function(values, parent, xindex, xcount){ return '" +
\r
296 tpl.body.replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn).replace(this.codeRe, codeFn) +
\r
299 body = ["tpl.compiled = function(values, parent, xindex, xcount){ return ['"];
\r
300 body.push(tpl.body.replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn).replace(this.codeRe, codeFn));
\r
301 body.push("'].join('');};");
\r
302 body = body.join('');
\r
309 * Returns an HTML fragment of this template with the specified values applied.
\r
310 * @param {Object} values The template values. Can be an array if your params are numeric (i.e. {0}) or an object (i.e. {foo: 'bar'})
\r
311 * @return {String} The HTML fragment
\r
313 applyTemplate : function(values){
\r
314 return this.master.compiled.call(this, values, {}, 1, 1);
\r
318 * Compile the template to a function for optimized performance. Recommended if the template will be used frequently.
\r
319 * @return {Function} The compiled function
\r
321 compile : function(){return this;}
\r
328 * @property disableFormats
\r
338 * Alias for {@link #applyTemplate}
\r
339 * Returns an HTML fragment of this template with the specified values applied.
\r
340 * @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
341 * @return {String} The HTML fragment
\r
342 * @member Ext.XTemplate
\r
345 Ext.XTemplate.prototype.apply = Ext.XTemplate.prototype.applyTemplate;
\r
348 * Creates a template from the passed element's value (<i>display:none</i> textarea, preferred) or innerHTML.
\r
349 * @param {String/HTMLElement} el A DOM element or its id
\r
350 * @return {Ext.Template} The created template
\r
353 Ext.XTemplate.from = function(el){
\r
354 el = Ext.getDom(el);
\r
355 return new Ext.XTemplate(el.value || el.innerHTML);
\r