3 * <p>Represents an HTML fragment template. Templates may be {@link #compile precompiled}
4 * for greater performance.</p>
5 * An instance of this class may be created by passing to the constructor either
6 * a single argument, or multiple arguments:
7 * <div class="mdetail-params"><ul>
8 * <li><b>single argument</b> : String/Array
9 * <div class="sub-desc">
10 * The single argument may be either a String or an Array:<ul>
11 * <li><tt>String</tt> : </li><pre><code>
12 var t = new Ext.Template("<div>Hello {0}.</div>");
13 t.{@link #append}('some-element', ['foo']);
15 * <li><tt>Array</tt> : </li>
16 * An Array will be combined with <code>join('')</code>.
18 var t = new Ext.Template([
19 '<div name="{id}">',
20 '<span class="{cls}">{name:trim} {value:ellipsis(10)}</span>',
24 t.{@link #append}('some-element', {id: 'myid', cls: 'myclass', name: 'foo', value: 'bar'});
27 * <li><b>multiple arguments</b> : String, Object, Array, ...
28 * <div class="sub-desc">
29 * Multiple arguments will be combined with <code>join('')</code>.
31 var t = new Ext.Template(
32 '<div name="{id}">',
33 '<span class="{cls}">{name} {value}</span>',
35 // a configuration object:
37 compiled: true, // {@link #compile} immediately
41 * <p><b>Notes</b>:</p>
42 * <div class="mdetail-params"><ul>
43 * <li>For a list of available format functions, see {@link Ext.util.Format}.</li>
44 * <li><code>disableFormats</code> reduces <code>{@link #apply}</code> time
45 * when no formatting is required.</li>
49 * @param {Mixed} config
52 Ext.define('Ext.Template', {
54 /* Begin Definitions */
56 requires: ['Ext.core.DomHelper', 'Ext.util.Format'],
60 * Creates a template from the passed element's value (<i>display:none</i> textarea, preferred) or innerHTML.
61 * @param {String/HTMLElement} el A DOM element or its id
62 * @param {Object} config A configuration object
63 * @return {Ext.Template} The created template
66 from: function(el, config) {
68 return new this(el.value || el.innerHTML, config || '');
74 constructor: function(html) {
82 me.initialConfig = {};
85 for (; i < length; i++) {
87 if (typeof value == 'object') {
88 Ext.apply(me.initialConfig, value);
94 html = buffer.join('');
96 if (Ext.isArray(html)) {
97 buffer.push(html.join(''));
104 me.html = buffer.join('');
112 * @cfg {Boolean} disableFormats true to disable format functions in the template. If the template doesn't contain format functions, setting
113 * disableFormats to true will reduce apply time (defaults to false)
115 disableFormats: false,
117 re: /\{([\w\-]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?\}/g,
119 * Returns an HTML fragment of this template with the specified values applied.
120 * @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'})
121 * @return {String} The HTML fragment
124 applyTemplate: function(values) {
126 useFormat = me.disableFormats !== true,
127 fm = Ext.util.Format,
131 return me.compiled(values);
133 function fn(m, name, format, args) {
134 if (format && useFormat) {
136 args = [values[name]].concat(Ext.functionFactory('return ['+ args +'];')());
138 args = [values[name]];
140 if (format.substr(0, 5) == "this.") {
141 return tpl[format.substr(5)].apply(tpl, args);
144 return fm[format].apply(fm, args);
148 return values[name] !== undefined ? values[name] : "";
151 return me.html.replace(me.re, fn);
155 * Sets the HTML used as the template and optionally compiles it.
156 * @param {String} html
157 * @param {Boolean} compile (optional) True to compile the template (defaults to undefined)
158 * @return {Ext.Template} this
160 set: function(html, compile) {
164 return compile ? me.compile() : me;
168 compileBRe: /(\r\n|\n)/g,
171 * Compiles the template into an internal function, eliminating the RegEx overhead.
172 * @return {Ext.Template} this
175 compile: function() {
177 fm = Ext.util.Format,
178 useFormat = me.disableFormats !== true,
181 function fn(m, name, format, args) {
182 if (format && useFormat) {
183 args = args ? ',' + args: "";
184 if (format.substr(0, 5) != "this.") {
185 format = "fm." + format + '(';
188 format = 'this.' + format.substr(5) + '(';
193 format = "(values['" + name + "'] == undefined ? '' : ";
195 return "'," + format + "values['" + name + "']" + args + ") ,'";
198 bodyReturn = me.html.replace(me.compileARe, '\\\\').replace(me.compileBRe, '\\n').replace(me.compileCRe, "\\'").replace(me.re, fn);
199 body = "this.compiled = function(values){ return ['" + bodyReturn + "'].join('');};";
205 * Applies the supplied values to the template and inserts the new node(s) as the first child of el.
206 * @param {Mixed} el The context element
207 * @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'})
208 * @param {Boolean} returnElement (optional) true to return a Ext.core.Element (defaults to undefined)
209 * @return {HTMLElement/Ext.core.Element} The new node or Element
211 insertFirst: function(el, values, returnElement) {
212 return this.doInsert('afterBegin', el, values, returnElement);
216 * Applies the supplied values to the template and inserts the new node(s) before el.
217 * @param {Mixed} el The context element
218 * @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'})
219 * @param {Boolean} returnElement (optional) true to return a Ext.core.Element (defaults to undefined)
220 * @return {HTMLElement/Ext.core.Element} The new node or Element
222 insertBefore: function(el, values, returnElement) {
223 return this.doInsert('beforeBegin', el, values, returnElement);
227 * Applies the supplied values to the template and inserts the new node(s) after el.
228 * @param {Mixed} el The context element
229 * @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'})
230 * @param {Boolean} returnElement (optional) true to return a Ext.core.Element (defaults to undefined)
231 * @return {HTMLElement/Ext.core.Element} The new node or Element
233 insertAfter: function(el, values, returnElement) {
234 return this.doInsert('afterEnd', el, values, returnElement);
238 * Applies the supplied <code>values</code> to the template and appends
239 * the new node(s) to the specified <code>el</code>.
240 * <p>For example usage {@link #Template see the constructor}.</p>
241 * @param {Mixed} el The context element
242 * @param {Object/Array} values
243 * The template values. Can be an array if the params are numeric (i.e. <code>{0}</code>)
244 * or an object (i.e. <code>{foo: 'bar'}</code>).
245 * @param {Boolean} returnElement (optional) true to return an Ext.core.Element (defaults to undefined)
246 * @return {HTMLElement/Ext.core.Element} The new node or Element
248 append: function(el, values, returnElement) {
249 return this.doInsert('beforeEnd', el, values, returnElement);
252 doInsert: function(where, el, values, returnEl) {
254 var newNode = Ext.core.DomHelper.insertHtml(where, el, this.applyTemplate(values));
255 return returnEl ? Ext.get(newNode, true) : newNode;
259 * Applies the supplied values to the template and overwrites the content of el with the new node(s).
260 * @param {Mixed} el The context element
261 * @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'})
262 * @param {Boolean} returnElement (optional) true to return a Ext.core.Element (defaults to undefined)
263 * @return {HTMLElement/Ext.core.Element} The new node or Element
265 overwrite: function(el, values, returnElement) {
267 el.innerHTML = this.applyTemplate(values);
268 return returnElement ? Ext.get(el.firstChild, true) : el.firstChild;
273 * Alias for {@link #applyTemplate}
274 * Returns an HTML fragment of this template with the specified <code>values</code> applied.
275 * @param {Object/Array} values
276 * The template values. Can be an array if the params are numeric (i.e. <code>{0}</code>)
277 * or an object (i.e. <code>{foo: 'bar'}</code>).
278 * @return {String} The HTML fragment
279 * @member Ext.Template
282 this.createAlias('apply', 'applyTemplate');