public final class XTemplate extends JavaScriptObject
Usage
This is the data used for reference in each code example:
class Kid extends BaseModelData {
public Kid(String name, int age) {
set("name", name);
set("age", age);
}
}
class Person extends BaseModelData {
public Person(String name, String company, String product, String location) {
set("name", name);
set("company", company);
set("product", product);
set("location", location);
}
public void setKids(List kids) {
set("kids", kids);
}
public List getKids() {
return get("kids");
}
}
final Person person = new Person("Darrell Meyer", "Sencha Inc", "GXT", "Washington, DC");
List kids = new ArrayList();
kids.add(new Kid("Alec", 4));
kids.add(new Kid("Lia", 2));
kids.add(new Kid("Andrew", 1));
person.setKids(kids);
Values can be formatted using the following syntax:
The tpl tag and the for operator are used to
process the provided data object:
<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:
// generating strings in native methods easier than in Java, can be created in Java as well
public native String getTemplate() /*-{
return ['<p>Kids: ',
'<tpl for=".">', // process the data.kids node
'<p>{#}. {name}</p>', // use current array index to autonumber
'</tpl></p>'
].join("");
);
XTemplate tpl = XTemplate.create(getTemplate());
tpl.overwrite(someElement, Util.getJsObject(person.getKids())); // 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:
public native String getTemplate() /*-{
return ['<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>'
].join("");
);
template.overwrite(someElement, Util.getJsObject(person));
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:
public native String getTemplate() /*-{
return ['<p>Name: {name}</p>',
'<p>Kids: ',
'<tpl for="kids">',
'<tpl if="age > 1">',
'<p>{name}</p>',
'<p>Dad: {parent.name}</p>',
'</tpl>',
'</tpl></p>'
].joint("");
);
template.overwrite(someElement, Util.getJsObject(person));
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:
<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 == "Jack"">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));
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 > 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));
Anything between {[ ... ]}
is considered code to be executed in
the scope of the template. There are some special variables available in that
code:
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));
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);
Modifier and Type | Method and Description |
---|---|
Element |
append(Element elem,
JavaScriptObject values) |
String |
applyTemplate(JavaScriptObject values)
Returns an HTML fragment of this template with the specified values
applied.
|
void |
compile()
Compiles the template into an internal function, eliminating the regex
overhead.
|
static XTemplate |
create(String html)
Returns a new template instance using the given html.
|
int |
getMaxDepth()
Returns the maximum number of nested children to process when preparing the
template's data.
|
void |
insertAfter(Element elem,
JavaScriptObject values)
Applies the supplied values to the template and inserts the new node(s)
after elem.
|
void |
insertBefore(Element elem,
JavaScriptObject values)
Applies the supplied values to the template and inserts the new node(s)
before elem.
|
void |
overwrite(Element elem,
JavaScriptObject values)
Applies the supplied values to the template and overwrites the content of
elem with the new node(s).
|
void |
setMaxDepth(int maxDepth)
Specifies the maxiumum number of nested models to search when preparing the
templates data (defaults to 4).
|
cast, createArray, createArray, createFunction, createObject, equals, hashCode, toSource, toString
public final void setMaxDepth(int maxDepth)
maxDepth
- the maximum number of nested childenpublic final int getMaxDepth()
public static XTemplate create(String html)
html
- the templatepublic final Element append(Element elem, JavaScriptObject values)
public final String applyTemplate(JavaScriptObject values)
values
- the substitution valuespublic final void compile()
public final void insertAfter(Element elem, JavaScriptObject values)
elem
- the context elementvalues
- the substitution valuespublic final void insertBefore(Element elem, JavaScriptObject values)
elem
- the context elementvalues
- the substitution valuespublic final void overwrite(Element elem, JavaScriptObject values)
elem
- the context elementvalues
- the substitution valuesCopyright © 2015. All rights reserved.