Textpattern tips, tutorials and code snippets

From-to copyright dates

This tip is based on an earlier tip by Yiannis Colakides (now in the archives).

Strictly speaking, whenever you edit a text, you should update the copyright notice to match the current year i.e © 2016-2019. In the past, one could use the sed_copyright plugin but this is no longer maintained.

Nowadays, we can achieve the same without the need for plugins. Here are two examples:

Posted and last updated years

To output a copyright date for when an article was posted and when it was last updated, use Textpattern’s txp:posted and txp:modified tags. The output of both tags can be customised using the format attribute with a strftime code, in this case %Y to denote the year as four digits.

Outputting these two tags is straightforward. The trick is to only output the updated year if it is different to the year in which it was posted. For this we need to compare them using txp:evaluate.

<txp:evaluate query='"<txp:posted format="%Y" />" = "<txp:modified format="%Y" />"'>
© <txp:posted format="%Y" />
<txp:else />
© <txp:posted format="%Y" />-<txp:modified format="%Y" />
</txp:evaluate>

This will produce © 2016 if the article hasn’t been modified or was modified in the same year, and © 2016-2019 if it was last updated in 2019.

Posted date and current year

The txp:evaluate tag can also be used to process and output selected php functions if you register them first in Admin › Preferences › Advanced options to enable their use with txp:evaluate.

If you don’t see the Advanced options preferences, you first need to make them visible: visit Admin › Preferences › Admin and set “Advanced options” to “on” and save the settings.

  1. In Admin › Preferences › Advanced options, add safe_strftime in the field “PHP functions enabled in txp:evaluate” and click “Save”.
  2. Now you can output the current year as <txp:evaluate query="safe_strftime('%Y')" />.

As with the example above, you need to avoid showing the current year if it is the same as the article’s posted year. The pattern is almost identical to above:

<txp:evaluate query='"<txp:posted format="%Y" />" = safe_strftime("%Y")'>
© <txp:posted format="%Y" />
<txp:else />
© <txp:posted format="%Y" />-<txp:evaluate query="safe_strftime('%Y')" />
</txp:evaluate>

You can shorten this further by just testing for the not matching case:

© <txp:posted format="%Y" /><txp:evaluate query='"<txp:posted format="%Y" />" = safe_strftime("%Y")' not>-<txp:evaluate query="safe_strftime('%Y')" /></txp:evaluate>

This will produce © 2016-2024 if the current year is 2024.

Other methods

In place of txp:evaluate, you can also use a combination of txp:variable to store the last modified year (or the current year) and if_variable to compare whether it matches the posted year. This method also works in earlier versions of Textpattern prior to v4.7 where txp:evaluate wasn’t available.

<txp:variable name="end_year" value='<txp:modified format="%Y" />' />
<txp:if_variable name="end_year" value='<txp:posted format="%Y" />'>
© <txp:posted format="%Y" />
<txp:else />
© <txp:posted format="%Y" />-<txp:variable name="end_year" /> 
</txp:if_variable>

And if you want to use the current year as the end year, just change the first line to:

<txp:variable name="end_year" value='<txp:php>echo safe_strftime("%Y");</txp:php>' />

txp:evaluate versus txp:php

As we have just seen, it is also possible to use <txp:php>echo safe_strftime("%Y");</txp:php> to output the current date instead of registering the tag with txp:evaluate. So, when is it better to use txp:evaluate and when txp:php?

txp:evaluate – Only “Publishers” and “Managing Editors” are permitted to use PHP in articles. Likewise PHP can optionally be switched off in both articles as well as page and form templates in the Admin › Publish settings. In such cases, code contained in txp:php tags will not execute. While this situation may seem unlikely, it also applies to self-made short-tags that use txp:php in their code. That is a more likely scenario. In these cases txp:evaluate with the corresponding PHP function enabled in the settings is the better option.

txp:php – If you are developing a theme that you plan to make available to others to download and use for their own sites, you cannot enable php tags for txp:evaluate as part of the theme. In such cases txp:php may be more useful. Alternatively, provide instructions to potential theme users to enable the respective php functions in the settings.
The other obvious situation where txp:php excels is when you need to include entire blocks of php code.