Numbers to Classnames in MODX

Nov 15, 2013

Use Case

Your markup includes class names based on ordinal numbers–words like "first", "second", or "third-column", for example. You want to assign those classes to objects returned by listing Snippets like getResources, but even though getResources has a myriad of output template options, there isn't one that quite fits the bill for this.

Even if you could make it work with the &tpl_nN property, you'd still need a copy of the output Chunk for each unique class name. Here's another way...

idxtoclass

This Snippet can be used as an Output Modifier to convert integers into ordinal numbers. You use it like this:

    [[+idx:idxtoclass=`4`]]

The above placeholder would be replaced with: "first", "second", "third", "fourth", and back to "first", "second", etc if it was in a Chunk used by getResources as an output template. The option "4" sets the highest ordinal rank. You can optionally set the "type" property (via property set) to 'cardinal' to return cardinal numbers ("one", "two", etc)

The class called by this Snippet is based almost entirely on this nifty bit of php. Shout out to Karl Rixon.

You can install this Extra via Package Management or download directly from modx.com/extras

Vive la MODX!