graphicpush

Thoughts on branding, design, writing and life by Kevin Potts. Established 2003.

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>

Each sorting method is housed in its own unique div. To prevent all four divs from displaying at once and creating a single long page, a simple Javascript activates just one and hides the remaining three.

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.

, , ,

commentary + criticism

Nathan Smith

wrote the following on Monday June 12, 2006

It’s nice to see I’m not the only one using the limit=”99999” trick. I always felt it was sort of a hacky way of doing it, but it’s either that or the default limit of 10, right?

Kevin

wrote the following on Monday June 12, 2006

I don’t consider it a hack as much as following TXP’s logic. The default is 10 — and that’s fine for some applications — but I would rather always manually define the limit. I suppose there could be some additional value like “all,” but how many times is there going to be 100,000 articles?

Robert Wetzlmayr

wrote the following on Tuesday June 13, 2006

txp:if_different isn’t that magic but obeys a simple principle:

Reads the text stemming from the enclosed tags, stores the results aside for future reference and renders it on the page whenever this is different from the previous outcome for the same enclosed tags.

Otherwise, it renders nothing (or the txp:else part, if present).

Plus: You can have as many txp:if_different tags in your forms and page templates as you like. Each of them will separately compare its own output for just the tags it embraces with the one it had in its previous occurrence and swallow duplicates.

It is a very powerful concept which will gain popularity over time.

I was using it lately grouping a list of chronologically ordered articles by intervening headlines designating the articles’ publishing month.

Naomi

wrote the following on Tuesday June 13, 2006

Wow! I just started learning Textpattern, but the more I know, the more I see how many possibilities it has. Thanks for sharing this!

Nick Blume

wrote the following on Sunday June 18, 2006

Your feeds donĀ“t work!

Kevin

wrote the following on Monday June 19, 2006

Good call. They do now. My bad.

Nora Brown

wrote the following on Tuesday September 26, 2006

Thank you thank you! Your post has been enormously helpful in planning and creating the archives section for site I’m working on. And I’m sure if_different will come in handy in all sorts of other contexts as well. Thanks again!