The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

Yanks25 (2) [Avatar] Offline
#1
I'm using an XTemplate and appending tpl data. When I use an if statement, the browser complains that the comparison value ('sport', in the example below) is undefined. If I remove the if, the html <td>{sport}</td> returns the correct value

Example:

var tplData = [{
sport : "Baseball"
}];

var tpl = new Ext.XTemplate(
'<table>',
'<tr>',
'<td>{sport}</td>', // shows the correct value when I exclude the following tpl lines
'<tpl if="sport == "Baseball"">',
'<td>Red Sox</td>',
'</tpl>',
'</tr></table>'
)

tpl.compile();
tpl.append(document.body, tplData);
MartinL (1) [Avatar] Offline
#2
Re: XTemplate tpl if statement
// Not exactly the same but works vor me


var tplData1 = [{
sport : ['Baseball', 'Football']
}];

var myTpl4 = new Ext.XTemplate(
'<tpl for=".">',
'<tpl for="sport">', // loop thru Sport Array
'<table border="0"><tr>',
'<td>{.}</td>',// XTemplate displays all data in the array
'<tpl if="this.isBaseball(values)">', // Execute the this.isBaseball method
'<td>Red Sox</td>',
'</tpl>',
'{[ (xindex < xcount) ? " " : "" ]}',
'</tpl>',
'</tr></table>',
'</tpl>',
{ // An object containing a local member, isBaseball
isBaseball : function(sport) {
return sport == 'Baseball';
}
}
);

myTpl4.compile();
myTpl4.append(document.body, tplData1);
Yanks25 (2) [Avatar] Offline
#3
Re: XTemplate tpl if statement
Thank You, Martini. By reviewing your example, I realized what I did wrong.

I wrote this:

var tplData = [{
sport : "Baseball"
}];

When I should had written this:

var tplData = {
sport : "Baseball"
};