PDA

View Full Version : XTemplate Conditional Processing



dmillasich
16 Mar 2011, 11:05 AM
If I have 10 items to be processed in an itemTpl, and only one of those items needs to be qualified for specific formatting - must each item be specifically identified? For example,



itemTpl: '<tpl for=".">'
+ '<tpl if="id==1"><div class="mycls"><img class="img" src="{alert}" />{desc}</div></tpl>'
+ ' <tpl if="id==2"><div class="mycls">{desc}</div></tpl>'
+ ' <tpl if="id==3"><div class="mycls">{desc}</div></tpl>'
+ ' <tpl if="id==4"><div class="mycls">{desc}</div></tpl>'
+ ' <tpl if="id==5"><div class="mycls">{desc}</div></tpl>'
+ ' <tpl if="id==6"><div class="mycls">{desc}</div></tpl>'
+ ' <tpl if="id==7"><div class="mycls">{desc}</div></tpl>'
+ ' <tpl if="id==8"><div class="mycls">{desc}</div></tpl>'
+ ' <tpl if="id==9"><div class="mycls">{desc}</div></tpl>'
+ ' <tpl if="id==10"><div class="mycls">{desc}</div></tpl>'
+ '</tpl>',


Or can the following be functionally accomplished:



itemTpl: '<tpl for=".">'
+ '<tpl if="id==1">'
+ ' <div class="mycls"><img class="img" src="{alert}" />{desc}</div></tpl>'
+ '<tpl else >
+ ' <div class="mycls">{desc}</div></tpl>'
+ '</tpl>',

andersonv1
16 Mar 2011, 1:53 PM
"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."

http://dev.sencha.com/deploy/touch/docs/source/XTemplate.html#cls-Ext.XTemplate

look at the example there and that should be it.

dmillasich
16 Mar 2011, 2:38 PM
Thanks for the link to the docs - the information helped tremendously. From there I was able to code a solution that is a huge improvement over my previous approach.