2 * Ext JS Library 2.2.1
\r
3 * Copyright(c) 2006-2009, Ext JS, LLC.
\r
4 * licensing@extjs.com
\r
6 * http://extjs.com/license
\r
10 * @class Ext.Template
\r
11 * Represents an HTML fragment template. Templates can be precompiled for greater performance.
\r
12 * For a list of available format functions, see {@link Ext.util.Format}.<br />
\r
15 var t = new Ext.Template(
\r
16 '<div name="{id}">',
\r
17 '<span class="{cls}">{name:trim} {value:ellipsis(10)}</span>',
\r
20 t.append('some-element', {id: 'myid', cls: 'myclass', name: 'foo', value: 'bar'});
\r
23 * @param {String/Array} html The HTML fragment or an array of fragments to join("") or multiple arguments to join("")
\r
25 Ext.Template = function(html){
\r
27 if(Ext.isArray(html)){
\r
28 html = html.join("");
\r
29 }else if(a.length > 1){
\r
31 for(var i = 0, len = a.length; i < len; i++){
\r
32 if(typeof a[i] == 'object'){
\r
33 Ext.apply(this, a[i]);
\r
35 buf[buf.length] = a[i];
\r
38 html = buf.join('');
\r
46 Ext.Template.prototype = {
\r
48 * Returns an HTML fragment of this template with the specified values applied.
\r
49 * @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'})
\r
50 * @return {String} The HTML fragment
\r
52 applyTemplate : function(values){
\r
54 return this.compiled(values);
\r
56 var useF = this.disableFormats !== true;
\r
57 var fm = Ext.util.Format, tpl = this;
\r
58 var fn = function(m, name, format, args){
\r
60 if(format.substr(0, 5) == "this."){
\r
61 return tpl.call(format.substr(5), values[name], values);
\r
64 // quoted values are required for strings in compiled templates,
\r
65 // but for non compiled we need to strip them
\r
66 // quoted reversed for jsmin
\r
67 var re = /^\s*['"](.*)["']\s*$/;
\r
68 args = args.split(',');
\r
69 for(var i = 0, len = args.length; i < len; i++){
\r
70 args[i] = args[i].replace(re, "$1");
\r
72 args = [values[name]].concat(args);
\r
74 args = [values[name]];
\r
76 return fm[format].apply(fm, args);
\r
79 return values[name] !== undefined ? values[name] : "";
\r
82 return this.html.replace(this.re, fn);
\r
86 * Sets the HTML used as the template and optionally compiles it.
\r
87 * @param {String} html
\r
88 * @param {Boolean} compile (optional) True to compile the template (defaults to undefined)
\r
89 * @return {Ext.Template} this
\r
91 set : function(html, compile){
\r
93 this.compiled = null;
\r
101 * True to disable format functions (defaults to false)
\r
104 disableFormats : false,
\r
107 * The regular expression used to match template variables
\r
111 re : /\{([\w-]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?\}/g,
\r
114 * Compiles the template into an internal function, eliminating the RegEx overhead.
\r
115 * @return {Ext.Template} this
\r
117 compile : function(){
\r
118 var fm = Ext.util.Format;
\r
119 var useF = this.disableFormats !== true;
\r
120 var sep = Ext.isGecko ? "+" : ",";
\r
121 var fn = function(m, name, format, args){
\r
122 if(format && useF){
\r
123 args = args ? ',' + args : "";
\r
124 if(format.substr(0, 5) != "this."){
\r
125 format = "fm." + format + '(';
\r
127 format = 'this.call("'+ format.substr(5) + '", ';
\r
131 args= ''; format = "(values['" + name + "'] == undefined ? '' : ";
\r
133 return "'"+ sep + format + "values['" + name + "']" + args + ")"+sep+"'";
\r
136 // branched to use + in gecko and [].join() in others
\r
138 body = "this.compiled = function(values){ return '" +
\r
139 this.html.replace(/\\/g, '\\\\').replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn) +
\r
142 body = ["this.compiled = function(values){ return ['"];
\r
143 body.push(this.html.replace(/\\/g, '\\\\').replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn));
\r
144 body.push("'].join('');};");
\r
145 body = body.join('');
\r
151 // private function used to call members
\r
152 call : function(fnName, value, allValues){
\r
153 return this[fnName](value, allValues);
\r
157 * Applies the supplied values to the template and inserts the new node(s) as the first child of el.
\r
158 * @param {Mixed} el The context element
\r
159 * @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'})
\r
160 * @param {Boolean} returnElement (optional) true to return a Ext.Element (defaults to undefined)
\r
161 * @return {HTMLElement/Ext.Element} The new node or Element
\r
163 insertFirst: function(el, values, returnElement){
\r
164 return this.doInsert('afterBegin', el, values, returnElement);
\r
168 * Applies the supplied values to the template and inserts the new node(s) before el.
\r
169 * @param {Mixed} el The context element
\r
170 * @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'})
\r
171 * @param {Boolean} returnElement (optional) true to return a Ext.Element (defaults to undefined)
\r
172 * @return {HTMLElement/Ext.Element} The new node or Element
\r
174 insertBefore: function(el, values, returnElement){
\r
175 return this.doInsert('beforeBegin', el, values, returnElement);
\r
179 * Applies the supplied values to the template and inserts the new node(s) after el.
\r
180 * @param {Mixed} el The context element
\r
181 * @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'})
\r
182 * @param {Boolean} returnElement (optional) true to return a Ext.Element (defaults to undefined)
\r
183 * @return {HTMLElement/Ext.Element} The new node or Element
\r
185 insertAfter : function(el, values, returnElement){
\r
186 return this.doInsert('afterEnd', el, values, returnElement);
\r
190 * Applies the supplied values to the template and appends the new node(s) to el.
\r
191 * @param {Mixed} el The context element
\r
192 * @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'})
\r
193 * @param {Boolean} returnElement (optional) true to return a Ext.Element (defaults to undefined)
\r
194 * @return {HTMLElement/Ext.Element} The new node or Element
\r
196 append : function(el, values, returnElement){
\r
197 return this.doInsert('beforeEnd', el, values, returnElement);
\r
200 doInsert : function(where, el, values, returnEl){
\r
201 el = Ext.getDom(el);
\r
202 var newNode = Ext.DomHelper.insertHtml(where, el, this.applyTemplate(values));
\r
203 return returnEl ? Ext.get(newNode, true) : newNode;
\r
207 * Applies the supplied values to the template and overwrites the content of el with the new node(s).
\r
208 * @param {Mixed} el The context element
\r
209 * @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'})
\r
210 * @param {Boolean} returnElement (optional) true to return a Ext.Element (defaults to undefined)
\r
211 * @return {HTMLElement/Ext.Element} The new node or Element
\r
213 overwrite : function(el, values, returnElement){
\r
214 el = Ext.getDom(el);
\r
215 el.innerHTML = this.applyTemplate(values);
\r
216 return returnElement ? Ext.get(el.firstChild, true) : el.firstChild;
\r
220 * Alias for {@link #applyTemplate}
\r
221 * Returns an HTML fragment of this template with the specified values applied.
\r
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'})
\r
223 * @return {String} The HTML fragment
\r
224 * @member Ext.Template
\r
227 Ext.Template.prototype.apply = Ext.Template.prototype.applyTemplate;
\r
229 // backwards compat
\r
230 Ext.DomHelper.Template = Ext.Template;
\r
233 * Creates a template from the passed element's value (<i>display:none</i> textarea, preferred) or innerHTML.
\r
234 * @param {String/HTMLElement} el A DOM element or its id
\r
235 * @param {Object} config A configuration object
\r
236 * @return {Ext.Template} The created template
\r
239 Ext.Template.from = function(el, config){
\r
240 el = Ext.getDom(el);
\r
241 return new Ext.Template(el.value || el.innerHTML, config || '');
\r