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({
20 '<div name="{id}">',
21 '<span class="{cls}">{name:trim} {value:ellipsis(10)}</span>',
24 t.append('some-element', {id: 'myid', cls: 'myclass', name: 'foo', value: 'bar'});
26 * 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>.
28 * @param {Object} cfg - Configuration object.
29 * @param {String/Array} html
31 Roo.Template = function(cfg){
33 if(cfg instanceof Array){
35 }else if(arguments.length > 1){
36 cfg = Array.prototype.join.call(arguments, "");
40 if (typeof(cfg) == 'object') {
49 Roo.Template.prototype = {
52 * @cfg {String} html The HTML fragment or an array of fragments to join("") or multiple arguments to join("")
56 * Returns an HTML fragment of this template with the specified values applied.
57 * @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'})
58 * @return {String} The HTML fragment
60 applyTemplate : function(values){
64 return this.compiled(values);
66 var useF = this.disableFormats !== true;
67 var fm = Roo.util.Format, tpl = this;
68 var fn = function(m, name, format, args){
70 if(format.substr(0, 5) == "this."){
71 return tpl.call(format.substr(5), values[name], values);
74 // quoted values are required for strings in compiled templates,
75 // but for non compiled we need to strip them
76 // quoted reversed for jsmin
77 var re = /^\s*['"](.*)["']\s*$/;
78 args = args.split(',');
79 for(var i = 0, len = args.length; i < len; i++){
80 args[i] = args[i].replace(re, "$1");
82 args = [values[name]].concat(args);
84 args = [values[name]];
86 return fm[format].apply(fm, args);
89 return values[name] !== undefined ? values[name] : "";
92 return this.html.replace(this.re, fn);
101 * Sets the HTML used as the template and optionally compiles it.
102 * @param {String} html
103 * @param {Boolean} compile (optional) True to compile the template (defaults to undefined)
104 * @return {Roo.Template} this
106 set : function(html, compile){
108 this.compiled = null;
116 * True to disable format functions (defaults to false)
119 disableFormats : false,
122 * The regular expression used to match template variables
126 re : /\{([\w-]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?\}/g,
129 * Compiles the template into an internal function, eliminating the RegEx overhead.
130 * @return {Roo.Template} this
132 compile : function(){
133 var fm = Roo.util.Format;
134 var useF = this.disableFormats !== true;
135 var sep = Roo.isGecko ? "+" : ",";
136 var fn = function(m, name, format, args){
138 args = args ? ',' + args : "";
139 if(format.substr(0, 5) != "this."){
140 format = "fm." + format + '(';
142 format = 'this.call("'+ format.substr(5) + '", ';
146 args= ''; format = "(values['" + name + "'] == undefined ? '' : ";
148 return "'"+ sep + format + "values['" + name + "']" + args + ")"+sep+"'";
151 // branched to use + in gecko and [].join() in others
153 body = "this.compiled = function(values){ return '" +
154 this.html.replace(/\\/g, '\\\\').replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn) +
157 body = ["this.compiled = function(values){ return ['"];
158 body.push(this.html.replace(/\\/g, '\\\\').replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn));
159 body.push("'].join('');};");
160 body = body.join('');
170 // private function used to call members
171 call : function(fnName, value, allValues){
172 return this[fnName](value, allValues);
176 * Applies the supplied values to the template and inserts the new node(s) as the first child of el.
177 * @param {String/HTMLElement/Roo.Element} el The context element
178 * @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'})
179 * @param {Boolean} returnElement (optional) true to return a Roo.Element (defaults to undefined)
180 * @return {HTMLElement/Roo.Element} The new node or Element
182 insertFirst: function(el, values, returnElement){
183 return this.doInsert('afterBegin', el, values, returnElement);
187 * Applies the supplied values to the template and inserts the new node(s) before el.
188 * @param {String/HTMLElement/Roo.Element} el The context element
189 * @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'})
190 * @param {Boolean} returnElement (optional) true to return a Roo.Element (defaults to undefined)
191 * @return {HTMLElement/Roo.Element} The new node or Element
193 insertBefore: function(el, values, returnElement){
194 return this.doInsert('beforeBegin', el, values, returnElement);
198 * Applies the supplied values to the template and inserts the new node(s) after el.
199 * @param {String/HTMLElement/Roo.Element} el The context element
200 * @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'})
201 * @param {Boolean} returnElement (optional) true to return a Roo.Element (defaults to undefined)
202 * @return {HTMLElement/Roo.Element} The new node or Element
204 insertAfter : function(el, values, returnElement){
205 return this.doInsert('afterEnd', el, values, returnElement);
209 * Applies the supplied values to the template and appends the new node(s) to el.
210 * @param {String/HTMLElement/Roo.Element} el The context element
211 * @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'})
212 * @param {Boolean} returnElement (optional) true to return a Roo.Element (defaults to undefined)
213 * @return {HTMLElement/Roo.Element} The new node or Element
215 append : function(el, values, returnElement){
216 return this.doInsert('beforeEnd', el, values, returnElement);
219 doInsert : function(where, el, values, returnEl){
221 var newNode = Roo.DomHelper.insertHtml(where, el, this.applyTemplate(values));
222 return returnEl ? Roo.get(newNode, true) : newNode;
226 * Applies the supplied values to the template and overwrites the content of el with the new node(s).
227 * @param {String/HTMLElement/Roo.Element} el The context element
228 * @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'})
229 * @param {Boolean} returnElement (optional) true to return a Roo.Element (defaults to undefined)
230 * @return {HTMLElement/Roo.Element} The new node or Element
232 overwrite : function(el, values, returnElement){
234 el.innerHTML = this.applyTemplate(values);
235 return returnElement ? Roo.get(el.firstChild, true) : el.firstChild;
239 * Alias for {@link #applyTemplate}
242 Roo.Template.prototype.apply = Roo.Template.prototype.applyTemplate;
245 Roo.DomHelper.Template = Roo.Template;
248 * Creates a template from the passed element's value (<i>display:none</i> textarea, preferred) or innerHTML.
249 * @param {String/HTMLElement} el A DOM element or its id
250 * @returns {Roo.Template} The created template
253 Roo.Template.from = function(el){
255 return new Roo.Template(el.value || el.innerHTML);