com.sencha.gxt.legacy.client.core

Class XTemplate

Package class diagram package XTemplate
  • 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="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:
     
     public native String getTemplate() /*-{
       return ['<p>Name: {name}</p>',
               '<p>Kids: ',
               '<tpl for="kids">',
               '<tpl if="age > 1">',
                 '<p>{name}</p>',
               '</tpl>',
               '</tpl></p>'
             ].join("");
     );
     template.overwrite(someElement, Util.getJsObject(person));
     
     
  • Basic math support


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

     + - * /
     
    For example:
     
     public native String getTemplate() /*-{
       return ['<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>',
               '</tpl></p>'
             ].join("");
     );
     template.overwrite(someElement, Util.getJsObject(person));
     
     
  • 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:

     
     public native String getTemplate() /*-{
       return ['<p>Name: {name}</p>',
               '<p>Company: {[values.company.toUpperCase() + ", " + values.title]}</p>',
               '<p>Kids: ',
               '<tpl for="kids">',
                 '<div class="{[xindex % 2 === 0 ? "even" : "odd"]}">',
                 '{name}',
                 '</div>',
               '</tpl></p>'
             ].join("");
     );
     template.overwrite(someElement, Util.getJsObject(person));
     
     
  • 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(
         '<p>Name: {name}</p>',
         '<p>Kids: ',
         '<tpl for="kids">',
             '<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>',
         {
             // 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);
     
     
  • Copyright © 2015. All rights reserved.