PDA

View Full Version : XTemplate condition issue



fabads
18 Sep 2010, 7:13 AM
Hi all,


Do you know why the following template is displayed correctly:


'<tpl for=".">',
'<tpl if="data &gt; 0">',
'<span class="positive_data">{data}</span>',
'</tpl>',
'</tpl>',


And the same with > instead of &gt; displays strangely:


'<tpl for=".">',
'<tpl if="data > 0">',
'<span class="positive_data">{data}</span>',
'</tpl>',
'</tpl>',



The second template is displayed like this:
And the result:

0"> followed by the data value

steffenk
18 Sep 2010, 3:39 PM
the second is correct. But what is data? Has to be an int

Animal
18 Sep 2010, 10:21 PM
As I always reply to these threads: practice on the Firebug command line!



new Ext.XTemplate('<tpl if="foo &gt; 0">{foo}</tpl>').apply({foo: 0})


Then



new Ext.XTemplate('<tpl if="foo &gt; 0">{foo}</tpl>').apply({foo: 1})

fabads
23 Sep 2010, 8:14 PM
I think my problem is that the value could be either "N/A" or a decimal value. And the value is between quotes in my json (don't know if it's a problem or not).

Animal
23 Sep 2010, 9:21 PM
Not a problem at all. Just write the code.

fabads
25 Sep 2010, 9:26 AM
Hum, I have the same problem in the Firebug console.
If I follows the documentation of the XTemplate constructor, I can use


new Ext.XTemplate('<tpl if="foo >= 0">{foo}</tpl>').apply({foo: 1})

And the result is :

= 0">1

Animal
25 Sep 2010, 12:38 PM
You didn't read my code.

fabads
25 Sep 2010, 1:01 PM
Yes I've read your code, copied it in the firebug console and replaced &gt; by >=
Sorry I don't see my error :-?

Animal
25 Sep 2010, 9:58 PM
replaced &gt; by >=

Why?

fabads
25 Sep 2010, 10:48 PM
For 2 reasons:

1. The documentation:

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 == "Jack"">Hello</tpl>
// encode " if it is part of the condition, e.g.
<tpl if="name == &quot;Jack&quot;">Hello</tpl>

2. &ge; (greater or equal) seems to not be implemented

Animal
26 Sep 2010, 11:10 AM
Of course not!

Have you ever seen that SINGLE CHARACTER in Javascript?