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
- For starters we use the #config directive to change the time format, and then use the #set directive to record the current day, month and year in variables bearing the same name.
- We can refer to any of the variables described earlier in our discussion on the SSI #echo directive.
- Variable assignments are made thus: value= "value". The use of quotes poses a problem. How is the SSI interpreter to know that we want to refer to a variable and not make a simple string assignment? This is accomplished by preceeding the variable name with the $ sign as in $DATE_LOCAL. The use of $ to designate variables should come as no shock to PHP programmers.
- If a literal dollar character is required, it should be escaped, i.e. written as "\$".
- Once we have correctly assigned a value to the variable $after we are in a position to construct the date string that will be echoed into the HTML document. We use the #echo directive to do this. Note that we do not precede variable names with the $ sign here - the #echo directive is intrinsically designed to echo the contents of variables, intrinsic or user-defined!
- The #if..#endif control structure is similar to the if statement found in other languages such as JavaScript, PHP and Delphi. Only the #if and #endif are obligatory. Apache will not complain if you leave out the #endif. However, if the #if evaluates to false you are liable to find that all HTML content after the #if is truncated!
- #elif is short for elseif. It corresponds to the individual case statements in the JavaScript switch control structure.
- #else corresponds to default in JavaScript switch.
- expr in the #if expression can be any logical expression - i.e. anything that evaluates to true or false. The operators, <, <=, =, >=, > and != can be used in the expression.
- On rare occasions Apache is unable to comprehend variable references such as $DATE_LOCAL and treats them as strings. Should you run into this problem simply wrap the variable name in braces. For instance, ${DATE_LOCAL}.
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.