3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
\r
4 <title>The source code</title>
\r
5 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
6 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
8 <body onload="prettyPrint();">
\r
9 <pre class="prettyprint lang-js"><div id="cls-Ext.XTemplate"></div>/**
10 * @class Ext.XTemplate
11 * @extends Ext.Template
12 * <p>A template class that supports advanced functionality like:<div class="mdetail-params"><ul>
13 * <li>Autofilling arrays using templates and sub-templates</li>
14 * <li>Conditional processing with basic comparison operators</li>
15 * <li>Basic math function support</li>
16 * <li>Execute arbitrary inline code with special built-in template variables</li>
17 * <li>Custom member functions</li>
18 * <li>Many special tags and built-in operators that aren't defined as part of
19 * the API, but are supported in the templates that can be created</li>
21 * <p>XTemplate provides the templating mechanism built into:<div class="mdetail-params"><ul>
22 * <li>{@link Ext.DataView}</li>
23 * <li>{@link Ext.ListView}</li>
24 * <li>{@link Ext.form.ComboBox}</li>
25 * <li>{@link Ext.grid.TemplateColumn}</li>
26 * <li>{@link Ext.grid.GroupingView}</li>
27 * <li>{@link Ext.menu.Item}</li>
28 * <li>{@link Ext.layout.MenuLayout}</li>
29 * <li>{@link Ext.ColorPalette}</li>
32 * <p>For example usage {@link #XTemplate see the constructor}.</p>
35 * The {@link Ext.Template#Template Ext.Template constructor} describes
36 * the acceptable parameters to pass to the constructor. The following
37 * examples demonstrate all of the supported features.</p>
39 * <div class="mdetail-params"><ul>
41 * <li><b><u>Sample Data</u></b>
42 * <div class="sub-desc">
43 * <p>This is the data object used for reference in each code example:</p>
47 title: 'Lead Developer',
48 company: 'Ext JS, LLC',
49 email: 'jack@extjs.com',
50 address: '4 Red Bulls Drive',
54 drinks: ['Red Bull', 'Coffee', 'Water'],
71 * <li><b><u>Auto filling of arrays</u></b>
72 * <div class="sub-desc">
73 * <p>The <b><tt>tpl</tt></b> tag and the <b><tt>for</tt></b> operator are used
74 * to process the provided data object:
76 * <li>If the value specified in <tt>for</tt> is an array, it will auto-fill,
77 * repeating the template block inside the <tt>tpl</tt> tag for each item in the
79 * <li>If <tt>for="."</tt> is specified, the data object provided is examined.</li>
80 * <li>While processing an array, the special variable <tt>{#}</tt>
81 * will provide the current array index + 1 (starts at 1, not 0).</li>
85 <tpl <b>for</b>=".">...</tpl> // loop through array at root node
86 <tpl <b>for</b>="foo">...</tpl> // loop through array at foo node
87 <tpl <b>for</b>="foo.bar">...</tpl> // loop through array at foo.bar node
89 * Using the sample data above:
91 var tpl = new Ext.XTemplate(
93 '<tpl <b>for</b>=".">', // process the data.kids node
94 '<p>{#}. {name}</p>', // use current array index to autonumber
97 tpl.overwrite(panel.body, data.kids); // pass the kids property of the data object
99 * <p>An example illustrating how the <b><tt>for</tt></b> property can be leveraged
100 * to access specified members of the provided data object to populate the template:</p>
102 var tpl = new Ext.XTemplate(
103 '<p>Name: {name}</p>',
104 '<p>Title: {title}</p>',
105 '<p>Company: {company}</p>',
107 '<tpl <b>for="kids"</b>>', // interrogate the kids property within the data
108 '<p>{name}</p>',
111 tpl.overwrite(panel.body, data); // pass the root node of the data object
113 * <p>Flat arrays that contain values (and not objects) can be auto-rendered
114 * using the special <b><tt>{.}</tt></b> variable inside a loop. This variable
115 * will represent the value of the array at the current index:</p>
117 var tpl = new Ext.XTemplate(
118 '<p>{name}\'s favorite beverages:</p>',
119 '<tpl for="drinks">',
120 '<div> - {.}</div>',
123 tpl.overwrite(panel.body, data);
125 * <p>When processing a sub-template, for example while looping through a child array,
126 * you can access the parent object's members via the <b><tt>parent</tt></b> object:</p>
128 var tpl = new Ext.XTemplate(
129 '<p>Name: {name}</p>',
131 '<tpl for="kids">',
132 '<tpl if="age > 1">',
133 '<p>{name}</p>',
134 '<p>Dad: {<b>parent</b>.name}</p>',
138 tpl.overwrite(panel.body, data);
144 * <li><b><u>Conditional processing with basic comparison operators</u></b>
145 * <div class="sub-desc">
146 * <p>The <b><tt>tpl</tt></b> tag and the <b><tt>if</tt></b> operator are used
147 * to provide conditional checks for deciding whether or not to render specific
148 * parts of the template. Notes:<div class="sub-desc"><ul>
149 * <li>Double quotes must be encoded if used within the conditional</li>
150 * <li>There is no <tt>else</tt> operator — if needed, two opposite
151 * <tt>if</tt> statements should be used.</li>
154 <tpl if="age > 1 && age < 10">Child</tpl>
155 <tpl if="age >= 10 && age < 18">Teenager</tpl>
156 <tpl <b>if</b>="this.isGirl(name)">...</tpl>
157 <tpl <b>if</b>="id==\'download\'">...</tpl>
158 <tpl <b>if</b>="needsIcon"><img src="{icon}" class="{iconCls}"/></tpl>
160 <tpl if="name == "Jack"">Hello</tpl>
161 // encode " if it is part of the condition, e.g.
162 <tpl if="name == &quot;Jack&quot;">Hello</tpl>
164 * Using the sample data above:
166 var tpl = new Ext.XTemplate(
167 '<p>Name: {name}</p>',
169 '<tpl for="kids">',
170 '<tpl if="age > 1">',
171 '<p>{name}</p>',
175 tpl.overwrite(panel.body, data);
181 * <li><b><u>Basic math support</u></b>
182 * <div class="sub-desc">
183 * <p>The following basic math operators may be applied directly on numeric
184 * data values:</p><pre>
189 var tpl = new Ext.XTemplate(
190 '<p>Name: {name}</p>',
192 '<tpl for="kids">',
193 '<tpl if="age &gt; 1">', // <-- Note that the > is encoded
194 '<p>{#}: {name}</p>', // <-- Auto-number each item
195 '<p>In 5 Years: {age+5}</p>', // <-- Basic math
196 '<p>Dad: {parent.name}</p>',
200 tpl.overwrite(panel.body, data);
206 * <li><b><u>Execute arbitrary inline code with special built-in template variables</u></b>
207 * <div class="sub-desc">
208 * <p>Anything between <code>{[ ... ]}</code> is considered code to be executed
209 * in the scope of the template. There are some special variables available in that code:
211 * <li><b><tt>values</tt></b>: The values in the current scope. If you are using
212 * scope changing sub-templates, you can change what <tt>values</tt> is.</li>
213 * <li><b><tt>parent</tt></b>: The scope (values) of the ancestor template.</li>
214 * <li><b><tt>xindex</tt></b>: If you are in a looping template, the index of the
215 * loop you are in (1-based).</li>
216 * <li><b><tt>xcount</tt></b>: If you are in a looping template, the total length
217 * of the array you are looping.</li>
218 * <li><b><tt>fm</tt></b>: An alias for <tt>Ext.util.Format</tt>.</li>
220 * This example demonstrates basic row striping using an inline code block and the
221 * <tt>xindex</tt> variable:</p>
223 var tpl = new Ext.XTemplate(
224 '<p>Name: {name}</p>',
225 '<p>Company: {[values.company.toUpperCase() + ", " + values.title]}</p>',
227 '<tpl for="kids">',
228 '<div class="{[xindex % 2 === 0 ? "even" : "odd"]}">',
233 tpl.overwrite(panel.body, data);
238 * <li><b><u>Template member functions</u></b>
239 * <div class="sub-desc">
240 * <p>One or more member functions can be specified in a configuration
241 * object passed into the XTemplate constructor for more complex processing:</p>
243 var tpl = new Ext.XTemplate(
244 '<p>Name: {name}</p>',
246 '<tpl for="kids">',
247 '<tpl if="this.isGirl(name)">',
248 '<p>Girl: {name} - {age}</p>',
250 // use opposite if statement to simulate 'else' processing:
251 '<tpl if="this.isGirl(name) == false">',
252 '<p>Boy: {name} - {age}</p>',
254 '<tpl if="this.isBaby(age)">',
255 '<p>{name} is a baby!</p>',
259 // XTemplate configuration:
261 disableFormats: true,
263 isGirl: function(name){
264 return name == 'Sara Grace';
266 isBaby: function(age){
271 tpl.overwrite(panel.body, data);
278 * @param {Mixed} config
280 Ext.XTemplate = function(){
281 Ext.XTemplate.superclass.constructor.apply(this, arguments);
285 re = /<tpl\b[^>]*>((?:(?=([^<]+))\2|<(?!tpl\b[^>]*>))*?)<\/tpl>/,
286 nameRe = /^<tpl\b[^>]*?for="(.*?)"/,
287 ifRe = /^<tpl\b[^>]*?if="(.*?)"/,
288 execRe = /^<tpl\b[^>]*?exec="(.*?)"/,
297 WITHVALUES = 'with(values){ ';
299 s = ['<tpl>', s, '</tpl>'].join('');
301 while((m = s.match(re))){
302 var m2 = m[0].match(nameRe),
303 m3 = m[0].match(ifRe),
304 m4 = m[0].match(execRe),
308 name = m2 && m2[1] ? m2[1] : '';
311 exp = m3 && m3[1] ? m3[1] : null;
313 fn = new Function(VALUES, PARENT, XINDEX, XCOUNT, WITHVALUES + RETURN +(Ext.util.Format.htmlDecode(exp))+'; }');
317 exp = m4 && m4[1] ? m4[1] : null;
319 exec = new Function(VALUES, PARENT, XINDEX, XCOUNT, WITHVALUES +(Ext.util.Format.htmlDecode(exp))+'; }');
324 case '.': name = new Function(VALUES, PARENT, WITHVALUES + RETURN + VALUES + '; }'); break;
325 case '..': name = new Function(VALUES, PARENT, WITHVALUES + RETURN + PARENT + '; }'); break;
326 default: name = new Function(VALUES, PARENT, WITHVALUES + RETURN + name + '; }');
336 s = s.replace(m[0], '{xtpl'+ id + '}');
339 Ext.each(tpls, function(t) {
342 me.master = tpls[tpls.length-1];
345 Ext.extend(Ext.XTemplate, Ext.Template, {
347 re : /\{([\w-\.\#]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?(\s?[\+\-\*\\]\s?[\d\.\+\-\*\\\(\)]+)?\}/g,
349 codeRe : /\{\[((?:\\\]|.|\n)*?)\]\}/g,
352 applySubTemplate : function(id, values, parent, xindex, xcount){
358 if ((t.test && !t.test.call(me, values, parent, xindex, xcount)) ||
359 (t.exec && t.exec.call(me, values, parent, xindex, xcount))) {
362 vs = t.target ? t.target.call(me, values, parent) : values;
364 parent = t.target ? values : parent;
365 if(t.target && Ext.isArray(vs)){
366 Ext.each(vs, function(v, i) {
367 buf[buf.length] = t.compiled.call(me, v, parent, i+1, len);
371 return t.compiled.call(me, vs, parent, xindex, xcount);
375 compileTpl : function(tpl){
376 var fm = Ext.util.Format,
377 useF = this.disableFormats !== true,
378 sep = Ext.isGecko ? "+" : ",",
381 function fn(m, name, format, args, math){
382 if(name.substr(0, 4) == 'xtpl'){
383 return "'"+ sep +'this.applySubTemplate('+name.substr(4)+', values, parent, xindex, xcount)'+sep+"'";
388 }else if(name === '#'){
390 }else if(name.indexOf('.') != -1){
393 v = "values['" + name + "']";
396 v = '(' + v + math + ')';
398 if (format && useF) {
399 args = args ? ',' + args : "";
400 if(format.substr(0, 5) != "this."){
401 format = "fm." + format + '(';
403 format = 'this.call("'+ format.substr(5) + '", ';
407 args= ''; format = "("+v+" === undefined ? '' : ";
409 return "'"+ sep + format + v + args + ")"+sep+"'";
412 function codeFn(m, code){
413 // Single quotes get escaped when the template is compiled, however we want to undo this when running code.
414 return "'" + sep + '(' + code.replace(/\\'/g, "'") + ')' + sep + "'";
417 // branched to use + in gecko and [].join() in others
419 body = "tpl.compiled = function(values, parent, xindex, xcount){ return '" +
420 tpl.body.replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn).replace(this.codeRe, codeFn) +
423 body = ["tpl.compiled = function(values, parent, xindex, xcount){ return ['"];
424 body.push(tpl.body.replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn).replace(this.codeRe, codeFn));
425 body.push("'].join('');};");
426 body = body.join('');
432 <div id="method-Ext.XTemplate-applyTemplate"></div>/**
433 * Returns an HTML fragment of this template with the specified values applied.
434 * @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'})
435 * @return {String} The HTML fragment
437 applyTemplate : function(values){
438 return this.master.compiled.call(this, values, {}, 1, 1);
441 <div id="method-Ext.XTemplate-compile"></div>/**
442 * Compile the template to a function for optimized performance. Recommended if the template will be used frequently.
443 * @return {Function} The compiled function
445 compile : function(){return this;}
447 <div id="prop-Ext.XTemplate-re"></div>/**
451 <div id="prop-Ext.XTemplate-disableFormats"></div>/**
452 * @property disableFormats
455 <div id="method-Ext.XTemplate-set"></div>/**
461 <div id="method-Ext.XTemplate-apply"></div>/**
462 * Alias for {@link #applyTemplate}
463 * Returns an HTML fragment of this template with the specified values applied.
464 * @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'})
465 * @return {String} The HTML fragment
466 * @member Ext.XTemplate
469 Ext.XTemplate.prototype.apply = Ext.XTemplate.prototype.applyTemplate;
471 <div id="method-Ext.XTemplate-XTemplate.from"></div>/**
472 * Creates a template from the passed element's value (<i>display:none</i> textarea, preferred) or innerHTML.
473 * @param {String/HTMLElement} el A DOM element or its id
474 * @return {Ext.Template} The created template
477 Ext.XTemplate.from = function(el){
479 return new Ext.XTemplate(el.value || el.innerHTML);