4 * Copyright(c) 2006-2007, Ext JS, LLC.
6 * Originally Released Under LGPL - original licence link has changed is not relivant.
9 * <script type="text/javascript">
14 * Represents an HTML fragment template. Templates can be precompiled for greater performance.
15 * For a list of available format functions, see {@link Roo.util.Format}.<br />
18 var t = new Roo.Template({
19 html : '<div name="{id}">' +
20 '<span class="{cls}">{name:trim} {someval:this.myformat}{value:ellipsis(10)}</span>' +
22 myformat: function (value, allValues) {
26 t.append('some-element', {id: 'myid', cls: 'myclass', name: 'foo', value: 'bar'});
28 * For more information see this blog post with examples: <a href="http://www.jackslocum.com/yui/2006/10/06/domhelper-create-elements-using-dom-html-fragments-or-templates/">DomHelper - Create Elements using DOM, HTML fragments and Templates</a>.
30 * @param {Object} cfg - Configuration object.
32 Roo.Template = function(cfg){
34 if(cfg instanceof Array){
36 }else if(arguments.length > 1){
37 cfg = Array.prototype.join.call(arguments, "");
41 if (typeof(cfg) == 'object') {
50 Roo.Template.prototype = {
53 * @cfg {String} html The HTML fragment or an array of fragments to join("") or multiple arguments to join("")
57 * Returns an HTML fragment of this template with the specified values applied.
58 * @param {Object} values The template values. Can be an array if your params are numeric (i.e. {0}) or an object (i.e. {foo: 'bar'})
59 * @return {String} The HTML fragment
61 applyTemplate : function(values){
65 return this.compiled(values);
67 var useF = this.disableFormats !== true;
68 var fm = Roo.util.Format, tpl = this;
69 var fn = function(m, name, format, args){
71 if(format.substr(0, 5) == "this."){
72 return tpl.call(format.substr(5), values[name], values);
75 // quoted values are required for strings in compiled templates,
76 // but for non compiled we need to strip them
77 // quoted reversed for jsmin
78 var re = /^\s*['"](.*)["']\s*$/;
79 args = args.split(',');
80 for(var i = 0, len = args.length; i < len; i++){
81 args[i] = args[i].replace(re, "$1");
83 args = [values[name]].concat(args);
85 args = [values[name]];
87 return fm[format].apply(fm, args);
90 return values[name] !== undefined ? values[name] : "";
93 return this.html.replace(this.re, fn);
102 * Sets the HTML used as the template and optionally compiles it.
103 * @param {String} html
104 * @param {Boolean} compile (optional) True to compile the template (defaults to undefined)
105 * @return {Roo.Template} this
107 set : function(html, compile){
109 this.compiled = null;
117 * True to disable format functions (defaults to false)
120 disableFormats : false,
123 * The regular expression used to match template variables
127 re : /\{([\w-]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?\}/g,
130 * Compiles the template into an internal function, eliminating the RegEx overhead.
131 * @return {Roo.Template} this
133 compile : function(){
134 var fm = Roo.util.Format;
135 var useF = this.disableFormats !== true;
136 var sep = Roo.isGecko ? "+" : ",";
137 var fn = function(m, name, format, args){
139 args = args ? ',' + args : "";
140 if(format.substr(0, 5) != "this."){
141 format = "fm." + format + '(';
143 format = 'this.call("'+ format.substr(5) + '", ';
147 args= ''; format = "(values['" + name + "'] == undefined ? '' : ";
149 return "'"+ sep + format + "values['" + name + "']" + args + ")"+sep+"'";
152 // branched to use + in gecko and [].join() in others
154 body = "this.compiled = function(values){ return '" +
155 this.html.replace(/\\/g, '\\\\').replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn) +
158 body = ["this.compiled = function(values){ return ['"];
159 body.push(this.html.replace(/\\/g, '\\\\').replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn));
160 body.push("'].join('');};");
161 body = body.join('');
171 // private function used to call members
172 call : function(fnName, value, allValues){
173 return this[fnName](value, allValues);
177 * Applies the supplied values to the template and inserts the new node(s) as the first child of el.
178 * @param {String/HTMLElement/Roo.Element} el The context element
179 * @param {Object} 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 Roo.Element (defaults to undefined)
181 * @return {HTMLElement/Roo.Element} The new node or Element
183 insertFirst: function(el, values, returnElement){
184 return this.doInsert('afterBegin', el, values, returnElement);
188 * Applies the supplied values to the template and inserts the new node(s) before el.
189 * @param {String/HTMLElement/Roo.Element} el The context element
190 * @param {Object} 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 Roo.Element (defaults to undefined)
192 * @return {HTMLElement/Roo.Element} The new node or Element
194 insertBefore: function(el, values, returnElement){
195 return this.doInsert('beforeBegin', el, values, returnElement);
199 * Applies the supplied values to the template and inserts the new node(s) after el.
200 * @param {String/HTMLElement/Roo.Element} el The context element
201 * @param {Object} values The template values. Can be an array if your params are numeric (i.e. {0}) or an object (i.e. {foo: 'bar'})
202 * @param {Boolean} returnElement (optional) true to return a Roo.Element (defaults to undefined)
203 * @return {HTMLElement/Roo.Element} The new node or Element
205 insertAfter : function(el, values, returnElement){
206 return this.doInsert('afterEnd', el, values, returnElement);
210 * Applies the supplied values to the template and appends the new node(s) to el.
211 * @param {String/HTMLElement/Roo.Element} el The context element
212 * @param {Object} values The template values. Can be an array if your params are numeric (i.e. {0}) or an object (i.e. {foo: 'bar'})
213 * @param {Boolean} returnElement (optional) true to return a Roo.Element (defaults to undefined)
214 * @return {HTMLElement/Roo.Element} The new node or Element
216 append : function(el, values, returnElement){
217 return this.doInsert('beforeEnd', el, values, returnElement);
220 doInsert : function(where, el, values, returnEl){
222 var newNode = Roo.DomHelper.insertHtml(where, el, this.applyTemplate(values));
223 return returnEl ? Roo.get(newNode, true) : newNode;
227 * Applies the supplied values to the template and overwrites the content of el with the new node(s).
228 * @param {String/HTMLElement/Roo.Element} el The context element
229 * @param {Object} 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 Roo.Element (defaults to undefined)
231 * @return {HTMLElement/Roo.Element} The new node or Element
233 overwrite : function(el, values, returnElement){
235 el.innerHTML = this.applyTemplate(values);
236 return returnElement ? Roo.get(el.firstChild, true) : el.firstChild;
240 * Alias for {@link #applyTemplate}
243 Roo.Template.prototype.apply = Roo.Template.prototype.applyTemplate;
246 Roo.DomHelper.Template = Roo.Template;
249 * Creates a template from the passed element's value (<i>display:none</i> textarea, preferred) or innerHTML.
250 * @param {String/HTMLElement} el A DOM element or its id
251 * @returns {Roo.Template} The created template
254 Roo.Template.from = function(el){
256 return new Roo.Template(el.value || el.innerHTML);