SSI:Conditional Directives

In our discussion of the SSI #config directive we noted that simple time format directives were incapable of correctly formatting dates in the format 3rd of October 2007, 21st of October 2007 etc. This can be accomplished using SSI conditionals. For instance, the date today - on our servers - is the 4th of February 2012. The date was inserted dynamically into this document using SSI. The SSI code used for the purpose is listed below

1.   <!--#config timefmt="%e" -->
2.   <!--#set var="day" value="$DATE_LOCAL" -->
3.   <!--#config timefmt="%B" -->
4.   <!--#set var="month" value="$DATE_LOCAL" -->
5.   <!--#config timefmt="%Y" -->
6.   <!--#set var="year" value="$DATE_LOCAL" -->
7.   <!--#if expr="$day=/ 1/" --><!--#set var="after" value="st" -->
8.   <!--#elif expr="$day=/ 2/" --><!--#set var="after" value="nd" -->
9.   <!--#elif expr="$day=/ 3/" --><!--#set var="after" value="rd" -->
10.  <!--#elif expr="$day=/21/" --><!--#set var="after" value="st" -->
11.  <!--#elif expr="$day=/22/" --><!--#set var="after" value="nd" -->
12.  <!--#elif expr="$day=/23/" --><!--#set var="after" value="rd" -->
13.  <!--#elif expr="$day=/31/" --><!--#set var="after" value="st" -->
14.  <!--#else --><!--#set var="after" value="th" -->
15.  <!--#endif -->
16. <!--#echo var="day" --><!--#echo var="after" --> of <!--#echo var="month" --> <!--#echo var="year" -->

The logic underlying this code will be evident to most programmers. Nevertheless the syntax is somewhat arcane and merits some explanation

One final word on the subject - like any technology Server Side Includes can be used inappropriately. The present example sails very close to being inappropriate use. The date formatting done here calls for the execution of a CGI script. Parsing a lengthy HTML document and evaluating multiple SSI directives is far less efficient than running a short PHP script.

Jump To...

Colophon