Using Conditional Tags in Textpattern CMS

The third article in this mini-series related to Textpattern CMS tags concerns conditional tags. Personally speaking, of all the tag types, I put conditionals up there near the top. They’re smart, clear and really useful. A conditional tag at its basic level operates like this:

  • if x is true, then do y

You can pep it up a bit, too:

  • if x is true, then do y; but if x is false, do z

Neat, huh? In this example, x is a scenario or situation, y is something (or nothing) and z is something (or nothing). That’s the high-level fundamentals introduced, let’s get into the details.

Textpattern conditional tags start with ‘txp:if’ and are container tags. Here’s an example:

<txp:if_article_image>
<txp:article image />
</txp:if_article_image>

In this case, the tags perform the following:

  • if there is an article image set, then display the image

This code follows the ‘if x is true, then do y’ route that I mentioned at the beginning; there is no explicit ‘but if x is false, do z’ part, the assumption is that if there is no article image, then nothing happens. To cover that eventually, conditional tags can make use of another tag: <txp:else />. Taking the above code example and hooking in the <txp:else /> tag makes it look like this:

<txp:if_article_image>
<txp:article image />
<txp:else />
<txp:image id="23" />
</txp:if_article_image>

This covers the ‘if x is true, then do y; but if x is false, do z’ routine, like this:

  • if there is an article image set, then display the image
  • if there is no article image set, then display the Textpattern image with an ID of 23

I’ve used tags throughout the example, you don’t have to. The part where I used <txp:image /> could just as easily be substituted for a block of (X)HTML, a message saying ‘there is no image’, or whatever you like.

Using this conditional tag logic, you can switch things up and do this:

  • if x is not true, then do z

Textpattern conditional tags check to see if situations and scenarios exist (or evaluate as true, if you prefer), with the <txp:else /> tag taking care of business if the thing doesn’t exist. To achieve this ‘if x is not true, then do z’, just use <txp:else /> inside the tag as the first thing:

<txp:if_article_image>
<txp:else />
<txp:image id="23" />
</txp:if_article_image>

Note the removal of the part that displays the article image – now, if there is an article image nothing will happen. If there is no image, then the placeholder image will be displayed.

If you read my last article on Textpattern, you’ll likely remember that tags can have attributes to fine-tune their functionality. The example above is a yes or no (boolean) check for an article image using a tag with no attributes. Conditional tags can have attributes. One such example is <txp:if_section>. Here it is in action:

<txp:if_section name="news">
<h4>The latest news from <txp:site_name />.</h4>
<txp:else />
<h4>Welcome to <txp:site_name />.</h4
</txp:if_section>

This code might be used on a site with a single section called news. The website front page would display ‘<h4>Welcome to [site name].</h4>’ but the news section would show ‘<h4>The latest news from [site name].</h4>’ instead. Any new sections created would also display ‘<h4>Welcome to [site name].</h4>’ because they are not called ‘news’ and therefore sit after the <txp:else /> part.

At the time of writing, there are 32 conditional tags in Textpattern, and that number is likely to gradually increase over time as Textpattern is improved and enhanced. You can check out the full list of Textpattern conditional tags if you’re interested – I recommend you do.

Looking for quality Textpattern Hosting? Look no further than Arvixe Web Hosting!

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

Author Spotlight

Pete Cooper

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 *


× 2 = 8

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>