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 '<div name="{id}">',
20 '<span class="{cls}">{name:trim} {value:ellipsis(10)}</span>',
23 t.append('some-element', {id: 'myid', cls: 'myclass', name: 'foo', value: 'bar'});
25 * 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>.
27 * @param {String/Array} html The HTML fragment or an array of fragments to join("") or multiple arguments to join("")
29 Roo.Template = function(html){
30 if(html instanceof Array){
32 }else if(arguments.length > 1){
33 html = Array.prototype.join.call(arguments, "");
39 Roo.Template.prototype = {
41 * Returns an HTML fragment of this template with the specified values applied.
42 * @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'})
43 * @return {String} The HTML fragment
45 applyTemplate : function(values){
49 return this.compiled(values);
51 var useF = this.disableFormats !== true;
52 var fm = Roo.util.Format, tpl = this;
53 var fn = function(m, name, format, args){
55 if(format.substr(0, 5) == "this."){
56 return tpl.call(format.substr(5), values[name], values);
59 // quoted values are required for strings in compiled templates,
60 // but for non compiled we need to strip them
61 // quoted reversed for jsmin
62 var re = /^\s*['"](.*)["']\s*$/;
63 args = args.split(',');
64 for(var i = 0, len = args.length; i < len; i++){
65 args[i] = args[i].replace(re, "$1");
67 args = [values[name]].concat(args);
69 args = [values[name]];
71 return fm[format].apply(fm, args);
74 return values[name] !== undefined ? values[name] : "";
77 return this.html.replace(this.re, fn);
86 * Sets the HTML used as the template and optionally compiles it.
87 * @param {String} html
88 * @param {Boolean} compile (optional) True to compile the template (defaults to undefined)
89 * @return {Roo.Template} this
91 set : function(html, compile){
101 * True to disable format functions (defaults to false)
104 disableFormats : false,
107 * The regular expression used to match template variables
111 re : /\{([\w-]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?\}/g,
114 * Compiles the template into an internal function, eliminating the RegEx overhead.
115 * @return {Roo.Template} this
117 compile : function(){
118 var fm = Roo.util.Format;
119 var useF = this.disableFormats !== true;
120 var sep = Roo.isGecko ? "+" : ",";
121 var fn = function(m, name, format, args){
123 args = args ? ',' + args : "";
124 if(format.substr(0, 5) != "this."){
125 format = "fm." + format + '(';
127 format = 'this.call("'+ format.substr(5) + '", ';
131 args= ''; format = "(values['" + name + "'] == undefined ? '' : ";
133 return "'"+ sep + format + "values['" + name + "']" + args + ")"+sep+"'";
136 // branched to use + in gecko and [].join() in others
138 body = "this.compiled = function(values){ return '" +
139 this.html.replace(/\\/g, '\\\\').replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn) +
142 body = ["this.compiled = function(values){ return ['"];
143 body.push(this.html.replace(/\\/g, '\\\\').replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn));
144 body.push("'].join('');};");
145 body = body.join('');
155 // private function used to call members
156 call : function(fnName, value, allValues){
157 return this[fnName](value, allValues);
161 * Applies the supplied values to the template and inserts the new node(s) as the first child of el.
162 * @param {String/HTMLElement/Roo.Element} el The context element
163 * @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'})
164 * @param {Boolean} returnElement (optional) true to return a Roo.Element (defaults to undefined)
165 * @return {HTMLElement/Roo.Element} The new node or Element
167 insertFirst: function(el, values, returnElement){
168 return this.doInsert('afterBegin', el, values, returnElement);
172 * Applies the supplied values to the template and inserts the new node(s) before el.
173 * @param {String/HTMLElement/Roo.Element} el The context element
174 * @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'})
175 * @param {Boolean} returnElement (optional) true to return a Roo.Element (defaults to undefined)
176 * @return {HTMLElement/Roo.Element} The new node or Element
178 insertBefore: function(el, values, returnElement){
179 return this.doInsert('beforeBegin', el, values, returnElement);
183 * Applies the supplied values to the template and inserts the new node(s) after el.
184 * @param {String/HTMLElement/Roo.Element} el The context element
185 * @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'})
186 * @param {Boolean} returnElement (optional) true to return a Roo.Element (defaults to undefined)
187 * @return {HTMLElement/Roo.Element} The new node or Element
189 insertAfter : function(el, values, returnElement){
190 return this.doInsert('afterEnd', el, values, returnElement);
194 * Applies the supplied values to the template and appends the new node(s) to el.
195 * @param {String/HTMLElement/Roo.Element} el The context element
196 * @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'})
197 * @param {Boolean} returnElement (optional) true to return a Roo.Element (defaults to undefined)
198 * @return {HTMLElement/Roo.Element} The new node or Element
200 append : function(el, values, returnElement){
201 return this.doInsert('beforeEnd', el, values, returnElement);
204 doInsert : function(where, el, values, returnEl){
206 var newNode = Roo.DomHelper.insertHtml(where, el, this.applyTemplate(values));
207 return returnEl ? Roo.get(newNode, true) : newNode;
211 * Applies the supplied values to the template and overwrites the content of el with the new node(s).
212 * @param {String/HTMLElement/Roo.Element} el The context element
213 * @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'})
214 * @param {Boolean} returnElement (optional) true to return a Roo.Element (defaults to undefined)
215 * @return {HTMLElement/Roo.Element} The new node or Element
217 overwrite : function(el, values, returnElement){
219 el.innerHTML = this.applyTemplate(values);
220 return returnElement ? Roo.get(el.firstChild, true) : el.firstChild;
224 * Alias for {@link #applyTemplate}
227 Roo.Template.prototype.apply = Roo.Template.prototype.applyTemplate;
230 Roo.DomHelper.Template = Roo.Template;
233 * Creates a template from the passed element's value (<i>display:none</i> textarea, preferred) or innerHTML.
234 * @param {String/HTMLElement} el A DOM element or its id
235 * @returns {Roo.Template} The created template
238 Roo.Template.from = function(el){
240 return new Roo.Template(el.value || el.innerHTML);