Textpattern CMS conditional tags: an overview

admin-ajaxThis is the second chapter of my six part series on Textpattern tags. The focus this month is conditional tags. Conditional tags use logic to decide the outcome of a given situation, and can be as simple or complex as you decide to go. If there’s such a thing as a tag rabbit hole, conditional tags are a good contender. There are currently 33 tags considered to be conditional-type tags:

  • <txp:else />
  • <txp:if_article_author />
  • <txp:if_article_category />
  • <txp:if_article_id />
  • <txp:if_article_image />
  • <txp:if_article_list />
  • <txp:if_article_section />
  • <txp:if_author />
  • <txp:if_category />
  • <txp:if_comments />
  • <txp:if_comments_allowed />
  • <txp:if_comments_disallowed />
  • <txp:if_comments_error />
  • <txp:if_comments_preview />
  • <txp:if_custom_field />
  • <txp:if_different />
  • <txp:if_excerpt />
  • <txp:if_expired />
  • <txp:if_expires />
  • <txp:if_first_article />
  • <txp:if_first_category />
  • <txp:if_first_section />
  • <txp:if_individual_article />
  • <txp:if_keywords />
  • <txp:if_last_article />
  • <txp:if_last_category />
  • <txp:if_last_section />
  • <txp:if_plugin />
  • <txp:if_search />
  • <txp:if_search_results />
  • <txp:if_section />
  • <txp:if_status />
  • <txp:if_variable />

With the exception of the `<txp:else />`, all the conditional tags begin with `<txp:if_` and most are pretty straightforward to understand when you know the building blocks of Textpattern CMS.

Tags have been introduced into Textpattern over a number of years, mostly in response to user feedback and requirements. Each of the tags listed above are covered by the core code; plugins can (and do) extend the functionality of Textpattern further, often with their own plugin-specific tags that you won’t see listed above.

I’ve selected 4 tags from the above list that I will explain in more detail, with examples. I consider these four a solid introduction to conditional tags, and knowing them will set you in good stead for learning the other tags. There is considerable crossover in the way conditional tags work, although their respective attributes vary. Some conditionals have no attributes at all, and others allow you to have very specific control over what can be outputted.

The first tag I’m going to cover is `<txp:else />`. It’s absolutely vital you know how to use it correctly, because it allows you to exercise a lot more control over your tag constructs. Essentially, knowing how `<txp:else />` connects to the other conditionals will extend the list of things you can check for. At a high level, a conditional tag will do something depending on the outcome of a check. For example, the `<txp:if_expired>` tag checks if the article being viewed has expired. If it has, then something can take place. Like this:

 do something

That’s great if you want to `do something` if the article has expired. There is no such thing as the `<txp:if_not_expired>` tag, but the effect can be achieved by using `<txp:if_expired>` and `<txp:if_else />` together:

<txp:else />
do something

In this code, a check takes place for the article expiry. If it’s expired, then nothing happens; if it hasn’t expired, the `do something` part comes into play.

One of my favorite parts of Textpattern site building is the tag building part. You have a powerful, flexible engine at your disposal and you don’t need to know how to write PHP to harness it – how cool is that? Over the coming month I’ll be showing you how to better understand conditional tags in Textpattern, and I look forward to telling you more.

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 *