3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
4 <title>The source code</title>
5 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
6 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
8 <body onload="prettyPrint();">
9 <pre class="prettyprint lang-js">/*!
10 * Ext JS Library 3.2.1
11 * Copyright(c) 2006-2010 Ext JS, Inc.
13 * http://www.extjs.com/license
15 <div id="cls-Ext.XTemplate"></div>/**
16 * @class Ext.XTemplate
17 * @extends Ext.Template
18 * <p>A template class that supports advanced functionality like:<div class="mdetail-params"><ul>
19 * <li>Autofilling arrays using templates and sub-templates</li>
20 * <li>Conditional processing with basic comparison operators</li>
21 * <li>Basic math function support</li>
22 * <li>Execute arbitrary inline code with special built-in template variables</li>
23 * <li>Custom member functions</li>
24 * <li>Many special tags and built-in operators that aren't defined as part of
25 * the API, but are supported in the templates that can be created</li>
27 * <p>XTemplate provides the templating mechanism built into:<div class="mdetail-params"><ul>
28 * <li>{@link Ext.DataView}</li>
29 * <li>{@link Ext.ListView}</li>
30 * <li>{@link Ext.form.ComboBox}</li>
31 * <li>{@link Ext.grid.TemplateColumn}</li>
32 * <li>{@link Ext.grid.GroupingView}</li>
33 * <li>{@link Ext.menu.Item}</li>
34 * <li>{@link Ext.layout.MenuLayout}</li>
35 * <li>{@link Ext.ColorPalette}</li>
38 * <p>For example usage {@link #XTemplate see the constructor}.</p>
41 * The {@link Ext.Template#Template Ext.Template constructor} describes
42 * the acceptable parameters to pass to the constructor. The following
43 * examples demonstrate all of the supported features.</p>
45 * <div class="mdetail-params"><ul>
47 * <li><b><u>Sample Data</u></b>
48 * <div class="sub-desc">
49 * <p>This is the data object used for reference in each code example:</p>
53 title: 'Lead Developer',
54 company: 'Ext JS, LLC',
55 email: 'jack@extjs.com',
56 address: '4 Red Bulls Drive',
60 drinks: ['Red Bull', 'Coffee', 'Water'],
77 * <li><b><u>Auto filling of arrays</u></b>
78 * <div class="sub-desc">
79 * <p>The <b><tt>tpl</tt></b> tag and the <b><tt>for</tt></b> operator are used
80 * to process the provided data object:
82 * <li>If the value specified in <tt>for</tt> is an array, it will auto-fill,
83 * repeating the template block inside the <tt>tpl</tt> tag for each item in the
85 * <li>If <tt>for="."</tt> is specified, the data object provided is examined.</li>
86 * <li>While processing an array, the special variable <tt>{#}</tt>
87 * will provide the current array index + 1 (starts at 1, not 0).</li>
91 <tpl <b>for</b>=".">...</tpl> // loop through array at root node
92 <tpl <b>for</b>="foo">...</tpl> // loop through array at foo node
93 <tpl <b>for</b>="foo.bar">...</tpl> // loop through array at foo.bar node
95 * Using the sample data above:
97 var tpl = new Ext.XTemplate(
99 '<tpl <b>for</b>=".">', // process the data.kids node
100 '<p>{#}. {name}</p>', // use current array index to autonumber
103 tpl.overwrite(panel.body, data.kids); // pass the kids property of the data object
105 * <p>An example illustrating how the <b><tt>for</tt></b> property can be leveraged
106 * to access specified members of the provided data object to populate the template:</p>
108 var tpl = new Ext.XTemplate(
109 '<p>Name: {name}</p>',
110 '<p>Title: {title}</p>',
111 '<p>Company: {company}</p>',
113 '<tpl <b>for="kids"</b>>', // interrogate the kids property within the data
114 '<p>{name}</p>',
117 tpl.overwrite(panel.body, data); // pass the root node of the data object
119 * <p>Flat arrays that contain values (and not objects) can be auto-rendered
120 * using the special <b><tt>{.}</tt></b> variable inside a loop. This variable
121 * will represent the value of the array at the current index:</p>
123 var tpl = new Ext.XTemplate(
124 '<p>{name}\'s favorite beverages:</p>',
125 '<tpl for="drinks">',
126 '<div> - {.}</div>',
129 tpl.overwrite(panel.body, data);
131 * <p>When processing a sub-template, for example while looping through a child array,
132 * you can access the parent object's members via the <b><tt>parent</tt></b> object:</p>
134 var tpl = new Ext.XTemplate(
135 '<p>Name: {name}</p>',
137 '<tpl for="kids">',
138 '<tpl if="age > 1">',
139 '<p>{name}</p>',
140 '<p>Dad: {<b>parent</b>.name}</p>',
144 tpl.overwrite(panel.body, data);
150 * <li><b><u>Conditional processing with basic comparison operators</u></b>
151 * <div class="sub-desc">
152 * <p>The <b><tt>tpl</tt></b> tag and the <b><tt>if</tt></b> operator are used
153 * to provide conditional checks for deciding whether or not to render specific
154 * parts of the template. Notes:<div class="sub-desc"><ul>
155 * <li>Double quotes must be encoded if used within the conditional</li>
156 * <li>There is no <tt>else</tt> operator — if needed, two opposite
157 * <tt>if</tt> statements should be used.</li>
160 <tpl if="age > 1 && age < 10">Child</tpl>
161 <tpl if="age >= 10 && age < 18">Teenager</tpl>
162 <tpl <b>if</b>="this.isGirl(name)">...</tpl>
163 <tpl <b>if</b>="id==\'download\'">...</tpl>
164 <tpl <b>if</b>="needsIcon"><img src="{icon}" class="{iconCls}"/></tpl>
166 <tpl if="name == "Jack"">Hello</tpl>
167 // encode " if it is part of the condition, e.g.
168 <tpl if="name == &quot;Jack&quot;">Hello</tpl>
170 * Using the sample data above:
172 var tpl = new Ext.XTemplate(
173 '<p>Name: {name}</p>',
175 '<tpl for="kids">',
176 '<tpl if="age > 1">',
177 '<p>{name}</p>',
181 tpl.overwrite(panel.body, data);
187 * <li><b><u>Basic math support</u></b>
188 * <div class="sub-desc">
189 * <p>The following basic math operators may be applied directly on numeric
190 * data values:</p><pre>
195 var tpl = new Ext.XTemplate(
196 '<p>Name: {name}</p>',
198 '<tpl for="kids">',
199 '<tpl if="age &gt; 1">', // <-- Note that the > is encoded
200 '<p>{#}: {name}</p>', // <-- Auto-number each item
201 '<p>In 5 Years: {age+5}</p>', // <-- Basic math
202 '<p>Dad: {parent.name}</p>',
206 tpl.overwrite(panel.body, data);
212 * <li><b><u>Execute arbitrary inline code with special built-in template variables</u></b>
213 * <div class="sub-desc">
214 * <p>Anything between <code>{[ ... ]}</code> is considered code to be executed
215 * in the scope of the template. There are some special variables available in that code:
217 * <li><b><tt>values</tt></b>: The values in the current scope. If you are using
218 * scope changing sub-templates, you can change what <tt>values</tt> is.</li>
219 * <li><b><tt>parent</tt></b>: The scope (values) of the ancestor template.</li>
220 * <li><b><tt>xindex</tt></b>: If you are in a looping template, the index of the
221 * loop you are in (1-based).</li>
222 * <li><b><tt>xcount</tt></b>: If you are in a looping template, the total length
223 * of the array you are looping.</li>
224 * <li><b><tt>fm</tt></b>: An alias for <tt>Ext.util.Format</tt>.</li>
226 * This example demonstrates basic row striping using an inline code block and the
227 * <tt>xindex</tt> variable:</p>
229 var tpl = new Ext.XTemplate(
230 '<p>Name: {name}</p>',
231 '<p>Company: {[values.company.toUpperCase() + ", " + values.title]}</p>',
233 '<tpl for="kids">',
234 '<div class="{[xindex % 2 === 0 ? "even" : "odd"]}">',
239 tpl.overwrite(panel.body, data);
244 * <li><b><u>Template member functions</u></b>
245 * <div class="sub-desc">
246 * <p>One or more member functions can be specified in a configuration
247 * object passed into the XTemplate constructor for more complex processing:</p>
249 var tpl = new Ext.XTemplate(
250 '<p>Name: {name}</p>',
252 '<tpl for="kids">',
253 '<tpl if="this.isGirl(name)">',
254 '<p>Girl: {name} - {age}</p>',
256 // use opposite if statement to simulate 'else' processing:
257 '<tpl if="this.isGirl(name) == false">',
258 '<p>Boy: {name} - {age}</p>',
260 '<tpl if="this.isBaby(age)">',
261 '<p>{name} is a baby!</p>',
265 // XTemplate configuration:
267 disableFormats: true,
269 isGirl: function(name){
270 return name == 'Sara Grace';
272 isBaby: function(age){
277 tpl.overwrite(panel.body, data);
284 * @param {Mixed} config
286 Ext.XTemplate = function(){
287 Ext.XTemplate.superclass.constructor.apply(this, arguments);
291 re = /<tpl\b[^>]*>((?:(?=([^<]+))\2|<(?!tpl\b[^>]*>))*?)<\/tpl>/,
292 nameRe = /^<tpl\b[^>]*?for="(.*?)"/,
293 ifRe = /^<tpl\b[^>]*?if="(.*?)"/,
294 execRe = /^<tpl\b[^>]*?exec="(.*?)"/,
303 WITHVALUES = 'with(values){ ';
305 s = ['<tpl>', s, '</tpl>'].join('');
307 while((m = s.match(re))){
308 var m2 = m[0].match(nameRe),
309 m3 = m[0].match(ifRe),
310 m4 = m[0].match(execRe),
314 name = m2 && m2[1] ? m2[1] : '';
317 exp = m3 && m3[1] ? m3[1] : null;
319 fn = new Function(VALUES, PARENT, XINDEX, XCOUNT, WITHVALUES + RETURN +(Ext.util.Format.htmlDecode(exp))+'; }');
323 exp = m4 && m4[1] ? m4[1] : null;
325 exec = new Function(VALUES, PARENT, XINDEX, XCOUNT, WITHVALUES +(Ext.util.Format.htmlDecode(exp))+'; }');
330 case '.': name = new Function(VALUES, PARENT, WITHVALUES + RETURN + VALUES + '; }'); break;
331 case '..': name = new Function(VALUES, PARENT, WITHVALUES + RETURN + PARENT + '; }'); break;
332 default: name = new Function(VALUES, PARENT, WITHVALUES + RETURN + name + '; }');
342 s = s.replace(m[0], '{xtpl'+ id + '}');
345 for(var i = tpls.length-1; i >= 0; --i){
346 me.compileTpl(tpls[i]);
348 me.master = tpls[tpls.length-1];
351 Ext.extend(Ext.XTemplate, Ext.Template, {
353 re : /\{([\w-\.\#]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?(\s?[\+\-\*\\]\s?[\d\.\+\-\*\\\(\)]+)?\}/g,
355 codeRe : /\{\[((?:\\\]|.|\n)*?)\]\}/g,
358 applySubTemplate : function(id, values, parent, xindex, xcount){
364 if ((t.test && !t.test.call(me, values, parent, xindex, xcount)) ||
365 (t.exec && t.exec.call(me, values, parent, xindex, xcount))) {
368 vs = t.target ? t.target.call(me, values, parent) : values;
370 parent = t.target ? values : parent;
371 if(t.target && Ext.isArray(vs)){
372 for(var i = 0, len = vs.length; i < len; i++){
373 buf[buf.length] = t.compiled.call(me, vs[i], parent, i+1, len);
377 return t.compiled.call(me, vs, parent, xindex, xcount);
381 compileTpl : function(tpl){
382 var fm = Ext.util.Format,
383 useF = this.disableFormats !== true,
384 sep = Ext.isGecko ? "+" : ",",
387 function fn(m, name, format, args, math){
388 if(name.substr(0, 4) == 'xtpl'){
389 return "'"+ sep +'this.applySubTemplate('+name.substr(4)+', values, parent, xindex, xcount)'+sep+"'";
394 }else if(name === '#'){
396 }else if(name.indexOf('.') != -1){
399 v = "values['" + name + "']";
402 v = '(' + v + math + ')';
404 if (format && useF) {
405 args = args ? ',' + args : "";
406 if(format.substr(0, 5) != "this."){
407 format = "fm." + format + '(';
409 format = 'this.call("'+ format.substr(5) + '", ';
413 args= ''; format = "("+v+" === undefined ? '' : ";
415 return "'"+ sep + format + v + args + ")"+sep+"'";
418 function codeFn(m, code){
419 // Single quotes get escaped when the template is compiled, however we want to undo this when running code.
420 return "'" + sep + '(' + code.replace(/\\'/g, "'") + ')' + sep + "'";
423 // branched to use + in gecko and [].join() in others
425 body = "tpl.compiled = function(values, parent, xindex, xcount){ return '" +
426 tpl.body.replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn).replace(this.codeRe, codeFn) +
429 body = ["tpl.compiled = function(values, parent, xindex, xcount){ return ['"];
430 body.push(tpl.body.replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn).replace(this.codeRe, codeFn));
431 body.push("'].join('');};");
432 body = body.join('');
438 <div id="method-Ext.XTemplate-applyTemplate"></div>/**
439 * Returns an HTML fragment of this template with the specified values applied.
440 * @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'})
441 * @return {String} The HTML fragment
443 applyTemplate : function(values){
444 return this.master.compiled.call(this, values, {}, 1, 1);
447 <div id="method-Ext.XTemplate-compile"></div>/**
448 * Compile the template to a function for optimized performance. Recommended if the template will be used frequently.
449 * @return {Function} The compiled function
451 compile : function(){return this;}
453 <div id="prop-Ext.XTemplate-re"></div>/**
457 <div id="prop-Ext.XTemplate-disableFormats"></div>/**
458 * @property disableFormats
461 <div id="method-Ext.XTemplate-set"></div>/**
467 <div id="method-Ext.XTemplate-apply"></div>/**
468 * Alias for {@link #applyTemplate}
469 * Returns an HTML fragment of this template with the specified values applied.
470 * @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'})
471 * @return {String} The HTML fragment
472 * @member Ext.XTemplate
475 Ext.XTemplate.prototype.apply = Ext.XTemplate.prototype.applyTemplate;
477 <div id="method-Ext.XTemplate-XTemplate.from"></div>/**
478 * Creates a template from the passed element's value (<i>display:none</i> textarea, preferred) or innerHTML.
479 * @param {String/HTMLElement} el A DOM element or its id
480 * @return {Ext.Template} The created template
483 Ext.XTemplate.from = function(el){
485 return new Ext.XTemplate(el.value || el.innerHTML);