PDA

View Full Version : Xtemplate & string wildcard



mergozzo
6 Sep 2011, 2:54 AM
Hi,
I'm having difficulty finding a way to use the XTemplate with a
string comparison. For instance, how would you adapt the following code
to search for any name beginning with D?

<p>Name: {name}</p>
<p>Company: {company}</p>
<p>Location: {location}</p>
<p>Salary: {income:currency}</p>
<p>Kids:</p>
<tpl for="kids" if="name=='Darrell Meyer'">
<tpl if="age &gt; 1"><p>{#}. {parent.name}'s kid - {name} - {bday:date("M/d/yyyy")}</p></tpl></tpl>

I thought the following would work:
<tpl for="kids" if="name=='D*'">

Thanks for any help.
Mer.

skirtle
6 Sep 2011, 3:55 AM
Please use CODE tags when posting code (# button on the editor toolbar).

The if attribute is just XML-encoded JavaScript, so it'd be:


<tpl for="kids" if="name.charAt(0) === 'D'">

mergozzo
6 Sep 2011, 5:22 AM
Many thanks for your helpful reply. Just to add to
this question, how would I deal with a string starting
with a hyphen (e.g. -Darrel Meyer-)

Thanks,
Mer.

skirtle
6 Sep 2011, 6:22 AM
Could you elaborate on what you mean by 'deal with a string starting with a hyphen'? What do you want it to do?

mergozzo
6 Sep 2011, 10:56 AM
Hi,
I should have checked before sending the message.
Using charAt allows me to check whether the first
character is a hyphen (I had previously thought that
it was a restricted character).

So the following works:


<tpl for="kids" if="name.charAt(0) != "-""</tpl>

Thanks again for your help.

Mer.