ThePort's Product and Technology blog. We'll share helpful tips about the platform, talk about upcoming releases, and maybe on occasion share a story or two on how awesome the team is here.
Template Trick: Looping in XSLT
One nifty trick that one of our customers showed me recently is how to control looping in XSLT. Previously, I didn't think it was possible to "break" out of a loop. That is, once you've reached 8 or 9 records, exit the loop. While the correct practice for exiting a loop is still to only ask for what you need, its still important to be able to control the ability to exit whenever you need / want to. So, without further ado, here's how you do it:
Let's assume you have an XSLT variable that contains your collection:
$SOCategories
This collection contains some number of group categories. If we only wanted to display the first two, we could do something like:
<xsl:for-each select="$SOCategories/SocialObjectCategories/SocialObjectCategoryCollection/SocialObjectCategory[position() < 3]">
</xsl:for-each>
The [position() < 3] XPath at the end tells the loop to only loop through the first two. Any subsequent items in the collection will be ignored.
You can use this to only display even numbered rows (using mod) or any such algorithm you wanted to craft.
Thanks to our user community for this helpful tip!