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:
29 * <a href="http://www.cnitblog.com/seeyeah/archive/2011/12/30/38728.html/">DomHelper
30 - Create Elements using DOM, HTML fragments and Templates</a>.
32 * @param {Object} cfg - Configuration object.
34 Roo.Template = function(cfg){
36 if(cfg instanceof Array){
38 }else if(arguments.length > 1){
39 cfg = Array.prototype.join.call(arguments, "");
43 if (typeof(cfg) == 'object') {
54 Roo.Template.prototype = {
57 * @cfg {String} url The Url to load the template from. beware if you are loading from a url, the data may not be ready if you use it instantly..
58 * it should be fixed so that template is observable...
62 * @cfg {String} html The HTML fragment or an array of fragments to join("") or multiple arguments to join("")
66 * Returns an HTML fragment of this template with the specified values applied.
67 * @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'})
68 * @return {String} The HTML fragment
70 applyTemplate : function(values){
71 Roo.log(["applyTemplate", values]);
75 return this.compiled(values);
77 var useF = this.disableFormats !== true;
78 var fm = Roo.util.Format, tpl = this;
79 var fn = function(m, name, format, args){
81 if(format.substr(0, 5) == "this."){
82 return tpl.call(format.substr(5), values[name], values);
85 // quoted values are required for strings in compiled templates,
86 // but for non compiled we need to strip them
87 // quoted reversed for jsmin
88 var re = /^\s*['"](.*)["']\s*$/;
89 args = args.split(',');
90 for(var i = 0, len = args.length; i < len; i++){
91 args[i] = args[i].replace(re, "$1");
93 args = [values[name]].concat(args);
95 args = [values[name]];
97 return fm[format].apply(fm, args);
100 return values[name] !== undefined ? values[name] : "";
103 return this.html.replace(this.re, fn);
122 this.compiled = false;
124 var cx = new Roo.data.Connection();
128 success : function (response) {
130 _t.html = response.responseText;
134 failure : function(response) {
135 Roo.log("Template failed to load from " + _t.url);
142 * Sets the HTML used as the template and optionally compiles it.
143 * @param {String} html
144 * @param {Boolean} compile (optional) True to compile the template (defaults to undefined)
145 * @return {Roo.Template} this
147 set : function(html, compile){
149 this.compiled = null;
157 * True to disable format functions (defaults to false)
160 disableFormats : false,
163 * The regular expression used to match template variables
167 re : /\{([\w-]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?\}/g,
170 * Compiles the template into an internal function, eliminating the RegEx overhead.
171 * @return {Roo.Template} this
173 compile : function(){
174 var fm = Roo.util.Format;
175 var useF = this.disableFormats !== true;
176 var sep = Roo.isGecko ? "+" : ",";
177 var fn = function(m, name, format, args){
179 args = args ? ',' + args : "";
180 if(format.substr(0, 5) != "this."){
181 format = "fm." + format + '(';
183 format = 'this.call("'+ format.substr(5) + '", ';
187 args= ''; format = "(values['" + name + "'] == undefined ? '' : ";
189 return "'"+ sep + format + "values['" + name + "']" + args + ")"+sep+"'";
192 // branched to use + in gecko and [].join() in others
194 body = "this.compiled = function(values){ return '" +
195 this.html.replace(/\\/g, '\\\\').replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn) +
198 body = ["this.compiled = function(values){ return ['"];
199 body.push(this.html.replace(/\\/g, '\\\\').replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn));
200 body.push("'].join('');};");
201 body = body.join('');
211 // private function used to call members
212 call : function(fnName, value, allValues){
213 return this[fnName](value, allValues);
217 * Applies the supplied values to the template and inserts the new node(s) as the first child of el.
218 * @param {String/HTMLElement/Roo.Element} el The context element
219 * @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'})
220 * @param {Boolean} returnElement (optional) true to return a Roo.Element (defaults to undefined)
221 * @return {HTMLElement/Roo.Element} The new node or Element
223 insertFirst: function(el, values, returnElement){
224 return this.doInsert('afterBegin', el, values, returnElement);
228 * Applies the supplied values to the template and inserts the new node(s) before el.
229 * @param {String/HTMLElement/Roo.Element} el The context element
230 * @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'})
231 * @param {Boolean} returnElement (optional) true to return a Roo.Element (defaults to undefined)
232 * @return {HTMLElement/Roo.Element} The new node or Element
234 insertBefore: function(el, values, returnElement){
235 return this.doInsert('beforeBegin', el, values, returnElement);
239 * Applies the supplied values to the template and inserts the new node(s) after el.
240 * @param {String/HTMLElement/Roo.Element} el The context element
241 * @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'})
242 * @param {Boolean} returnElement (optional) true to return a Roo.Element (defaults to undefined)
243 * @return {HTMLElement/Roo.Element} The new node or Element
245 insertAfter : function(el, values, returnElement){
246 return this.doInsert('afterEnd', el, values, returnElement);
250 * Applies the supplied values to the template and appends the new node(s) to el.
251 * @param {String/HTMLElement/Roo.Element} el The context element
252 * @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'})
253 * @param {Boolean} returnElement (optional) true to return a Roo.Element (defaults to undefined)
254 * @return {HTMLElement/Roo.Element} The new node or Element
256 append : function(el, values, returnElement){
257 return this.doInsert('beforeEnd', el, values, returnElement);
260 doInsert : function(where, el, values, returnEl){
262 var newNode = Roo.DomHelper.insertHtml(where, el, this.applyTemplate(values));
263 return returnEl ? Roo.get(newNode, true) : newNode;
267 * Applies the supplied values to the template and overwrites the content of el with the new node(s).
268 * @param {String/HTMLElement/Roo.Element} el The context element
269 * @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'})
270 * @param {Boolean} returnElement (optional) true to return a Roo.Element (defaults to undefined)
271 * @return {HTMLElement/Roo.Element} The new node or Element
273 overwrite : function(el, values, returnElement){
275 el.innerHTML = this.applyTemplate(values);
276 return returnElement ? Roo.get(el.firstChild, true) : el.firstChild;
280 * Alias for {@link #applyTemplate}
283 Roo.Template.prototype.apply = Roo.Template.prototype.applyTemplate;
286 Roo.DomHelper.Template = Roo.Template;
289 * Creates a template from the passed element's value (<i>display:none</i> textarea, preferred) or innerHTML.
290 * @param {String/HTMLElement} el A DOM element or its id
291 * @returns {Roo.Template} The created template
294 Roo.Template.from = function(el){
296 return new Roo.Template(el.value || el.innerHTML);