X-Git-Url: http://git.ithinksw.org/extjs.git/blobdiff_plain/c930e9176a5a85509c5b0230e2bff5c22a591432..refs/heads/old:/docs/source/XTemplate.html?ds=sidebyside diff --git a/docs/source/XTemplate.html b/docs/source/XTemplate.html index ba213553..41d600a7 100644 --- a/docs/source/XTemplate.html +++ b/docs/source/XTemplate.html @@ -1,19 +1,52 @@ - - - The source code - - - - -
/** + + + + The source code + + + + +
/*!
+ * Ext JS Library 3.3.1
+ * Copyright(c) 2006-2010 Sencha Inc.
+ * licensing@sencha.com
+ * http://www.sencha.com/license
+ */
+
/** * @class Ext.XTemplate * @extends Ext.Template - *

A template class that supports advanced functionality like autofilling arrays, conditional processing with - * basic comparison operators, sub-templates, basic math function support, special built-in template variables, - * inline code execution and more. XTemplate also provides the templating mechanism built into {@link Ext.DataView}.

- *

XTemplate supports many special tags and built-in operators that aren't defined as part of the API, but are - * supported in the templates that can be created. The following examples demonstrate all of the supported features. - * This is the data object used for reference in each code example:

+ *

A template class that supports advanced functionality like:

    + *
  • Autofilling arrays using templates and sub-templates
  • + *
  • Conditional processing with basic comparison operators
  • + *
  • Basic math function support
  • + *
  • Execute arbitrary inline code with special built-in template variables
  • + *
  • Custom member functions
  • + *
  • Many special tags and built-in operators that aren't defined as part of + * the API, but are supported in the templates that can be created
  • + *

+ *

XTemplate provides the templating mechanism built into:

    + *
  • {@link Ext.DataView}
  • + *
  • {@link Ext.ListView}
  • + *
  • {@link Ext.form.ComboBox}
  • + *
  • {@link Ext.grid.TemplateColumn}
  • + *
  • {@link Ext.grid.GroupingView}
  • + *
  • {@link Ext.menu.Item}
  • + *
  • {@link Ext.layout.MenuLayout}
  • + *
  • {@link Ext.ColorPalette}
  • + *

+ * + *

For example usage {@link #XTemplate see the constructor}.

+ * + * @constructor + * The {@link Ext.Template#Template Ext.Template constructor} describes + * the acceptable parameters to pass to the constructor. The following + * examples demonstrate all of the supported features.

+ * + *
    + * + *
  • Sample Data + *
    + *

    This is the data object used for reference in each code example:

    *
    
     var data = {
         name: 'Jack Slocum',
    @@ -37,104 +70,161 @@ var data = {
         }]
     };
      * 
    - *

    Auto filling of arrays
    The tpl tag and the for operator are used - * to process the provided data object. If for="." is specified, the data object provided - * is examined. If the variable in for is an array, it will auto-fill, repeating the template - * block inside the tpl tag for each item in the array:

    + *
    + *
  • + * + * + *
  • Auto filling of arrays + *
    + *

    The tpl tag and the for operator are used + * to process the provided data object: + *

      + *
    • If the value specified in for is an array, it will auto-fill, + * repeating the template block inside the tpl tag for each item in the + * array.
    • + *
    • If for="." is specified, the data object provided is examined.
    • + *
    • While processing an array, the special variable {#} + * will provide the current array index + 1 (starts at 1, not 0).
    • + *
    + *

    + *
    
    +<tpl for=".">...</tpl>       // loop through array at root node
    +<tpl for="foo">...</tpl>     // loop through array at foo node
    +<tpl for="foo.bar">...</tpl> // loop through array at foo.bar node
    + * 
    + * Using the sample data above: *
    
     var tpl = new Ext.XTemplate(
         '<p>Kids: ',
    -    '<tpl for=".">',
    -        '<p>{name}</p>',
    +    '<tpl for=".">',       // process the data.kids node
    +        '<p>{#}. {name}</p>',  // use current array index to autonumber
         '</tpl></p>'
     );
     tpl.overwrite(panel.body, data.kids); // pass the kids property of the data object
      * 
    - *

    Scope switching
    The for property can be leveraged to access specified members - * of the provided data object to populate the template:

    + *

    An example illustrating how the for property can be leveraged + * to access specified members of the provided data object to populate the template:

    *
    
     var tpl = new Ext.XTemplate(
         '<p>Name: {name}</p>',
         '<p>Title: {title}</p>',
         '<p>Company: {company}</p>',
         '<p>Kids: ',
    -    '<tpl for="kids">', // interrogate the kids property within the data
    +    '<tpl for="kids">',     // interrogate the kids property within the data
             '<p>{name}</p>',
         '</tpl></p>'
     );
    +tpl.overwrite(panel.body, data);  // pass the root node of the data object
    + * 
    + *

    Flat arrays that contain values (and not objects) can be auto-rendered + * using the special {.} variable inside a loop. This variable + * will represent the value of the array at the current index:

    + *
    
    +var tpl = new Ext.XTemplate(
    +    '<p>{name}\'s favorite beverages:</p>',
    +    '<tpl for="drinks">',
    +       '<div> - {.}</div>',
    +    '</tpl>'
    +);
     tpl.overwrite(panel.body, data);
      * 
    - *

    Access to parent object from within sub-template scope
    When processing a sub-template, for example while - * looping through a child array, you can access the parent object's members via the parent object:

    + *

    When processing a sub-template, for example while looping through a child array, + * you can access the parent object's members via the parent object:

    *
    
     var tpl = new Ext.XTemplate(
         '<p>Name: {name}</p>',
         '<p>Kids: ',
         '<tpl for="kids">',
    -        '<tpl if="age &gt; 1">',  // <-- Note that the > is encoded
    +        '<tpl if="age > 1">',
                 '<p>{name}</p>',
    -            '<p>Dad: {parent.name}</p>',
    +            '<p>Dad: {parent.name}</p>',
             '</tpl>',
         '</tpl></p>'
     );
     tpl.overwrite(panel.body, data);
    -
    - *

    Array item index and basic math support
    While processing an array, the special variable {#} - * will provide the current array index + 1 (starts at 1, not 0). Templates also support the basic math operators - * + - * and / that can be applied directly on numeric data values:

    + *
+ * + * + * + * + *
  • Conditional processing with basic comparison operators + *
    + *

    The tpl tag and the if operator are used + * to provide conditional checks for deciding whether or not to render specific + * parts of the template. Notes:

      + *
    • Double quotes must be encoded if used within the conditional
    • + *
    • There is no else operator — if needed, two opposite + * if statements should be used.
    • + *
    + *
    
    +<tpl if="age > 1 && age < 10">Child</tpl>
    +<tpl if="age >= 10 && age < 18">Teenager</tpl>
    +<tpl if="this.isGirl(name)">...</tpl>
    +<tpl if="id==\'download\'">...</tpl>
    +<tpl if="needsIcon"><img src="{icon}" class="{iconCls}"/></tpl>
    +// no good:
    +<tpl if="name == "Jack"">Hello</tpl>
    +// encode " if it is part of the condition, e.g.
    +<tpl if="name == &quot;Jack&quot;">Hello</tpl>
    + * 
    + * Using the sample data above: *
    
     var tpl = new Ext.XTemplate(
         '<p>Name: {name}</p>',
         '<p>Kids: ',
         '<tpl for="kids">',
    -        '<tpl if="age &gt; 1">',  // <-- Note that the > is encoded
    -            '<p>{#}: {name}</p>',  // <-- Auto-number each item
    -            '<p>In 5 Years: {age+5}</p>',  // <-- Basic math
    -            '<p>Dad: {parent.name}</p>',
    +        '<tpl if="age > 1">',
    +            '<p>{name}</p>',
             '</tpl>',
         '</tpl></p>'
     );
     tpl.overwrite(panel.body, data);
    -
    - *

    Auto-rendering of flat arrays
    Flat arrays that contain values (and not objects) can be auto-rendered - * using the special {.} variable inside a loop. This variable will represent the value of - * the array at the current index:

    - *
    
    -var tpl = new Ext.XTemplate(
    -    '<p>{name}\'s favorite beverages:</p>',
    -    '<tpl for="drinks">',
    -       '<div> - {.}</div>',
    -    '</tpl>'
    -);
    -tpl.overwrite(panel.body, data);
    -
    - *

    Basic conditional logic
    Using the tpl tag and the if - * operator you can provide conditional checks for deciding whether or not to render specific parts of the template. - * Note that there is no else operator — if needed, you should use two opposite if statements. - * Properly-encoded attributes are required as seen in the following example:

    + *
  • + * + * + * + * + *
  • Basic math support + *
    + *

    The following basic math operators may be applied directly on numeric + * data values:

    + * + - * /
    + * 
    + * For example: *
    
     var tpl = new Ext.XTemplate(
         '<p>Name: {name}</p>',
         '<p>Kids: ',
         '<tpl for="kids">',
             '<tpl if="age &gt; 1">',  // <-- Note that the > is encoded
    -            '<p>{name}</p>',
    +            '<p>{#}: {name}</p>',  // <-- Auto-number each item
    +            '<p>In 5 Years: {age+5}</p>',  // <-- Basic math
    +            '<p>Dad: {parent.name}</p>',
             '</tpl>',
         '</tpl></p>'
     );
     tpl.overwrite(panel.body, data);
     
    - *

    Ability to execute arbitrary inline code
    In an XTemplate, anything between {[ ... ]} is considered - * code to be executed in the scope of the template. There are some special variables available in that code: + *

    + *
  • + * + * + *
  • Execute arbitrary inline code with special built-in template variables + *
    + *

    Anything between {[ ... ]} is considered code to be executed + * in the scope of the template. There are some special variables available in that code: *

    - * This example demonstrates basic row striping using an inline code block and the xindex variable:

    + * This example demonstrates basic row striping using an inline code block and the + * xindex variable:

    *
    
     var tpl = new Ext.XTemplate(
         '<p>Name: {name}</p>',
    @@ -147,8 +237,13 @@ var tpl = new Ext.XTemplate(
         '</tpl></p>'
     );
     tpl.overwrite(panel.body, data);
    -
    - *

    Template member functions
    One or more member functions can be defined directly on the config + * + *

    + *
  • + * + *
  • Template member functions + *
    + *

    One or more member functions can be specified in a configuration * object passed into the XTemplate constructor for more complex processing:

    *
    
     var tpl = new Ext.XTemplate(
    @@ -158,55 +253,65 @@ var tpl = new Ext.XTemplate(
             '<tpl if="this.isGirl(name)">',
                 '<p>Girl: {name} - {age}</p>',
             '</tpl>',
    +        // use opposite if statement to simulate 'else' processing:
             '<tpl if="this.isGirl(name) == false">',
                 '<p>Boy: {name} - {age}</p>',
             '</tpl>',
             '<tpl if="this.isBaby(age)">',
                 '<p>{name} is a baby!</p>',
             '</tpl>',
    -    '</tpl></p>', {
    -     isGirl: function(name){
    -         return name == 'Sara Grace';
    -     },
    -     isBaby: function(age){
    -        return age < 1;
    -     }
    -});
    +    '</tpl></p>',
    +    {
    +        // XTemplate configuration:
    +        compiled: true,
    +        disableFormats: true,
    +        // member functions:
    +        isGirl: function(name){
    +            return name == 'Sara Grace';
    +        },
    +        isBaby: function(age){
    +            return age < 1;
    +        }
    +    }
    +);
     tpl.overwrite(panel.body, data);
    -
    - * @constructor - * @param {String/Array/Object} parts The HTML fragment or an array of fragments to join(""), or multiple arguments - * to join("") that can also include a config object + * + *
    + *
  • + * + * + * + * @param {Mixed} config */ Ext.XTemplate = function(){ Ext.XTemplate.superclass.constructor.apply(this, arguments); var me = this, - s = me.html, - re = /]*>((?:(?=([^<]+))\2|<(?!tpl\b[^>]*>))*?)<\/tpl>/, - nameRe = /^]*?for="(.*?)"/, - ifRe = /^]*?if="(.*?)"/, - execRe = /^]*?exec="(.*?)"/, - m, - id = 0, - tpls = [], - VALUES = 'values', - PARENT = 'parent', - XINDEX = 'xindex', - XCOUNT = 'xcount', - RETURN = 'return ', - WITHVALUES = 'with(values){ '; + s = me.html, + re = /]*>((?:(?=([^<]+))\2|<(?!tpl\b[^>]*>))*?)<\/tpl>/, + nameRe = /^]*?for="(.*?)"/, + ifRe = /^]*?if="(.*?)"/, + execRe = /^]*?exec="(.*?)"/, + m, + id = 0, + tpls = [], + VALUES = 'values', + PARENT = 'parent', + XINDEX = 'xindex', + XCOUNT = 'xcount', + RETURN = 'return ', + WITHVALUES = 'with(values){ '; s = ['', s, ''].join(''); while((m = s.match(re))){ - var m2 = m[0].match(nameRe), - m3 = m[0].match(ifRe), - m4 = m[0].match(execRe), - exp = null, - fn = null, - exec = null, - name = m2 && m2[1] ? m2[1] : ''; + var m2 = m[0].match(nameRe), + m3 = m[0].match(ifRe), + m4 = m[0].match(execRe), + exp = null, + fn = null, + exec = null, + name = m2 && m2[1] ? m2[1] : ''; if (m3) { exp = m3 && m3[1] ? m3[1] : null; @@ -237,9 +342,9 @@ Ext.XTemplate = function(){ s = s.replace(m[0], '{xtpl'+ id + '}'); ++id; } - Ext.each(tpls, function(t) { - me.compileTpl(t); - }); + for(var i = tpls.length-1; i >= 0; --i){ + me.compileTpl(tpls[i]); + } me.master = tpls[tpls.length-1]; me.tpls = tpls; }; @@ -252,10 +357,10 @@ Ext.extend(Ext.XTemplate, Ext.Template, { // private applySubTemplate : function(id, values, parent, xindex, xcount){ var me = this, - len, - t = me.tpls[id], - vs, - buf = []; + len, + t = me.tpls[id], + vs, + buf = []; if ((t.test && !t.test.call(me, values, parent, xindex, xcount)) || (t.exec && t.exec.call(me, values, parent, xindex, xcount))) { return ''; @@ -264,9 +369,9 @@ Ext.extend(Ext.XTemplate, Ext.Template, { len = vs.length; parent = t.target ? values : parent; if(t.target && Ext.isArray(vs)){ - Ext.each(vs, function(v, i) { - buf[buf.length] = t.compiled.call(me, v, parent, i+1, len); - }); + for(var i = 0, len = vs.length; i < len; i++){ + buf[buf.length] = t.compiled.call(me, vs[i], parent, i+1, len); + } return buf.join(''); } return t.compiled.call(me, vs, parent, xindex, xcount); @@ -275,7 +380,7 @@ Ext.extend(Ext.XTemplate, Ext.Template, { // private compileTpl : function(tpl){ var fm = Ext.util.Format, - useF = this.disableFormats !== true, + useF = this.disableFormats !== true, sep = Ext.isGecko ? "+" : ",", body; @@ -311,7 +416,8 @@ Ext.extend(Ext.XTemplate, Ext.Template, { } function codeFn(m, code){ - return "'"+ sep +'('+code+')'+sep+"'"; + // Single quotes get escaped when the template is compiled, however we want to undo this when running code. + return "'" + sep + '(' + code.replace(/\\'/g, "'") + ')' + sep + "'"; } // branched to use + in gecko and [].join() in others @@ -377,6 +483,7 @@ Ext.XTemplate.prototype.apply = Ext.XTemplate.prototype.applyTemplate; Ext.XTemplate.from = function(el){ el = Ext.getDom(el); return new Ext.XTemplate(el.value || el.innerHTML); -}; - +}; + + \ No newline at end of file