If Spry Do This...

Sorry for the obtuse title, but I wanted to demonstrate how Spry handles conditionals. What do I mean by that? So far all of my examples have mainly shown how to dump data into a table. While this is useful, sometimes you want to filter the data being shown. I've shown examples with filtering for pagination and keyword filtering, but there are other ways to do conditional type operations with Spry. Let's first take a look at a quick way to filter out data. In my last Spry article, I showed you how you could quickly add a RSS based Spry pod to your site. The pod dumped all the entries from the MXNA XML feed. What if you wanted to filter it by certain categories? Here is the code for that:

<span spry:repeat="mydata"> <span spry:if="'{dc:subject}'.indexOf('ColdFusion') != -1;"> <a href="{link}"><span spry:content="{title}"></span></a><br> </span> </span>

What you want to focus in on is the spry:if condition. Basically, the data inside the block will not be displayed unless the condition matches. You use a JavaScript expression for your condition. In my example, I used:

'{dc:subject}'.indexOf('ColdFusion') != -1;

Spry will replace the {dc:subject} at runtime, and then do an indexOf to see if "ColdFusion" is in the category list. (Notice I didn't do a direct comparison as the category may have been "Flash, ColdFusion".

You can see this example running here. Now for the next example. What if you wanted to do a simple If/Then type check with Spry? Spry supports a spry:choose command which acts much like an If/Else or Case type block.

Consider this example:

<tr spry:repeat="mydata"> <td>{name}</td> <td> <div spry:choose="spry:choose"> <div spry:when="'{debt}' > 100000" class="bad">{debt}</div> <div spry:when="'{debt}' > 50000" class="warning">{debt}</div> <div spry:default="spry:default">{debt}</div> </div> </td> </tr>

Each spry:when inside the choose block will be checked. If a condition is matched, the content inside is run. You can also use a spry:default block to handle a final condition. Check it out at this example. I use it to flag debts that are either very high, or close to getting high.

Like This?

If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can also subscribe to the email feed to get notified of new posts.