Upgrade to ExtJS 3.0.0 - Released 07/06/2009
[extjs.git] / docs / output / Ext.XTemplate.html
1 <div class="body-wrap" xmlns:ext="http://www.extjs.com"><div class="top-tools"><a class="inner-link" href="#Ext.XTemplate-props"><img src="resources/images/default/s.gif" class="item-icon icon-prop">Properties</a>&#13;<a class="inner-link" href="#Ext.XTemplate-methods"><img src="resources/images/default/s.gif" class="item-icon icon-method">Methods</a>&#13;<a class="inner-link" href="#Ext.XTemplate-events"><img src="resources/images/default/s.gif" class="item-icon icon-event">Events</a>&#13;<a class="bookmark" href="../docs/?class=Ext.XTemplate"><img src="resources/images/default/s.gif" class="item-icon icon-fav">Direct Link</a>&#13;</div><div class="inheritance res-block"><pre class="res-block-inner"><a href="output/Ext.Template.html" ext:member="" ext:cls="Ext.Template">Template</a>&#13;&nbsp;&nbsp;<img src="resources/elbow-end.gif">XTemplate</pre></div><h1>Class <a href="source/XTemplate.html#cls-Ext.XTemplate">Ext.XTemplate</a></h1><table cellspacing="0"><tr><td class="label">Package:</td><td class="hd-info">Ext</td></tr><tr><td class="label">Defined In:</td><td class="hd-info">XTemplate.js</td></tr><tr><td class="label">Class:</td><td class="hd-info"><a href="source/XTemplate.html#cls-Ext.XTemplate">XTemplate</a></td></tr><tr><td class="label">Extends:</td><td class="hd-info"><a href="output/Ext.Template.html" ext:cls="Ext.Template" ext:member="">Template</a></td></tr></table><div class="description"><p>A template class that supports advanced functionality like autofilling arrays, conditional processing with
2 basic comparison operators, sub-templates, basic math function support, special built-in template variables,
3 inline code execution and more.  XTemplate also provides the templating mechanism built into <a href="output/Ext.DataView.html" ext:cls="Ext.DataView">Ext.DataView</a>.</p>
4 <p>XTemplate supports many special tags and built-in operators that aren't defined as part of the API, but are
5 supported in the templates that can be created.  The following examples demonstrate all of the supported features.
6 This is the data object used for reference in each code example:</p>
7 <pre><code><b>var</b> data = {
8     name: <em>'Jack Slocum'</em>,
9     title: <em>'Lead Developer'</em>,
10     company: <em>'Ext JS, LLC'</em>,
11     email: <em>'jack@extjs.com'</em>,
12     address: <em>'4 Red Bulls Drive'</em>,
13     city: <em>'Cleveland'</em>,
14     state: <em>'Ohio'</em>,
15     zip: <em>'44102'</em>,
16     drinks: [<em>'Red Bull'</em>, <em>'Coffee'</em>, <em>'Water'</em>],
17     kids: [{
18         name: <em>'Sara Grace'</em>,
19         age:3
20     },{
21         name: <em>'Zachary'</em>,
22         age:2
23     },{
24         name: <em>'John James'</em>,
25         age:0
26     }]
27 };</code></pre>
28 <p><b>Auto filling of arrays</b><br/>The <tt>tpl</tt> tag and the <tt>for</tt> operator are used
29 to process the provided data object. If <tt>for="."</tt> is specified, the data object provided
30 is examined. If the variable in <tt>for</tt> is an array, it will auto-fill, repeating the template
31 block inside the <tt>tpl</tt> tag for each item in the array:</p>
32 <pre><code><b>var</b> tpl = <b>new</b> Ext.XTemplate(
33     <em>'&lt;p>Kids: '</em>,
34     <em>'&lt;tpl <b>for</b>=<em>"."</em>>'</em>,
35         <em>'&lt;p>{name}&lt;/p>'</em>,
36     <em>'&lt;/tpl>&lt;/p>'</em>
37 );
38 tpl.overwrite(panel.body, data.kids); <i>// pass the kids property of the data object</i></code></pre>
39 <p><b>Scope switching</b><br/>The <tt>for</tt> property can be leveraged to access specified members
40 of the provided data object to populate the template:</p>
41 <pre><code><b>var</b> tpl = <b>new</b> Ext.XTemplate(
42     <em>'&lt;p>Name: {name}&lt;/p>'</em>,
43     <em>'&lt;p>Title: {title}&lt;/p>'</em>,
44     <em>'&lt;p>Company: {company}&lt;/p>'</em>,
45     <em>'&lt;p>Kids: '</em>,
46     <em>'&lt;tpl <b><b>for</b>=<em>"kids"</em></b>>'</em>, <i>// interrogate the kids property within the data</i>
47         <em>'&lt;p>{name}&lt;/p>'</em>,
48     <em>'&lt;/tpl>&lt;/p>'</em>
49 );
50 tpl.overwrite(panel.body, data);</code></pre>
51 <p><b>Access to parent object from within sub-template scope</b><br/>When processing a sub-template, for example while
52 looping through a child array, you can access the parent object's members via the <tt>parent</tt> object:</p>
53 <pre><code><b>var</b> tpl = <b>new</b> Ext.XTemplate(
54     <em>'&lt;p>Name: {name}&lt;/p>'</em>,
55     <em>'&lt;p>Kids: '</em>,
56     <em>'&lt;tpl <b>for</b>=<em>"kids"</em>>'</em>,
57         <em>'&lt;tpl <b>if</b>=<em>"age &amp;gt; 1"</em>>'</em>,  <i>// <-- Note that the &gt; is encoded</i>
58             <em>'&lt;p>{name}&lt;/p>'</em>,
59             <em>'&lt;p>Dad: {parent.name}&lt;/p>'</em>,
60         <em>'&lt;/tpl>'</em>,
61     <em>'&lt;/tpl>&lt;/p>'</em>
62 );
63 tpl.overwrite(panel.body, data);</code></pre>
64 <p><b>Array item index and basic math support</b> <br/>While processing an array, the special variable <tt>{#}</tt>
65 will provide the current array index + 1 (starts at 1, not 0). Templates also support the basic math operators
66 + - * and / that can be applied directly on numeric data values:</p>
67 <pre><code><b>var</b> tpl = <b>new</b> Ext.XTemplate(
68     <em>'&lt;p>Name: {name}&lt;/p>'</em>,
69     <em>'&lt;p>Kids: '</em>,
70     <em>'&lt;tpl <b>for</b>=<em>"kids"</em>>'</em>,
71         <em>'&lt;tpl <b>if</b>=<em>"age &amp;gt; 1"</em>>'</em>,  <i>// <-- Note that the &gt; is encoded</i>
72             <em>'&lt;p>{#}: {name}&lt;/p>'</em>,  <i>// <-- Auto-number each item</i>
73             <em>'&lt;p>In 5 Years: {age+5}&lt;/p>'</em>,  <i>// <-- Basic math</i>
74             <em>'&lt;p>Dad: {parent.name}&lt;/p>'</em>,
75         <em>'&lt;/tpl>'</em>,
76     <em>'&lt;/tpl>&lt;/p>'</em>
77 );
78 tpl.overwrite(panel.body, data);</code></pre>
79 <p><b>Auto-rendering of flat arrays</b> <br/>Flat arrays that contain values (and not objects) can be auto-rendered
80 using the special <tt>{.}</tt> variable inside a loop.  This variable will represent the value of
81 the array at the current index:</p>
82 <pre><code><b>var</b> tpl = <b>new</b> Ext.XTemplate(
83     <em>'&lt;p>{name}\'</em>s favorite beverages:&lt;/p><em>',
84     '</em>&lt;tpl <b>for</b>=<em>"drinks"</em>><em>',
85        '</em>&lt;div> - {.}&lt;/div><em>',
86     '</em>&lt;/tpl><em>'
87 );
88 tpl.overwrite(panel.body, data);</code></pre>
89 <p><b>Basic conditional logic</b> <br/>Using the <tt>tpl</tt> tag and the <tt>if</tt>
90 operator you can provide conditional checks for deciding whether or not to render specific parts of the template.
91 Note that there is no <tt>else</tt> operator &mdash; if needed, you should use two opposite <tt>if</tt> statements.
92 Properly-encoded attributes are required as seen in the following example:</p>
93 <pre><code><b>var</b> tpl = <b>new</b> Ext.XTemplate(
94     <em>'&lt;p>Name: {name}&lt;/p>'</em>,
95     <em>'&lt;p>Kids: '</em>,
96     <em>'&lt;tpl <b>for</b>=<em>"kids"</em>>'</em>,
97         <em>'&lt;tpl <b>if</b>=<em>"age &amp;gt; 1"</em>>'</em>,  <i>// <-- Note that the &gt; is encoded</i>
98             <em>'&lt;p>{name}&lt;/p>'</em>,
99         <em>'&lt;/tpl>'</em>,
100     <em>'&lt;/tpl>&lt;/p>'</em>
101 );
102 tpl.overwrite(panel.body, data);</code></pre>
103 <p><b>Ability to execute arbitrary inline code</b> <br/>In an XTemplate, anything between {[ ... ]}  is considered
104 code to be executed in the scope of the template. There are some special variables available in that code:
105 <ul>
106 <li><b><tt>values</tt></b>: The values in the current scope. If you are using scope changing sub-templates, you
107 can change what <tt>values</tt> is.</li>
108 <li><b><tt>parent</tt></b>: The scope (values) of the ancestor template.</li>
109 <li><b><tt>xindex</tt></b>: If you are in a looping template, the index of the loop you are in (1-based).</li>
110 <li><b><tt>xcount</tt></b>: If you are in a looping template, the total length of the array you are looping.</li>
111 <li><b><tt>fm</tt></b>: An alias for <tt>Ext.util.Format</tt>.</li>
112 </ul>
113 This example demonstrates basic row striping using an inline code block and the <tt>xindex</tt> variable:</p>
114 <pre><code><b>var</b> tpl = <b>new</b> Ext.XTemplate(
115     <em>'&lt;p>Name: {name}&lt;/p>'</em>,
116     <em>'&lt;p>Company: {[values.company.toUpperCase() + <em>", "</em> + values.title]}&lt;/p>'</em>,
117     <em>'&lt;p>Kids: '</em>,
118     <em>'&lt;tpl <b>for</b>=<em>"kids"</em>>'</em>,
119        <em>'&lt;div class=<em>"{[xindex % 2 === 0 ? "</em>even<em>" : "</em>odd<em>"]}"</em>>'</em>,
120         <em>'{name}'</em>,
121         <em>'&lt;/div>'</em>,
122     <em>'&lt;/tpl>&lt;/p>'</em>
123 );
124 tpl.overwrite(panel.body, data);</code></pre>
125 <p><b>Template member functions</b> <br/>One or more member functions can be defined directly on the config
126 object passed into the XTemplate constructor for more complex processing:</p>
127 <pre><code><b>var</b> tpl = <b>new</b> Ext.XTemplate(
128     <em>'&lt;p>Name: {name}&lt;/p>'</em>,
129     <em>'&lt;p>Kids: '</em>,
130     <em>'&lt;tpl <b>for</b>=<em>"kids"</em>>'</em>,
131         <em>'&lt;tpl <b>if</b>=<em>"this.isGirl(name)"</em>>'</em>,
132             <em>'&lt;p>Girl: {name} - {age}&lt;/p>'</em>,
133         <em>'&lt;/tpl>'</em>,
134         <em>'&lt;tpl <b>if</b>=<em>"this.isGirl(name) == false"</em>>'</em>,
135             <em>'&lt;p>Boy: {name} - {age}&lt;/p>'</em>,
136         <em>'&lt;/tpl>'</em>,
137         <em>'&lt;tpl <b>if</b>=<em>"this.isBaby(age)"</em>>'</em>,
138             <em>'&lt;p>{name} is a baby!&lt;/p>'</em>,
139         <em>'&lt;/tpl>'</em>,
140     <em>'&lt;/tpl>&lt;/p>'</em>, {
141      isGirl: <b>function</b>(name){
142          <b>return</b> name == <em>'Sara Grace'</em>;
143      },
144      isBaby: <b>function</b>(age){
145         <b>return</b> age < 1;
146      }
147 });
148 tpl.overwrite(panel.body, data);</code></pre></div><div class="hr"></div><a id="Ext.XTemplate-props"></a><h2>Public Properties</h2><div class="no-members">This class has no public properties.</div><a id="Ext.XTemplate-methods"></a><h2>Public Methods</h2><table cellspacing="0" class="member-table"><tbody><tr><th colspan="2" class="sig-header">Method</th><th class="msource-header">Defined By</th></tr><tr class="method-row expandable"><td class="micon"><a href="#expand" class="exi">&nbsp;</a></td><td class="sig"><a id="Ext.XTemplate-XTemplate"></a><b><a href="source/XTemplate.html#cls-Ext.XTemplate">XTemplate</a></b>(&nbsp;<code>String/Array/Object&nbsp;parts</code>&nbsp;)
149     <div class="mdesc"><div class="short"></div><div class="long"><div class="mdetail-params"><strong>Parameters:</strong><ul><li><code>parts</code> : String/Array/Object<div class="sub-desc">The HTML fragment or an array of fragments to join(""), or multiple arguments
150 to join("") that can also include a config object</div></li></ul><strong>Returns:</strong><ul><li>void</li></ul></div></div></div></td><td class="msource">XTemplate</td></tr><tr class="method-row expandable "><td class="micon"><a href="#expand" class="exi">&nbsp;</a></td><td class="sig"><a id="Ext.XTemplate-XTemplate.from"></a><b><a href="source/XTemplate.html#method-Ext.XTemplate-XTemplate.from">XTemplate.from</a></b>(&nbsp;<code>String/HTMLElement&nbsp;el</code>&nbsp;)
151     :
152                                         Ext.Template<div class="mdesc"><div class="short">&lt;static&gt;&nbsp;Creates a template from the passed element's value (display:none textarea, preferred) or innerHTML.</div><div class="long">&lt;static&gt;&nbsp;Creates a template from the passed element's value (<i>display:none</i> textarea, preferred) or innerHTML.<div class="mdetail-params"><strong>Parameters:</strong><ul><li><code>el</code> : String/HTMLElement<div class="sub-desc">A DOM element or its id</div></li></ul><strong>Returns:</strong><ul><li><code>Ext.Template</code><div class="sub-desc">The created template</div></li></ul></div></div></div></td><td class="msource">XTemplate</td></tr><tr class="method-row expandable inherited"><td class="micon"><a href="#expand" class="exi">&nbsp;</a></td><td class="sig"><a id="Ext.Template-append"></a><b><a href="source/Template.html#method-Ext.Template-append">append</a></b>(&nbsp;<code>Mixed&nbsp;el</code>,&nbsp;<code>Object/Array&nbsp;values</code>,&nbsp;<span title="Optional" class="optional">[<code>Boolean&nbsp;returnElement</code>]</span>&nbsp;)
153     :
154                                         HTMLElement/Ext.Element<div class="mdesc"><div class="short">Applies the supplied values to the template and appends the new node(s) to el.</div><div class="long">Applies the supplied values to the template and appends the new node(s) to el.<div class="mdetail-params"><strong>Parameters:</strong><ul><li><code>el</code> : Mixed<div class="sub-desc">The context element</div></li><li><code>values</code> : Object/Array<div class="sub-desc">The template values. Can be an array if your params are numeric (i.e. {0}) or an object (i.e. {foo: 'bar'})</div></li><li><code>returnElement</code> : Boolean<div class="sub-desc">(optional) true to return a Ext.Element (defaults to undefined)</div></li></ul><strong>Returns:</strong><ul><li><code>HTMLElement/Ext.Element</code><div class="sub-desc">The new node or Element</div></li></ul></div></div></div></td><td class="msource"><a href="output/Ext.Template.html#append" ext:member="#append" ext:cls="Ext.Template">Template</a></td></tr><tr class="method-row expandable "><td class="micon"><a href="#expand" class="exi">&nbsp;</a></td><td class="sig"><a id="Ext.XTemplate-apply"></a><b><a href="source/XTemplate.html#method-Ext.XTemplate-apply">apply</a></b>(&nbsp;<code>Object/Array&nbsp;values</code>&nbsp;)
155     :
156                                         String<div class="mdesc"><div class="short">Alias for applyTemplate
157 Returns an HTML fragment of this template with the specified values applied.</div><div class="long">Alias for <a href="output/Ext.XTemplate.html#Ext.XTemplate-applyTemplate" ext:member="applyTemplate" ext:cls="Ext.XTemplate">applyTemplate</a>
158 Returns an HTML fragment of this template with the specified values applied.<div class="mdetail-params"><strong>Parameters:</strong><ul><li><code>values</code> : Object/Array<div class="sub-desc">The template values. Can be an array if your params are numeric (i.e. {0}) or an object (i.e. {foo: 'bar'})</div></li></ul><strong>Returns:</strong><ul><li><code>String</code><div class="sub-desc">The HTML fragment</div></li></ul></div></div></div></td><td class="msource">XTemplate</td></tr><tr class="method-row expandable "><td class="micon"><a href="#expand" class="exi">&nbsp;</a></td><td class="sig"><a id="Ext.XTemplate-applyTemplate"></a><b><a href="source/XTemplate.html#method-Ext.XTemplate-applyTemplate">applyTemplate</a></b>(&nbsp;<code>Object&nbsp;values</code>&nbsp;)
159     :
160                                         String<div class="mdesc"><div class="short">Returns an HTML fragment of this template with the specified values applied.</div><div class="long">Returns an HTML fragment of this template with the specified values applied.<div class="mdetail-params"><strong>Parameters:</strong><ul><li><code>values</code> : Object<div class="sub-desc">The template values. Can be an array if your params are numeric (i.e. {0}) or an object (i.e. {foo: 'bar'})</div></li></ul><strong>Returns:</strong><ul><li><code>String</code><div class="sub-desc">The HTML fragment</div></li></ul></div></div></div></td><td class="msource">XTemplate</td></tr><tr class="method-row expandable "><td class="micon"><a href="#expand" class="exi">&nbsp;</a></td><td class="sig"><a id="Ext.XTemplate-compile"></a><b><a href="source/XTemplate.html#method-Ext.XTemplate-compile">compile</a></b>()
161     :
162                                         Function<div class="mdesc"><div class="short">Compile the template to a function for optimized performance.  Recommended if the template will be used frequently.</div><div class="long">Compile the template to a function for optimized performance.  Recommended if the template will be used frequently.<div class="mdetail-params"><strong>Parameters:</strong><ul><li>None.</li></ul><strong>Returns:</strong><ul><li><code>Function</code><div class="sub-desc">The compiled function</div></li></ul></div></div></div></td><td class="msource">XTemplate</td></tr><tr class="method-row expandable inherited"><td class="micon"><a href="#expand" class="exi">&nbsp;</a></td><td class="sig"><a id="Ext.Template-insertAfter"></a><b><a href="source/Template.html#method-Ext.Template-insertAfter">insertAfter</a></b>(&nbsp;<code>Mixed&nbsp;el</code>,&nbsp;<code>Object/Array&nbsp;values</code>,&nbsp;<span title="Optional" class="optional">[<code>Boolean&nbsp;returnElement</code>]</span>&nbsp;)
163     :
164                                         HTMLElement/Ext.Element<div class="mdesc"><div class="short">Applies the supplied values to the template and inserts the new node(s) after el.</div><div class="long">Applies the supplied values to the template and inserts the new node(s) after el.<div class="mdetail-params"><strong>Parameters:</strong><ul><li><code>el</code> : Mixed<div class="sub-desc">The context element</div></li><li><code>values</code> : Object/Array<div class="sub-desc">The template values. Can be an array if your params are numeric (i.e. {0}) or an object (i.e. {foo: 'bar'})</div></li><li><code>returnElement</code> : Boolean<div class="sub-desc">(optional) true to return a Ext.Element (defaults to undefined)</div></li></ul><strong>Returns:</strong><ul><li><code>HTMLElement/Ext.Element</code><div class="sub-desc">The new node or Element</div></li></ul></div></div></div></td><td class="msource"><a href="output/Ext.Template.html#insertAfter" ext:member="#insertAfter" ext:cls="Ext.Template">Template</a></td></tr><tr class="method-row expandable inherited"><td class="micon"><a href="#expand" class="exi">&nbsp;</a></td><td class="sig"><a id="Ext.Template-insertBefore"></a><b><a href="source/Template.html#method-Ext.Template-insertBefore">insertBefore</a></b>(&nbsp;<code>Mixed&nbsp;el</code>,&nbsp;<code>Object/Array&nbsp;values</code>,&nbsp;<span title="Optional" class="optional">[<code>Boolean&nbsp;returnElement</code>]</span>&nbsp;)
165     :
166                                         HTMLElement/Ext.Element<div class="mdesc"><div class="short">Applies the supplied values to the template and inserts the new node(s) before el.</div><div class="long">Applies the supplied values to the template and inserts the new node(s) before el.<div class="mdetail-params"><strong>Parameters:</strong><ul><li><code>el</code> : Mixed<div class="sub-desc">The context element</div></li><li><code>values</code> : Object/Array<div class="sub-desc">The template values. Can be an array if your params are numeric (i.e. {0}) or an object (i.e. {foo: 'bar'})</div></li><li><code>returnElement</code> : Boolean<div class="sub-desc">(optional) true to return a Ext.Element (defaults to undefined)</div></li></ul><strong>Returns:</strong><ul><li><code>HTMLElement/Ext.Element</code><div class="sub-desc">The new node or Element</div></li></ul></div></div></div></td><td class="msource"><a href="output/Ext.Template.html#insertBefore" ext:member="#insertBefore" ext:cls="Ext.Template">Template</a></td></tr><tr class="method-row expandable inherited"><td class="micon"><a href="#expand" class="exi">&nbsp;</a></td><td class="sig"><a id="Ext.Template-insertFirst"></a><b><a href="source/Template.html#method-Ext.Template-insertFirst">insertFirst</a></b>(&nbsp;<code>Mixed&nbsp;el</code>,&nbsp;<code>Object/Array&nbsp;values</code>,&nbsp;<span title="Optional" class="optional">[<code>Boolean&nbsp;returnElement</code>]</span>&nbsp;)
167     :
168                                         HTMLElement/Ext.Element<div class="mdesc"><div class="short">Applies the supplied values to the template and inserts the new node(s) as the first child of el.</div><div class="long">Applies the supplied values to the template and inserts the new node(s) as the first child of el.<div class="mdetail-params"><strong>Parameters:</strong><ul><li><code>el</code> : Mixed<div class="sub-desc">The context element</div></li><li><code>values</code> : Object/Array<div class="sub-desc">The template values. Can be an array if your params are numeric (i.e. {0}) or an object (i.e. {foo: 'bar'})</div></li><li><code>returnElement</code> : Boolean<div class="sub-desc">(optional) true to return a Ext.Element (defaults to undefined)</div></li></ul><strong>Returns:</strong><ul><li><code>HTMLElement/Ext.Element</code><div class="sub-desc">The new node or Element</div></li></ul></div></div></div></td><td class="msource"><a href="output/Ext.Template.html#insertFirst" ext:member="#insertFirst" ext:cls="Ext.Template">Template</a></td></tr><tr class="method-row expandable inherited"><td class="micon"><a href="#expand" class="exi">&nbsp;</a></td><td class="sig"><a id="Ext.Template-overwrite"></a><b><a href="source/Template.html#method-Ext.Template-overwrite">overwrite</a></b>(&nbsp;<code>Mixed&nbsp;el</code>,&nbsp;<code>Object/Array&nbsp;values</code>,&nbsp;<span title="Optional" class="optional">[<code>Boolean&nbsp;returnElement</code>]</span>&nbsp;)
169     :
170                                         HTMLElement/Ext.Element<div class="mdesc"><div class="short">Applies the supplied values to the template and overwrites the content of el with the new node(s).</div><div class="long">Applies the supplied values to the template and overwrites the content of el with the new node(s).<div class="mdetail-params"><strong>Parameters:</strong><ul><li><code>el</code> : Mixed<div class="sub-desc">The context element</div></li><li><code>values</code> : Object/Array<div class="sub-desc">The template values. Can be an array if your params are numeric (i.e. {0}) or an object (i.e. {foo: 'bar'})</div></li><li><code>returnElement</code> : Boolean<div class="sub-desc">(optional) true to return a Ext.Element (defaults to undefined)</div></li></ul><strong>Returns:</strong><ul><li><code>HTMLElement/Ext.Element</code><div class="sub-desc">The new node or Element</div></li></ul></div></div></div></td><td class="msource"><a href="output/Ext.Template.html#overwrite" ext:member="#overwrite" ext:cls="Ext.Template">Template</a></td></tr></tbody></table><a id="Ext.XTemplate-events"></a><h2>Public Events</h2><div class="no-members">This class has no public events.</div></div>