Using Textpattern's If_Different Tag to Sort Articles
Creating sorted lists of articles, like in a common site map, is easy to achieve with Textpattern’s if_different tag. Use any common value — including categories or custom fields — to group articles under common headings.
When I started creating the discography page for Sacred Dub, I wanted the visitor to have the ability to browse all the titles four different ways: album title, artist, record label and year of release.
Two recent developments in Textpattern 4 made this possible: the custom_field tag and the somewhat ambiguous and poorly documented if_different tag. I’ve previously covered different uses of custom fields, but I’d like to add one more thing to that list: using them as sorting hooks for archive, site map and any other kind of page where articles need to be sorted via a specific value.
Discography entries in Sacred Dub have many attributes, and custom fields are employed liberally:
- Name of album [title field]
- Artist [custom field]
- Label [custom field]
- Format [custom field]
- Catalog Number [custom field]
- Year [category]
- Tracklisting, Liner Notes and Additional Info [body field]
There are other smaller things, but these are the biggies because every entry has some value for every field. Ideally, the entire catalog could be sorted by any one of these values. Using the if_different tag, that is now possible.
This is a snippet of code from the “discography” section page:
<txp:if_individual_article> <txp:article form="disco_entry_full" /> <txp:else /> <!-- sort by year --> <div id="default" class="tabcontent"> <dl> <txp:article_custom limit="99999" form="disco_by-year" sortby="Category1" sortdir="asc" section="discography" /> </dl> </div> <!-- sort by artist or project --> <div id="discobyproject" class="tabcontent"> <dl> <txp:article_custom limit="99999" form="disco_by-project" sortby="custom_1" sortdir="asc" section="discography" /> </dl> </div> <!-- sort by album title --> <div id="discobyalbum" class="tabcontent"> <ul> <txp:article_custom limit="99999" form="disco_by-album" sortby="Title" sortdir="asc" section="discography" /> </ul> </div> <!-- sort by label --> <div id="discobylabel" class="tabcontent"> <dl> <txp:article_custom limit="99999" form="disco_by-label" sortby="custom_2" sortdir="asc" section="discography" /> </dl> </div> </txp:if_individual_article>
Using If_Different With a Category
The first div is displaying the titles organized by year. Using the article_custom tag, we kick the limit attribute high to make sure everything gets listed, define the section so it doesn’t pull articles from all over the site, tell TXP what value we want to sort by (in this case, Category1, which is the year) and then display everything with the form “disco_by-year,” which contains the following:
<txp:if_different> <dt><txp:category1 title="1" /></dt> </txp:if_different> <dd> <txp:custom_field name="artist" /> | <txp:permlink><txp:title /></txp:permlink> </dd>
This is where the mysterious if_different tag works its magic. Textbook describes if_different somewhat obtusely:
Textpattern will execute the contained statement when the value of the contained statement differs from the preceding value for the contained statement.
Let’s be honest. That sounds like something you’d read on a tax form. I don’t fully understand the functionality myself, only that it works — the above form will create a heading of the year (our Category1), then list all the albums that fall under that year before creating a header with the next available value. The resulting HTML looks like this:
<dt>1985</dt> <dd>Artist | Album Title</dd> <dd>Artist | Album Title</dd> <dd>Artist | Album Title</dd> <dt>1986</dt> <dd>Artist | Album Title</dd> <dt>1987</dt> <dd>Artist | Album Title</dd> <dd>Artist | Album Title</dd> ... etc ...
Creating Site Maps Using Categories and If_Different
I have used this technique extensively on site maps (here’s one simple example and a more complex one). Say you have a site with a primary article section, but a number of important categories under that section. The code would be simple:
<txp:if_different> <h3><txp:category1 title="1" /></h3> </txp:if_different> <p><txp:permlink><txp:title /></txp:permlink></p>
You can see that exact code working on the simple example cited above.
Using If_Different With a Custom Field
Sacred Dub’s discography can also be sorted with the artist name or the record label, both custom fields. The setup is very similar to the category output above, but we are calling a different form (“disco_by-artist” and “disco_by-label”) from the custom_article. The contents of the form demonstrate sorting via custom_field instead of Category1:
<txp:if_different> <dt><txp:custom_field name="artist" /></dt> </txp:if_different> <dd> <txp:permlink><txp:title /></txp:permlink> [<txp:category1 title="1" />] </dd>
As you can see, we’ve just rearranged our various database hooks. This form will create a heading for a particular artist (say “The Golden Palominos”) and list every release under that artist before moving on to the next alphabetical entry.
Using If_Different With Anything
You can use this markup anytime articles can be clumped together under a common value. Organization with categories is the most obvious application, since that’s how most site maps are laid out. But the ability to group articles together under custom fields (or dates, or article images … anything, really) opens up new avenues for creativity and usability.