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') {
52 Roo.Template.prototype = {
55 * @cfg {String} url The Url to load the template from.
59 * @cfg {String} html The HTML fragment or an array of fragments to join("") or multiple arguments to join("")
63 * Returns an HTML fragment of this template with the specified values applied.
64 * @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'})
65 * @return {String} The HTML fragment
67 applyTemplate : function(values){
71 this.afterLoad = function () { _t.applyTemplate(values); };
76 return this.compiled(values);
78 var useF = this.disableFormats !== true;
79 var fm = Roo.util.Format, tpl = this;
80 var fn = function(m, name, format, args){
82 if(format.substr(0, 5) == "this."){
83 return tpl.call(format.substr(5), values[name], values);
86 // quoted values are required for strings in compiled templates,
87 // but for non compiled we need to strip them
88 // quoted reversed for jsmin
89 var re = /^\s*['"](.*)["']\s*$/;
90 args = args.split(',');
91 for(var i = 0, len = args.length; i < len; i++){
92 args[i] = args[i].replace(re, "$1");
94 args = [values[name]].concat(args);
96 args = [values[name]];
98 return fm[format].apply(fm, args);
101 return values[name] !== undefined ? values[name] : "";
104 return this.html.replace(this.re, fn);
125 this.compiled = false;
127 var cx = new Roo.data.Connection();
131 success : function (response) {
133 _t.html = response.responseText;
134 _t.afterLoad && _t.afterLoad();
136 failure : function(response) {
137 Roo.log("Template failed to load from " + url);
144 * Sets the HTML used as the template and optionally compiles it.
145 * @param {String} html
146 * @param {Boolean} compile (optional) True to compile the template (defaults to undefined)
147 * @return {Roo.Template} this
149 set : function(html, compile){
151 this.compiled = null;
159 * True to disable format functions (defaults to false)
162 disableFormats : false,
165 * The regular expression used to match template variables
169 re : /\{([\w-]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?\}/g,
172 * Compiles the template into an internal function, eliminating the RegEx overhead.
173 * @return {Roo.Template} this
175 compile : function(){
176 var fm = Roo.util.Format;
177 var useF = this.disableFormats !== true;
178 var sep = Roo.isGecko ? "+" : ",";
179 var fn = function(m, name, format, args){
181 args = args ? ',' + args : "";
182 if(format.substr(0, 5) != "this."){
183 format = "fm." + format + '(';
185 format = 'this.call("'+ format.substr(5) + '", ';
189 args= ''; format = "(values['" + name + "'] == undefined ? '' : ";
191 return "'"+ sep + format + "values['" + name + "']" + args + ")"+sep+"'";
194 // branched to use + in gecko and [].join() in others
196 body = "this.compiled = function(values){ return '" +
197 this.html.replace(/\\/g, '\\\\').replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn) +
200 body = ["this.compiled = function(values){ return ['"];
201 body.push(this.html.replace(/\\/g, '\\\\').replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn));
202 body.push("'].join('');};");
203 body = body.join('');
213 // private function used to call members
214 call : function(fnName, value, allValues){
215 return this[fnName](value, allValues);
219 * Applies the supplied values to the template and inserts the new node(s) as the first child of el.
220 * @param {String/HTMLElement/Roo.Element} el The context element
221 * @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'})
222 * @param {Boolean} returnElement (optional) true to return a Roo.Element (defaults to undefined)
223 * @return {HTMLElement/Roo.Element} The new node or Element
225 insertFirst: function(el, values, returnElement){
226 return this.doInsert('afterBegin', el, values, returnElement);
230 * Applies the supplied values to the template and inserts the new node(s) before el.
231 * @param {String/HTMLElement/Roo.Element} el The context element
232 * @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'})
233 * @param {Boolean} returnElement (optional) true to return a Roo.Element (defaults to undefined)
234 * @return {HTMLElement/Roo.Element} The new node or Element
236 insertBefore: function(el, values, returnElement){
237 return this.doInsert('beforeBegin', el, values, returnElement);
241 * Applies the supplied values to the template and inserts the new node(s) after el.
242 * @param {String/HTMLElement/Roo.Element} el The context element
243 * @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'})
244 * @param {Boolean} returnElement (optional) true to return a Roo.Element (defaults to undefined)
245 * @return {HTMLElement/Roo.Element} The new node or Element
247 insertAfter : function(el, values, returnElement){
248 return this.doInsert('afterEnd', el, values, returnElement);
252 * Applies the supplied values to the template and appends the new node(s) to el.
253 * @param {String/HTMLElement/Roo.Element} el The context element
254 * @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'})
255 * @param {Boolean} returnElement (optional) true to return a Roo.Element (defaults to undefined)
256 * @return {HTMLElement/Roo.Element} The new node or Element
258 append : function(el, values, returnElement){
259 return this.doInsert('beforeEnd', el, values, returnElement);
262 doInsert : function(where, el, values, returnEl){
264 var newNode = Roo.DomHelper.insertHtml(where, el, this.applyTemplate(values));
265 return returnEl ? Roo.get(newNode, true) : newNode;
269 * Applies the supplied values to the template and overwrites the content of el with the new node(s).
270 * @param {String/HTMLElement/Roo.Element} el The context element
271 * @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'})
272 * @param {Boolean} returnElement (optional) true to return a Roo.Element (defaults to undefined)
273 * @return {HTMLElement/Roo.Element} The new node or Element
275 overwrite : function(el, values, returnElement){
277 el.innerHTML = this.applyTemplate(values);
278 return returnElement ? Roo.get(el.firstChild, true) : el.firstChild;
282 * Alias for {@link #applyTemplate}
285 Roo.Template.prototype.apply = Roo.Template.prototype.applyTemplate;
288 Roo.DomHelper.Template = Roo.Template;
291 * Creates a template from the passed element's value (<i>display:none</i> textarea, preferred) or innerHTML.
292 * @param {String/HTMLElement} el A DOM element or its id
293 * @returns {Roo.Template} The created template
296 Roo.Template.from = function(el){
298 return new Roo.Template(el.value || el.innerHTML);