The first Textpattern CMS conditional tag to learn about is `<txp:else />`. It’s a standout tag when compared alongside the other 32 conditional tags because it can be legitimately used in any of these tags. It’s typically used in the following situation:
<txp:if_something> do something <txp:else /> do something else </txp:if_something>
Note: there is no `<txp:if_something>` tag – it’s just used for this example. In the above code, Textpattern will check for something, and if that check returns a true or positive value, it will `do something`. If it comes back false or negative, it will `do something else`. Replacing `<txp:if_something>` with a proper tag – say, `<txp:if_excerpt />` – and the actions with something helpful will help you understand the Textpattern logic further.
Taking the above example, and switching out the made-up tag for a real one, we can start to build up a tag block:
<txp:if_excerpt> <txp:excerpt /> <txp:else /> <span>Click to find out more.</span> </txp:if_excerpt>
The logic route of the example above will check for an article excerpt, and it one exists it will be outputted. If the article does not have an excerpt, then some text will be substituted advising the reader to click for more.
Let’s take this line by line. It might be helpful to look at this in pairs, too. With that, take a look at the first and last line of that code block. The first line opens the `<txp:if_excerpt>` tag, and the last line closes it. Textpattern tags work in a similar way to (X)HTML tags insofar as some work in pairs, and others work alone. In the above example, the second line is a single tag. The magic happens in the third line where `<txp:else />` does its thing. You may have picked up on `<txp:else />` being a single tag; the way to tell is the self-closing trailing slash.
An important aside: make sure your Textpattern tags, both single and paired, are closed properly. It’s especially important for those ambiguous tags that can be either single or paired tags. I’m coming up to my tenth year of using Textpattern and I still find myself closing incorrectly on occasion. It’s infuriating with extended design and constructing sessions when something as simple as a forward slash can break the rendering of a whole page.
Cursing Textpattern won’t help, it’s just about always the fault of the person doing the typing. I mean, yes, cursing Textpattern may help you vent your frustrations, but some fresh air away from a screen and some rubber duck problem solving is far more helpful.
The example above can be the starting point for some tag nesting, if you’re feeling brave. Let’s extend the example above:
<txp:if_excerpt> <txp:excerpt /> <txp:else /> <txp:if_article_author name="alice"> <span>Alice Speaks: click to find out more.</span> <txp:else /> <span>Click to find out more.</span> </txp:if_article_author> </txp:if_excerpt>
Note the extra lines of code. This is now a nested tag, or a tag inside a tag. I’ve also indented to help you pair-up the tags.
The new logic route of the above code will check for an article excerpt, and it one exists it will be outputted. If the article does not have an excerpt, then a check for the article author will happen. A keen marketing person will have decided that all of Alice’s posts should have some sort of branding attached to them, and the substituted text will be prepended appropriately.
There are no realistic limits on how far you can go in tag nesting. There will come a point, somewhere way down the line, that your web server memory might be exhausted by hundreds of concurrent conditional checks, so keep your eyes on Textpattern memory usage as you’re developing your Textpattern sites.