For up to date documentation and features, visit http://docs.sencha.com/ext-js/4-0

## Sencha Documentation

# Ext.XTemplate

####

    HierarchyExt.TemplateExt.XTemplate

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:

- Ext.view.View

The Ext.Template 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: 'Tommy Maintz',
      title: 'Lead Developer',
      company: 'Sencha Inc.',
      email: 'tommy@sencha.com',
      address: '5 Cups Drive',
      city: 'Palo Alto',
      state: 'CA',
      zip: '44102',
      drinks: ['Coffee', 'Soda', 'Water'],
      kids: [{
              name: 'Joshua',
              age:3
          },{
              name: 'Matthew',
              age:2
          },{
              name: 'Solomon',
              age:0
      }]
      };

- 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=".">',       // 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

  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
              '<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);

  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">',
                  '<p>{name}</p>',
                  '<p>Dad: {parent.name}</p>',
              '</tpl>',
          '</tpl></p>'
      );
      tpl.overwrite(panel.body, data);

- 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 == "Tommy"">Hello</tpl>
      // encode " if it is part of the condition, e.g.
      <tpl if="name == &quot;Tommy&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">',
                  '<p>{name}</p>',
              '</tpl>',
          '</tpl></p>'
      );
      tpl.overwrite(panel.body, data);

- 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>',  // <-- 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);

- 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:

  - `values`: The values in the current scope. If you are using scope changing sub-templates, you can change what `values` is.
  - `parent`: The scope (values) of the ancestor template.
  - `xindex`: If you are in a looping template, the index of the loop you are in (1-based).
  - `xcount`: If you are in a looping template, the total length of the array you are looping.
  This example demonstrates basic row striping using an inline code block and the `xindex` variable:

      var tpl = new Ext.XTemplate(
          '<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>'
       );
      tpl.overwrite(panel.body, data);

- 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,
              // member functions:
              isGirl: function(name){
                 return name == 'Sara Grace';
              },
              isBaby: function(age){
                 return age < 1;
              }
          }
      );
      tpl.overwrite(panel.body, data);

Defined By

### Config Options

#### Other Configs

Ext.XTemplate
view source
codeRe : RegExp

The regular expression used to match code variables (default: matches `{[expression]}`).

The regular expression used to match code variables (default: matches `{[expression]}`).

Ext.Template
view source
disableFormats : Boolean
true to disable format functions in the template. If the template doesn't contain format functions, setting disableFo...

true to disable format functions in the template. If the template doesn't contain format functions, setting disableFormats to true will reduce apply time (defaults to false)

Defined By

### Methods

Ext.Template
view source
append( Mixed el, Object/Array values, [Boolean returnElement]) : HTMLElement/Ext.core.Element
Applies the supplied values to the template and appends the new node(s) to the specified el. For example usage see t...

Applies the supplied `values` to the template and appends the new node(s) to the specified `el`.

For example usage see the constructor.

### Parameters

- el : Mixed

  The context element

- values : Object/Array

  The template values. Can be an array if the params are numeric (i.e. `{0}`) or an object (i.e. `{foo: 'bar'}`).

- returnElement : Boolean

  (optional) true to return an Ext.core.Element (defaults to undefined)

### Returns

- HTMLElement/Ext.core.Element

  The new node or Element

Ext.XTemplate
view source
apply( Object/Array values) : String

Alias for applyTemplate Returns an HTML fragment of this template with the specified values applied.

Alias for applyTemplate Returns an HTML fragment of this template with the specified values applied.

### Parameters

- values : Object/Array

  The template values. Can be an array if your params are numeric (i.e. {0}) or an object (i.e. {foo: 'bar'})

### Returns

- String

  The HTML fragment

Ext.XTemplate
view source
applyTemplate( Object values) : String

Returns an HTML fragment of this template with the specified values applied.

Returns an HTML fragment of this template with the specified values applied.

### Parameters

- values : Object

  The template values. Can be an array if your params are numeric (i.e. {0}) or an object (i.e. {foo: 'bar'})

### Returns

- String

  The HTML fragment

Ext.XTemplate
view source
compile : Function

Compile the template to a function for optimized performance. Recommended if the template will be used frequently.

Compile the template to a function for optimized performance. Recommended if the template will be used frequently.

### Returns

- Function

  The compiled function

Ext.XTemplate
view source
from( String/HTMLElement el, Object config) : Ext.Template

Creates a template from the passed element's value (display:none textarea, preferred) or innerHTML.

Creates a template from the passed element's value (display:none textarea, preferred) or innerHTML.

### Parameters

- el : String/HTMLElement

  A DOM element or its id

- config : Object

### Returns

- Ext.Template

  The created template

Ext.Template
view source
insertAfter( Mixed el, Object/Array values, [Boolean returnElement]) : HTMLElement/Ext.core.Element

Applies the supplied values to the template and inserts the new node(s) after el.

Applies the supplied values to the template and inserts the new node(s) after el.

### Parameters

- el : Mixed

  The context element

- values : Object/Array

  The template values. Can be an array if your params are numeric (i.e. {0}) or an object (i.e. {foo: 'bar'})

- returnElement : Boolean

  (optional) true to return a Ext.core.Element (defaults to undefined)

### Returns

- HTMLElement/Ext.core.Element

  The new node or Element

Ext.Template
view source
insertBefore( Mixed el, Object/Array values, [Boolean returnElement]) : HTMLElement/Ext.core.Element

Applies the supplied values to the template and inserts the new node(s) before el.

Applies the supplied values to the template and inserts the new node(s) before el.

### Parameters

- el : Mixed

  The context element

- values : Object/Array

  The template values. Can be an array if your params are numeric (i.e. {0}) or an object (i.e. {foo: 'bar'})

- returnElement : Boolean

  (optional) true to return a Ext.core.Element (defaults to undefined)

### Returns

- HTMLElement/Ext.core.Element

  The new node or Element

Ext.Template
view source
insertFirst( Mixed el, Object/Array values, [Boolean returnElement]) : HTMLElement/Ext.core.Element

Applies the supplied values to the template and inserts the new node(s) as the first child of el.

Applies the supplied values to the template and inserts the new node(s) as the first child of el.

### Parameters

- el : Mixed

  The context element

- values : Object/Array

  The template values. Can be an array if your params are numeric (i.e. {0}) or an object (i.e. {foo: 'bar'})

- returnElement : Boolean

  (optional) true to return a Ext.core.Element (defaults to undefined)

### Returns

- HTMLElement/Ext.core.Element

  The new node or Element

Ext.Template
view source
overwrite( Mixed el, Object/Array values, [Boolean returnElement]) : HTMLElement/Ext.core.Element

Applies the supplied values to the template and overwrites the content of el with the new node(s).

Applies the supplied values to the template and overwrites the content of el with the new node(s).

### Parameters

- el : Mixed

  The context element

- values : Object/Array

  The template values. Can be an array if your params are numeric (i.e. {0}) or an object (i.e. {foo: 'bar'})

- returnElement : Boolean

  (optional) true to return a Ext.core.Element (defaults to undefined)

### Returns

- HTMLElement/Ext.core.Element

  The new node or Element

Ext.Template
view source
set( String html, [Boolean compile]) : Ext.Template

Sets the HTML used as the template and optionally compiles it.

Sets the HTML used as the template and optionally compiles it.

### Parameters

- html : String
- compile : Boolean

  (optional) True to compile the template (defaults to undefined)

### Returns

- Ext.Template

  this