Textpattern CMS conditional tags: nesting in action

admin-ajax

At a base level, Textpattern CMS conditional tags check on the status of a thing (i.e., is the thing an article, is there an excerpt with this article, is the HTTP response code 200, and so forth), evaluate them as true or false, then either do a thing that you ask or do nothing. That’s about it, really. With that in mind, it’s sometimes challenging to talk about certain Textpattern CMS tags in depth, especially conditional tags, because there is no real depth to them. Conditional tags work in pairs with the enclosed stuff dictating what should happen when a condition evaluates as true, and if you throw in some `<txp:else />` you can choose what happens when condition doesn’t evaluate to true, which covers all your bases.

There you go – conditional tags. Thanks for listening.

The mentally-taxing part of conditional tags comes when you start nesting tags inside tags. This is one of my favourite parts of building Textpattern sites, and I admit to feeling like a Merlin-esque wizard when a particularly fancy piece of code works out as it should. Take this beauty, for example:

<title>
<txp:if_status status="200">
<txp:if_article_list>
<txp:if_author>
Author: <txp:author />
<txp:else />
<txp:if_category>
Category: <txp:category title="1" />
<txp:else />
<txp:if_search>
Search: '<txp:search_term />'
<txp:else />
<txp:if_section name="">
Home
<txp:else />
<txp:section title="1" />
</txp:if_section>
</txp:if_search>
</txp:if_category>
</txp:if_author>
<txp:else />
<txp:title />
</txp:if_article_list>
<txp:else />
<txp:error_status />: <txp:page_url />
</txp:if_status>
&middot; <txp:site_name />
</title>

Those 28 lines of code live in the `<head>` section of Textpattern page template for a website I’m rebuilding from scratch. It’s important to me that humans are the top of the priority list when it comes to building websites. Search engines are also important, too, of course, but if humans can’t work out what your site is about, search engines are wasting their time. That block of code handles the `<title>` part of the page. The brief that I set myself was specific:

  • articles should use their given title
  • sections should use their given title
  • the homepage should be indicated as such
  • search results should be indicated as such, and include the search term
  • category article lists should be indicated as such, and include the category name
  • author article lists should be indicated as such and include the real name the author
  • error pages should show the http response
  • the site name should be appended to all of the above

From there, it was a case of scribbling down some logic and building the blocks. I’m sure the code above can be further optimised, but for me it treads the line between function and form; it’s straightforward to understand, and achieves everything it sets out to do without excessive code cruft.

There are six `<txp:else />` statements. If a section is being viewed, that’s the deepest part of the code being used. To get that section title, this is what happens:

  • the `<title>` tag is inserted (line #1)
  • check if the status is 200 (line #2) – yes; continue
  • check if this an article list (line #3) – yes; continue
  • check if this is an author list (line #4) – no; skip line #5
  • continue to the `<txp:else />` on line #6
  • check if this is an category list (line #7) – no; skip line #8
  • continue to the `<txp:else />` on line #9
  • check if this is a search results article list (line #10) – no; skip line #11
  • continue to the `<txp:else />` on line #12
  • check to see if this is the homepage (line #13) – no; skip line #14
  • continue to the `<txp:else />` on line #15
  • display the section title (line #16)
  • close the `<txp:if_section>` conditional (line #17) opened from line #13
  • close the `<txp:if_search>` conditional (line #18) opened from line #10
  • close the `<txp:if_category>` conditional (line #19) opened from line #7
  • close the `<txp:if_author>` conditional (line #20) opened from line #4
  • ignore line #21 because it is an article list, which was opened at line #3
  • ignore line #22 for the same reason
  • close the `<txp:if_article_list>` conditional (line #23) opened from line #3
  • ignore line #24 because the http status was 200, which was opened at line #2
  • ignore line #25 for the same reason
  • close the `<txp:if_status>` conditional (line #26) opened from line #2
  • insert a `&middot` character and then the site name (line #27)
  • the `<title>` tag is inserted (line #28)

Those 28 lines might display something as simple as `Blog – My Website` in this example, but it also catches all the other permutations of what Textpattern can output, which makes it even more fulfilling and 48% more amazing.

Now, if you’ll excuse me, I’m going to return to my castle to put on my robe and wizard hat.

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 *