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.Template"></div>/**
17 * <p>Represents an HTML fragment template. Templates may be {@link #compile precompiled}
18 * for greater performance.</p>
19 * <p>For example usage {@link #Template see the constructor}.</p>
22 * An instance of this class may be created by passing to the constructor either
23 * a single argument, or multiple arguments:
24 * <div class="mdetail-params"><ul>
25 * <li><b>single argument</b> : String/Array
26 * <div class="sub-desc">
27 * The single argument may be either a String or an Array:<ul>
28 * <li><tt>String</tt> : </li><pre><code>
29 var t = new Ext.Template("<div>Hello {0}.</div>");
30 t.{@link #append}('some-element', ['foo']);
32 * <li><tt>Array</tt> : </li>
33 * An Array will be combined with <code>join('')</code>.
35 var t = new Ext.Template([
36 '<div name="{id}">',
37 '<span class="{cls}">{name:trim} {value:ellipsis(10)}</span>',
41 t.{@link #append}('some-element', {id: 'myid', cls: 'myclass', name: 'foo', value: 'bar'});
44 * <li><b>multiple arguments</b> : String, Object, Array, ...
45 * <div class="sub-desc">
46 * Multiple arguments will be combined with <code>join('')</code>.
48 var t = new Ext.Template(
49 '<div name="{id}">',
50 '<span class="{cls}">{name} {value}</span>',
52 // a configuration object:
54 compiled: true, // {@link #compile} immediately
55 disableFormats: true // See Notes below.
59 * <p><b>Notes</b>:</p>
60 * <div class="mdetail-params"><ul>
61 * <li>Formatting and <code>disableFormats</code> are not applicable for Ext Core.</li>
62 * <li>For a list of available format functions, see {@link Ext.util.Format}.</li>
63 * <li><code>disableFormats</code> reduces <code>{@link #apply}</code> time
64 * when no formatting is required.</li>
68 * @param {Mixed} config
70 Ext.Template = function(html){
76 if (Ext.isArray(html)) {
78 } else if (a.length > 1) {
79 for(var i = 0, len = a.length; i < len; i++){
81 if(typeof v == 'object'){
92 <div id="cfg-Ext.Template-compiled"></div>/**
93 * @cfg {Boolean} compiled Specify <tt>true</tt> to compile the template
94 * immediately (see <code>{@link #compile}</code>).
95 * Defaults to <tt>false</tt>.
101 Ext.Template.prototype = {
102 <div id="cfg-Ext.Template-re"></div>/**
103 * @cfg {RegExp} re The regular expression used to match template variables.
104 * Defaults to:<pre><code>
105 * re : /\{([\w-]+)\}/g // for Ext Core
106 * re : /\{([\w-]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?\}/g // for Ext JS
109 re : /\{([\w-]+)\}/g,
110 <div id="prop-Ext.Template-re"></div>/**
111 * See <code>{@link #re}</code>.
116 <div id="method-Ext.Template-applyTemplate"></div>/**
117 * Returns an HTML fragment of this template with the specified <code>values</code> applied.
118 * @param {Object/Array} values
119 * The template values. Can be an array if the params are numeric (i.e. <code>{0}</code>)
120 * or an object (i.e. <code>{foo: 'bar'}</code>).
121 * @return {String} The HTML fragment
123 applyTemplate : function(values){
127 me.compiled(values) :
128 me.html.replace(me.re, function(m, name){
129 return values[name] !== undefined ? values[name] : "";
133 <div id="method-Ext.Template-set"></div>/**
134 * Sets the HTML used as the template and optionally compiles it.
135 * @param {String} html
136 * @param {Boolean} compile (optional) True to compile the template (defaults to undefined)
137 * @return {Ext.Template} this
139 set : function(html, compile){
143 return compile ? me.compile() : me;
146 <div id="method-Ext.Template-compile"></div>/**
147 * Compiles the template into an internal function, eliminating the RegEx overhead.
148 * @return {Ext.Template} this
150 compile : function(){
152 sep = Ext.isGecko ? "+" : ",";
154 function fn(m, name){
155 name = "values['" + name + "']";
156 return "'"+ sep + '(' + name + " == undefined ? '' : " + name + ')' + sep + "'";
159 eval("this.compiled = function(values){ return " + (Ext.isGecko ? "'" : "['") +
160 me.html.replace(/\\/g, '\\\\').replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn) +
161 (Ext.isGecko ? "';};" : "'].join('');};"));
165 <div id="method-Ext.Template-insertFirst"></div>/**
166 * Applies the supplied values to the template and inserts the new node(s) as the first child of el.
167 * @param {Mixed} el The context element
168 * @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'})
169 * @param {Boolean} returnElement (optional) true to return a Ext.Element (defaults to undefined)
170 * @return {HTMLElement/Ext.Element} The new node or Element
172 insertFirst: function(el, values, returnElement){
173 return this.doInsert('afterBegin', el, values, returnElement);
176 <div id="method-Ext.Template-insertBefore"></div>/**
177 * Applies the supplied values to the template and inserts the new node(s) before el.
178 * @param {Mixed} el The context element
179 * @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'})
180 * @param {Boolean} returnElement (optional) true to return a Ext.Element (defaults to undefined)
181 * @return {HTMLElement/Ext.Element} The new node or Element
183 insertBefore: function(el, values, returnElement){
184 return this.doInsert('beforeBegin', el, values, returnElement);
187 <div id="method-Ext.Template-insertAfter"></div>/**
188 * Applies the supplied values to the template and inserts the new node(s) after el.
189 * @param {Mixed} el The context element
190 * @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'})
191 * @param {Boolean} returnElement (optional) true to return a Ext.Element (defaults to undefined)
192 * @return {HTMLElement/Ext.Element} The new node or Element
194 insertAfter : function(el, values, returnElement){
195 return this.doInsert('afterEnd', el, values, returnElement);
198 <div id="method-Ext.Template-append"></div>/**
199 * Applies the supplied <code>values</code> to the template and appends
200 * the new node(s) to the specified <code>el</code>.
201 * <p>For example usage {@link #Template see the constructor}.</p>
202 * @param {Mixed} el The context element
203 * @param {Object/Array} values
204 * The template values. Can be an array if the params are numeric (i.e. <code>{0}</code>)
205 * or an object (i.e. <code>{foo: 'bar'}</code>).
206 * @param {Boolean} returnElement (optional) true to return an Ext.Element (defaults to undefined)
207 * @return {HTMLElement/Ext.Element} The new node or Element
209 append : function(el, values, returnElement){
210 return this.doInsert('beforeEnd', el, values, returnElement);
213 doInsert : function(where, el, values, returnEl){
215 var newNode = Ext.DomHelper.insertHtml(where, el, this.applyTemplate(values));
216 return returnEl ? Ext.get(newNode, true) : newNode;
219 <div id="method-Ext.Template-overwrite"></div>/**
220 * Applies the supplied values to the template and overwrites the content of el with the new node(s).
221 * @param {Mixed} el The context element
222 * @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'})
223 * @param {Boolean} returnElement (optional) true to return a Ext.Element (defaults to undefined)
224 * @return {HTMLElement/Ext.Element} The new node or Element
226 overwrite : function(el, values, returnElement){
228 el.innerHTML = this.applyTemplate(values);
229 return returnElement ? Ext.get(el.firstChild, true) : el.firstChild;
232 <div id="method-Ext.Template-apply"></div>/**
233 * Alias for {@link #applyTemplate}
234 * Returns an HTML fragment of this template with the specified <code>values</code> applied.
235 * @param {Object/Array} values
236 * The template values. Can be an array if the params are numeric (i.e. <code>{0}</code>)
237 * or an object (i.e. <code>{foo: 'bar'}</code>).
238 * @return {String} The HTML fragment
239 * @member Ext.Template
242 Ext.Template.prototype.apply = Ext.Template.prototype.applyTemplate;
244 <div id="method-Ext.Template-Template.from"></div>/**
245 * Creates a template from the passed element's value (<i>display:none</i> textarea, preferred) or innerHTML.
246 * @param {String/HTMLElement} el A DOM element or its id
247 * @param {Object} config A configuration object
248 * @return {Ext.Template} The created template
251 Ext.Template.from = function(el, config){
253 return new Ext.Template(el.value || el.innerHTML, config || '');