Conditional tags

2013-01-17 17:39

Keywords: tag, condition, else

As a byproduct of etc_query XPath evaluation ability, you can easily construct conditional “tags”. Let us see some examples.

Suppose you need to output some text every Friday the 13th. A quick look at php date functions shows that Fridays satisfy date('l')='Friday', and the 13th of each month reads as date('j')=13. They can be combined into date('l j')='Friday 13'. That’s all we need:

<txp:etc_query query="date('l j')='Friday 13'" functions="date">
  Bad day for paraskevidekatriaphobia...
<txp:else />
  It's not Friday the 13th today.

which results in

It's not Friday the 13th today.

We can also construct complex conditions and inject some article and other data into query. For example, to check if this article has been written a Saturday night, call

<txp:etc_query query="date('w', {?posted})=6 and date('G', {?posted})>22" functions="date">
  Do not take this article too seriously.