3 <title>The source code</title>
\r
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
7 <body onload="prettyPrint();">
\r
8 <pre class="prettyprint lang-js"><div id="cls-Ext.XTemplate"></div>/**
10 * @extends Ext.Template
11 * <p>A template class that supports advanced functionality like autofilling arrays, conditional processing with
12 * basic comparison operators, sub-templates, basic math function support, special built-in template variables,
13 * inline code execution and more. XTemplate also provides the templating mechanism built into {@link Ext.DataView}.</p>
14 * <p>XTemplate supports many special tags and built-in operators that aren't defined as part of the API, but are
15 * supported in the templates that can be created. The following examples demonstrate all of the supported features.
16 * This is the data object used for reference in each code example:</p>
20 title: 'Lead Developer',
21 company: 'Ext JS, LLC',
22 email: 'jack@extjs.com',
23 address: '4 Red Bulls Drive',
27 drinks: ['Red Bull', 'Coffee', 'Water'],
40 * <p><b>Auto filling of arrays</b><br/>The <tt>tpl</tt> tag and the <tt>for</tt> operator are used
41 * to process the provided data object. If <tt>for="."</tt> is specified, the data object provided
42 * is examined. If the variable in <tt>for</tt> is an array, it will auto-fill, repeating the template
43 * block inside the <tt>tpl</tt> tag for each item in the array:</p>
45 var tpl = new Ext.XTemplate(
48 '<p>{name}</p>',
51 tpl.overwrite(panel.body, data.kids); // pass the kids property of the data object
53 * <p><b>Scope switching</b><br/>The <tt>for</tt> property can be leveraged to access specified members
54 * of the provided data object to populate the template:</p>
56 var tpl = new Ext.XTemplate(
57 '<p>Name: {name}</p>',
58 '<p>Title: {title}</p>',
59 '<p>Company: {company}</p>',
61 '<tpl <b>for="kids"</b>>', // interrogate the kids property within the data
62 '<p>{name}</p>',
65 tpl.overwrite(panel.body, data);
67 * <p><b>Access to parent object from within sub-template scope</b><br/>When processing a sub-template, for example while
68 * looping through a child array, you can access the parent object's members via the <tt>parent</tt> object:</p>
70 var tpl = new Ext.XTemplate(
71 '<p>Name: {name}</p>',
73 '<tpl for="kids">',
74 '<tpl if="age &gt; 1">', // <-- Note that the > is encoded
75 '<p>{name}</p>',
76 '<p>Dad: {parent.name}</p>',
80 tpl.overwrite(panel.body, data);
82 * <p><b>Array item index and basic math support</b> <br/>While processing an array, the special variable <tt>{#}</tt>
83 * will provide the current array index + 1 (starts at 1, not 0). Templates also support the basic math operators
84 * + - * and / that can be applied directly on numeric data values:</p>
86 var tpl = new Ext.XTemplate(
87 '<p>Name: {name}</p>',
89 '<tpl for="kids">',
90 '<tpl if="age &gt; 1">', // <-- Note that the > is encoded
91 '<p>{#}: {name}</p>', // <-- Auto-number each item
92 '<p>In 5 Years: {age+5}</p>', // <-- Basic math
93 '<p>Dad: {parent.name}</p>',
97 tpl.overwrite(panel.body, data);
99 * <p><b>Auto-rendering of flat arrays</b> <br/>Flat arrays that contain values (and not objects) can be auto-rendered
100 * using the special <tt>{.}</tt> variable inside a loop. This variable will represent the value of
101 * the array at the current index:</p>
103 var tpl = new Ext.XTemplate(
104 '<p>{name}\'s favorite beverages:</p>',
105 '<tpl for="drinks">',
106 '<div> - {.}</div>',
109 tpl.overwrite(panel.body, data);
111 * <p><b>Basic conditional logic</b> <br/>Using the <tt>tpl</tt> tag and the <tt>if</tt>
112 * operator you can provide conditional checks for deciding whether or not to render specific parts of the template.
113 * Note that there is no <tt>else</tt> operator — if needed, you should use two opposite <tt>if</tt> statements.
114 * Properly-encoded attributes are required as seen in the following example:</p>
116 var tpl = new Ext.XTemplate(
117 '<p>Name: {name}</p>',
119 '<tpl for="kids">',
120 '<tpl if="age &gt; 1">', // <-- Note that the > is encoded
121 '<p>{name}</p>',
125 tpl.overwrite(panel.body, data);
127 * <p><b>Ability to execute arbitrary inline code</b> <br/>In an XTemplate, anything between {[ ... ]} is considered
128 * code to be executed in the scope of the template. There are some special variables available in that code:
130 * <li><b><tt>values</tt></b>: The values in the current scope. If you are using scope changing sub-templates, you
131 * can change what <tt>values</tt> is.</li>
132 * <li><b><tt>parent</tt></b>: The scope (values) of the ancestor template.</li>
133 * <li><b><tt>xindex</tt></b>: If you are in a looping template, the index of the loop you are in (1-based).</li>
134 * <li><b><tt>xcount</tt></b>: If you are in a looping template, the total length of the array you are looping.</li>
135 * <li><b><tt>fm</tt></b>: An alias for <tt>Ext.util.Format</tt>.</li>
137 * This example demonstrates basic row striping using an inline code block and the <tt>xindex</tt> variable:</p>
139 var tpl = new Ext.XTemplate(
140 '<p>Name: {name}</p>',
141 '<p>Company: {[values.company.toUpperCase() + ", " + values.title]}</p>',
143 '<tpl for="kids">',
144 '<div class="{[xindex % 2 === 0 ? "even" : "odd"]}">',
149 tpl.overwrite(panel.body, data);
151 * <p><b>Template member functions</b> <br/>One or more member functions can be defined directly on the config
152 * object passed into the XTemplate constructor for more complex processing:</p>
154 var tpl = new Ext.XTemplate(
155 '<p>Name: {name}</p>',
157 '<tpl for="kids">',
158 '<tpl if="this.isGirl(name)">',
159 '<p>Girl: {name} - {age}</p>',
161 '<tpl if="this.isGirl(name) == false">',
162 '<p>Boy: {name} - {age}</p>',
164 '<tpl if="this.isBaby(age)">',
165 '<p>{name} is a baby!</p>',
167 '</tpl></p>', {
168 isGirl: function(name){
169 return name == 'Sara Grace';
171 isBaby: function(age){
175 tpl.overwrite(panel.body, data);
178 * @param {String/Array/Object} parts The HTML fragment or an array of fragments to join(""), or multiple arguments
179 * to join("") that can also include a config object
181 Ext.XTemplate = function(){
182 Ext.XTemplate.superclass.constructor.apply(this, arguments);
186 re = /<tpl\b[^>]*>((?:(?=([^<]+))\2|<(?!tpl\b[^>]*>))*?)<\/tpl>/,
187 nameRe = /^<tpl\b[^>]*?for="(.*?)"/,
188 ifRe = /^<tpl\b[^>]*?if="(.*?)"/,
189 execRe = /^<tpl\b[^>]*?exec="(.*?)"/,
198 WITHVALUES = 'with(values){ ';
200 s = ['<tpl>', s, '</tpl>'].join('');
202 while((m = s.match(re))){
203 var m2 = m[0].match(nameRe),
204 m3 = m[0].match(ifRe),
205 m4 = m[0].match(execRe),
209 name = m2 && m2[1] ? m2[1] : '';
212 exp = m3 && m3[1] ? m3[1] : null;
214 fn = new Function(VALUES, PARENT, XINDEX, XCOUNT, WITHVALUES + RETURN +(Ext.util.Format.htmlDecode(exp))+'; }');
218 exp = m4 && m4[1] ? m4[1] : null;
220 exec = new Function(VALUES, PARENT, XINDEX, XCOUNT, WITHVALUES +(Ext.util.Format.htmlDecode(exp))+'; }');
225 case '.': name = new Function(VALUES, PARENT, WITHVALUES + RETURN + VALUES + '; }'); break;
226 case '..': name = new Function(VALUES, PARENT, WITHVALUES + RETURN + PARENT + '; }'); break;
227 default: name = new Function(VALUES, PARENT, WITHVALUES + RETURN + name + '; }');
237 s = s.replace(m[0], '{xtpl'+ id + '}');
240 Ext.each(tpls, function(t) {
243 me.master = tpls[tpls.length-1];
246 Ext.extend(Ext.XTemplate, Ext.Template, {
248 re : /\{([\w-\.\#]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?(\s?[\+\-\*\\]\s?[\d\.\+\-\*\\\(\)]+)?\}/g,
250 codeRe : /\{\[((?:\\\]|.|\n)*?)\]\}/g,
253 applySubTemplate : function(id, values, parent, xindex, xcount){
259 if ((t.test && !t.test.call(me, values, parent, xindex, xcount)) ||
260 (t.exec && t.exec.call(me, values, parent, xindex, xcount))) {
263 vs = t.target ? t.target.call(me, values, parent) : values;
265 parent = t.target ? values : parent;
266 if(t.target && Ext.isArray(vs)){
267 Ext.each(vs, function(v, i) {
268 buf[buf.length] = t.compiled.call(me, v, parent, i+1, len);
272 return t.compiled.call(me, vs, parent, xindex, xcount);
276 compileTpl : function(tpl){
277 var fm = Ext.util.Format,
278 useF = this.disableFormats !== true,
279 sep = Ext.isGecko ? "+" : ",",
282 function fn(m, name, format, args, math){
283 if(name.substr(0, 4) == 'xtpl'){
284 return "'"+ sep +'this.applySubTemplate('+name.substr(4)+', values, parent, xindex, xcount)'+sep+"'";
289 }else if(name === '#'){
291 }else if(name.indexOf('.') != -1){
294 v = "values['" + name + "']";
297 v = '(' + v + math + ')';
299 if (format && useF) {
300 args = args ? ',' + args : "";
301 if(format.substr(0, 5) != "this."){
302 format = "fm." + format + '(';
304 format = 'this.call("'+ format.substr(5) + '", ';
308 args= ''; format = "("+v+" === undefined ? '' : ";
310 return "'"+ sep + format + v + args + ")"+sep+"'";
313 function codeFn(m, code){
314 return "'"+ sep +'('+code+')'+sep+"'";
317 // branched to use + in gecko and [].join() in others
319 body = "tpl.compiled = function(values, parent, xindex, xcount){ return '" +
320 tpl.body.replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn).replace(this.codeRe, codeFn) +
323 body = ["tpl.compiled = function(values, parent, xindex, xcount){ return ['"];
324 body.push(tpl.body.replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn).replace(this.codeRe, codeFn));
325 body.push("'].join('');};");
326 body = body.join('');
332 <div id="method-Ext.XTemplate-applyTemplate"></div>/**
333 * Returns an HTML fragment of this template with the specified values applied.
334 * @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'})
335 * @return {String} The HTML fragment
337 applyTemplate : function(values){
338 return this.master.compiled.call(this, values, {}, 1, 1);
341 <div id="method-Ext.XTemplate-compile"></div>/**
342 * Compile the template to a function for optimized performance. Recommended if the template will be used frequently.
343 * @return {Function} The compiled function
345 compile : function(){return this;}
347 <div id="prop-Ext.XTemplate-re"></div>/**
351 <div id="prop-Ext.XTemplate-disableFormats"></div>/**
352 * @property disableFormats
355 <div id="method-Ext.XTemplate-set"></div>/**
361 <div id="method-Ext.XTemplate-apply"></div>/**
362 * Alias for {@link #applyTemplate}
363 * Returns an HTML fragment of this template with the specified values applied.
364 * @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'})
365 * @return {String} The HTML fragment
366 * @member Ext.XTemplate
369 Ext.XTemplate.prototype.apply = Ext.XTemplate.prototype.applyTemplate;
371 <div id="method-Ext.XTemplate-XTemplate.from"></div>/**
372 * Creates a template from the passed element's value (<i>display:none</i> textarea, preferred) or innerHTML.
373 * @param {String/HTMLElement} el A DOM element or its id
374 * @return {Ext.Template} The created template
377 Ext.XTemplate.from = function(el){
379 return new Ext.XTemplate(el.value || el.innerHTML);