Understanding Textpattern structural tags

admin-ajax

This is the third part of my six-part series on Textpattern CMS tags. For the following bunch of articles, I’m going to focus on Textpattern structural tags. This type of tag is, as the name implies, primarily used in the building blocks of a Textpattern website. These tags should be considered to provide some of the navigational scaffolding of the Textpattern-powered website:

  • category *
  • category list *
  • if_first_category
  • if_first_section
  • if_last_category
  • if_last_section
  • output form *
  • section *
  • section_list *
  • yield *

Tags marked with a * will be covered in the next 4 articles. The remaining tags, all beginning with `if_` are both structural and conditional. As a refresher, Textpattern conditional tags can undertake actions depending on the outcome of a given scenario, for example:

<txp:if_individual_article>
 <span>This is an individual article</span>
 </txp:if_individual_article>

…and, adding an `else` tag to the mix:

<txp:if_individual_article>
<span>This is an individual article</span>
<txp:else />
<span>This is not an individual article</span>
</txp:if_individual_article>

In the case of the four `if_` tags above, there is some common ground with their roles:

  • if first category = the current category is the first in the currently displayed list
  • if first section = the current section is the first in the currently displayed list
  • if last category = the current category is the last in the currently displayed list
  • if last section = the current section is the last in the currently displayed list

Essentially, these four tags so do very similar things: they check for the first or list thing in a list of sections or categories. This list of things is typically generated by the `section_list` or `category_list` tags. In the case of `if_first_category` it’s especially helpful if you want to prevent the first category being hyperlinked, for example if you want to use nested categories.

Here’s an example of how this can be achieved:

<txp:category_list parent="basegroup-1" children="0">
<txp:if_first_category>
<h4><txp:category /></h4>
<txp:else />
<txp:category link="1" />
</txp:if_first_category>
</txp:category_list>

The `if_last_category` is frankly less useful in everyday use. I haven’t found a situation in which I could actively use it on a production website, but if you think of something do leave me a note here.

Moving on to the `if_first_category` and `if_last_category` tags, there’s a similar code structure to each. In fact, it’s logical to use a variation on the code above to show how it works. I’ll start by constructing a similar block:

<txp:section_list wraptag="ol" break="">
<txp:if_first_section>
<li class="ol-li-top"><txp:section title="1" /></li>
<txp:else />
<li class="ol-li"><txp:section title="1" /></li>
</txp:if_first_section>
</txp:section_list>

In this case case, the `section_list` tag is used to populate a list of sections, and the first in said list is assigned a CSS class of `ol-li-top`, which might involve some presentational flourish or other stuff to make it stand out in some fashion. Other sections in the list have the CSS class of `ol-li`, and as they’re not the first in the list, they may have a more neutral or less whizzy look. To factor in `if_last_section`, the code can be tweaked further:

<txp:section_list wraptag="ol" break="">
<txp:if_first_section>
<li class="ol-li-top"><txp:section title="1" /></li>
<txp:else />
<txp:if_last_section>
<li class="ol-li-top"><txp:section title="1" /></li>
<txp:else />
<li class="ol-li"><txp:section title="1" /></li>
</txp:if_first_section>
</txp:if_first_section>
</txp:section_list>

It’s logical to step through, but will have performance impacts with long lists on busy sites. More tags equals more work that Textpattern has to do, and if the code gets too nested and complex, it makes human debugging less straightforward. You can experiment with how tags nest and get processed, but bear in mind that you (or someone else) may have to poke around with your code in the future, so make plenty of comments.

Next time: `txp:category` and `txp:section`.

Looking for quality Textpattern Hosting? Look no further than Arvixe Web Hosting and use coupon TEXTPATTERN for 20% off your first invoice!

Tags: , , , , , , , , , | Posted under Textpattern | RSS 2.0

Author Spotlight

Pete Cooper

Pete Cooper has been using Textpattern since 2005. Textpattern is his preferred CMS weapon of choice. Its logical and flexible approach to content management makes Pete happy, as does its lightweight core and helpful user community. Pete's website - petecooper.org - runs on top of Textpattern and chronicles his day-to-day experiences from his home near the Atlantic in north Cornwall, United Kingdom.

Leave a Reply

Your email address will not be published. Required fields are marked *