3 * Copyright(c) 2006-2009 Ext JS, LLC
5 * http://www.extjs.com/license
8 * @class Ext.Template
\r
10 Ext.apply(Ext.Template.prototype, {
\r
12 * Returns an HTML fragment of this template with the specified values applied.
\r
13 * @param {Object/Array} values The template values. Can be an array if your params are numeric (i.e. {0}) or an object (i.e. {foo: 'bar'})
\r
14 * @return {String} The HTML fragment
\r
17 applyTemplate : function(values){
\r
19 useF = me.disableFormats !== true,
\r
20 fm = Ext.util.Format,
\r
24 return me.compiled(values);
\r
26 function fn(m, name, format, args){
\r
27 if (format && useF) {
\r
28 if (format.substr(0, 5) == "this.") {
\r
29 return tpl.call(format.substr(5), values[name], values);
\r
32 // quoted values are required for strings in compiled templates,
\r
33 // but for non compiled we need to strip them
\r
34 // quoted reversed for jsmin
\r
35 var re = /^\s*['"](.*)["']\s*$/;
\r
36 args = args.split(',');
\r
37 for(var i = 0, len = args.length; i < len; i++){
\r
38 args[i] = args[i].replace(re, "$1");
\r
40 args = [values[name]].concat(args);
\r
42 args = [values[name]];
\r
44 return fm[format].apply(fm, args);
\r
47 return values[name] !== undefined ? values[name] : "";
\r
50 return me.html.replace(me.re, fn);
\r
54 * <tt>true</tt> to disable format functions (defaults to <tt>false</tt>)
\r
58 disableFormats : false,
\r
61 * The regular expression used to match template variables
\r
66 re : /\{([\w-]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?\}/g,
\r
69 * Compiles the template into an internal function, eliminating the RegEx overhead.
\r
70 * @return {Ext.Template} this
\r
73 compile : function(){
\r
75 fm = Ext.util.Format,
\r
76 useF = me.disableFormats !== true,
\r
77 sep = Ext.isGecko ? "+" : ",",
\r
80 function fn(m, name, format, args){
\r
82 args = args ? ',' + args : "";
\r
83 if(format.substr(0, 5) != "this."){
\r
84 format = "fm." + format + '(';
\r
86 format = 'this.call("'+ format.substr(5) + '", ';
\r
90 args= ''; format = "(values['" + name + "'] == undefined ? '' : ";
\r
92 return "'"+ sep + format + "values['" + name + "']" + args + ")"+sep+"'";
\r
95 // branched to use + in gecko and [].join() in others
\r
97 body = "this.compiled = function(values){ return '" +
\r
98 me.html.replace(/\\/g, '\\\\').replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn) +
\r
101 body = ["this.compiled = function(values){ return ['"];
\r
102 body.push(me.html.replace(/\\/g, '\\\\').replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn));
\r
103 body.push("'].join('');};");
\r
104 body = body.join('');
\r
110 // private function used to call members
\r
111 call : function(fnName, value, allValues){
\r
112 return this[fnName](value, allValues);
\r
115 Ext.Template.prototype.apply = Ext.Template.prototype.applyTemplate;