Textpattern CMS conditional tags:


In my previous article, I explained the most important Textpattern CMS conditional tag: `<txp:else />`. Now that’s ticked off, it’s time to get into the details of `<txp:if_individual_article>`. Conditional tags are, by their design, intended to check for one or more given conditions and then do stuff based on the findings of those checks. In the case of `<txp:if_individual_article>` the check that takes place is, as the name implies, for an individual article.

A Textpattern-rendered page can be one or more of the following (not comprehensive – I’m going to save the absolute, all-encompassing list for another article):

  • an individual article
  • an article list
  • an article list in a named section
  • an article list by one or more named authors
  • an article list by one or more categories
  • an article list of search results
  • an article list on the site’s home page

Suffice to say, `<txp:if_individual_article>` will evaluate to true on any article that’s being viewed by the user agent (browser). Anything being viewed that’s considered an article list – so, the majority of the list above – will mean the check will evaluate to false. This is a particularly useful tag to use inside article forms, especially with the `default` form which is used for lists and individual articles. Take the following code, lifted straight from the `default` article form:

<!-- if this is an individual article then add the comments section via form: comments_display.article.txp -->
<txp:article form="comments_display" />

Phil Wareham, the Textpattern design dude, has helpfully added a comment to the code to aid understanding. In this case, an individual article check happens and the result dictates whether the comments should be displayed. The comments list is only displayed if it’s an individual article, so although displaying the article list will run the check, it will evaluate to false and the comments will not be displayed if it’s an article list.

Another section of code from the same form:

<!-- if not an individual article then make the title h1 a link -->
<h1 itemprop="name"><txp:title /></h1>
<txp:else />
<h1 itemprop="name"><a href="<txp:permlink />" itemprop="url"><txp:title /></a></h1>

Again, there’s some helpful and concise commentary going on here. In each case, whether the thing being displayed is an article list or not, the article title will be an `<h1>` headline. If it is an individual article, it will not be hyperlinked. If it is not an individual article (i.e., an article list), then the title will be hyperlinked and have additional `itemprop` information inserted into the markup.

Similarly, toward the top of the default page (not article form, to avoid confusion), there’s a check to define the canonical URL:

<link rel="canonical" href="<txp:permlink />">
<txp:else />
<link rel="canonical" href="<txp:section url='1' />">

Same arrangement: do a check, then insert code that does a certain thing based on the results of that check. In this case, if it’s an article, then the canonical URL is the permanent link to the article, and if it’s a list the URL is to that particular list.

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 *