3 <title>The source code</title>
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
7 <body onload="prettyPrint();">
8 <pre class="prettyprint lang-js">/*!
10 * Copyright(c) 2006-2009 Ext JS, LLC
12 * http://www.extjs.com/license
14 <div id="cls-Ext.Template"></div>/**
16 * <p>Represents an HTML fragment template. Templates may be {@link #compile precompiled}
17 * for greater performance.</p>
18 * <p>For example usage {@link #Template see the constructor}.</p>
21 * An instance of this class may be created by passing to the constructor either
22 * a single argument, or multiple arguments:
23 * <div class="mdetail-params"><ul>
24 * <li><b>single argument</b> : String/Array
25 * <div class="sub-desc">
26 * The single argument may be either a String or an Array:<ul>
27 * <li><tt>String</tt> : </li><pre><code>
28 var t = new Ext.Template("<div>Hello {0}.</div>");
29 t.{@link #append}('some-element', ['foo']);
31 * <li><tt>Array</tt> : </li>
32 * An Array will be combined with <code>join('')</code>.
34 var t = new Ext.Template([
35 '<div name="{id}">',
36 '<span class="{cls}">{name:trim} {value:ellipsis(10)}</span>',
40 t.{@link #append}('some-element', {id: 'myid', cls: 'myclass', name: 'foo', value: 'bar'});
43 * <li><b>multiple arguments</b> : String, Object, Array, ...
44 * <div class="sub-desc">
45 * Multiple arguments will be combined with <code>join('')</code>.
47 var t = new Ext.Template(
48 '<div name="{id}">',
49 '<span class="{cls}">{name} {value}</span>',
51 // a configuration object:
53 compiled: true, // {@link #compile} immediately
54 disableFormats: true // See Notes below.
58 * <p><b>Notes</b>:</p>
59 * <div class="mdetail-params"><ul>
60 * <li>Formatting and <code>disableFormats</code> are not applicable for Ext Core.</li>
61 * <li>For a list of available format functions, see {@link Ext.util.Format}.</li>
62 * <li><code>disableFormats</code> reduces <code>{@link #apply}</code> time
63 * when no formatting is required.</li>
67 * @param {Mixed} config
69 Ext.Template = function(html){
74 if (Ext.isArray(html)) {
76 } else if (a.length > 1) {
77 Ext.each(a, function(v) {
78 if (Ext.isObject(v)) {
89 <div id="cfg-Ext.Template-compiled"></div>/**
90 * @cfg {Boolean} compiled Specify <tt>true</tt> to compile the template
91 * immediately (see <code>{@link #compile}</code>).
92 * Defaults to <tt>false</tt>.
98 Ext.Template.prototype = {
99 <div id="cfg-Ext.Template-re"></div>/**
100 * @cfg {RegExp} re The regular expression used to match template variables.
101 * Defaults to:<pre><code>
102 * re : /\{([\w-]+)\}/g // for Ext Core
103 * re : /\{([\w-]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?\}/g // for Ext JS
106 re : /\{([\w-]+)\}/g,
107 <div id="prop-Ext.Template-re"></div>/**
108 * See <code>{@link #re}</code>.
113 <div id="method-Ext.Template-applyTemplate"></div>/**
114 * Returns an HTML fragment of this template with the specified <code>values</code> applied.
115 * @param {Object/Array} values
116 * The template values. Can be an array if the params are numeric (i.e. <code>{0}</code>)
117 * or an object (i.e. <code>{foo: 'bar'}</code>).
118 * @return {String} The HTML fragment
120 applyTemplate : function(values){
124 me.compiled(values) :
125 me.html.replace(me.re, function(m, name){
126 return values[name] !== undefined ? values[name] : "";
130 <div id="method-Ext.Template-set"></div>/**
131 * Sets the HTML used as the template and optionally compiles it.
132 * @param {String} html
133 * @param {Boolean} compile (optional) True to compile the template (defaults to undefined)
134 * @return {Ext.Template} this
136 set : function(html, compile){
140 return compile ? me.compile() : me;
143 <div id="method-Ext.Template-compile"></div>/**
144 * Compiles the template into an internal function, eliminating the RegEx overhead.
145 * @return {Ext.Template} this
147 compile : function(){
149 sep = Ext.isGecko ? "+" : ",";
151 function fn(m, name){
152 name = "values['" + name + "']";
153 return "'"+ sep + '(' + name + " == undefined ? '' : " + name + ')' + sep + "'";
156 eval("this.compiled = function(values){ return " + (Ext.isGecko ? "'" : "['") +
157 me.html.replace(/\\/g, '\\\\').replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn) +
158 (Ext.isGecko ? "';};" : "'].join('');};"));
162 <div id="method-Ext.Template-insertFirst"></div>/**
163 * Applies the supplied values to the template and inserts the new node(s) as the first child of el.
164 * @param {Mixed} el The context element
165 * @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'})
166 * @param {Boolean} returnElement (optional) true to return a Ext.Element (defaults to undefined)
167 * @return {HTMLElement/Ext.Element} The new node or Element
169 insertFirst: function(el, values, returnElement){
170 return this.doInsert('afterBegin', el, values, returnElement);
173 <div id="method-Ext.Template-insertBefore"></div>/**
174 * Applies the supplied values to the template and inserts the new node(s) before el.
175 * @param {Mixed} el The context element
176 * @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'})
177 * @param {Boolean} returnElement (optional) true to return a Ext.Element (defaults to undefined)
178 * @return {HTMLElement/Ext.Element} The new node or Element
180 insertBefore: function(el, values, returnElement){
181 return this.doInsert('beforeBegin', el, values, returnElement);
184 <div id="method-Ext.Template-insertAfter"></div>/**
185 * Applies the supplied values to the template and inserts the new node(s) after el.
186 * @param {Mixed} el The context element
187 * @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'})
188 * @param {Boolean} returnElement (optional) true to return a Ext.Element (defaults to undefined)
189 * @return {HTMLElement/Ext.Element} The new node or Element
191 insertAfter : function(el, values, returnElement){
192 return this.doInsert('afterEnd', el, values, returnElement);
195 <div id="method-Ext.Template-append"></div>/**
196 * Applies the supplied <code>values</code> to the template and appends
197 * the new node(s) to the specified <code>el</code>.
198 * <p>For example usage {@link #Template see the constructor}.</p>
199 * @param {Mixed} el The context element
200 * @param {Object/Array} values
201 * The template values. Can be an array if the params are numeric (i.e. <code>{0}</code>)
202 * or an object (i.e. <code>{foo: 'bar'}</code>).
203 * @param {Boolean} returnElement (optional) true to return an Ext.Element (defaults to undefined)
204 * @return {HTMLElement/Ext.Element} The new node or Element
206 append : function(el, values, returnElement){
207 return this.doInsert('beforeEnd', el, values, returnElement);
210 doInsert : function(where, el, values, returnEl){
212 var newNode = Ext.DomHelper.insertHtml(where, el, this.applyTemplate(values));
213 return returnEl ? Ext.get(newNode, true) : newNode;
216 <div id="method-Ext.Template-overwrite"></div>/**
217 * Applies the supplied values to the template and overwrites the content of el with the new node(s).
218 * @param {Mixed} el The context element
219 * @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'})
220 * @param {Boolean} returnElement (optional) true to return a Ext.Element (defaults to undefined)
221 * @return {HTMLElement/Ext.Element} The new node or Element
223 overwrite : function(el, values, returnElement){
225 el.innerHTML = this.applyTemplate(values);
226 return returnElement ? Ext.get(el.firstChild, true) : el.firstChild;
229 <div id="method-Ext.Template-apply"></div>/**
230 * Alias for {@link #applyTemplate}
231 * Returns an HTML fragment of this template with the specified <code>values</code> applied.
232 * @param {Object/Array} values
233 * The template values. Can be an array if the params are numeric (i.e. <code>{0}</code>)
234 * or an object (i.e. <code>{foo: 'bar'}</code>).
235 * @return {String} The HTML fragment
236 * @member Ext.Template
239 Ext.Template.prototype.apply = Ext.Template.prototype.applyTemplate;
241 <div id="method-Ext.Template-Template.from"></div>/**
242 * Creates a template from the passed element's value (<i>display:none</i> textarea, preferred) or innerHTML.
243 * @param {String/HTMLElement} el A DOM element or its id
244 * @param {Object} config A configuration object
245 * @return {Ext.Template} The created template
248 Ext.Template.from = function(el, config){
250 return new Ext.Template(el.value || el.innerHTML, config || '');