Home     SSI Lab     CSS Templates     CSS     Htaccess     Web Design     XHTML

X-SSI Lab

graphic-img   



SSI Conditional Expressions - Flow Control Elements

Bookmark with StumbleUpon
del.icio.us
Digg it
reddit
Furl
Spurl
ma.gnolia
Yahoo MyWeb
Windows Live
Sponsors

Introduction

SSI allows conditional processing and provides a number of elements for this purpose.


The basic flow control elements are:

<!--#if expr="test_condition" -->
<!--#elif expr="test_condition" -->
<!--#else -->
<!--#endif -->


The #IF element works like an IF statement in programming. The test condition is evaluated and if the result is true then the text/lines following the IF statement is returned in the output until either on #ELIF, #ELSE or #END element is reached.

The #ELIF and #ELSE elements are used to output text/lines if the original #IF statement was false. If #ELIF is parsed the SSI will continue until either an #ELSE or #END element is reached, and if an #ELSE statement is parsed the SSI will continue until an #END element is reached.

The #END element closes the #IF element and must always be present or an error will likely occur.



The test_condition from the flow control code above is one of the following:

string1If string NOT empty result is true
string1=string2If string1 is equal to string2 result is true
string1!=string2If string1 is NOT equal to string2 result is true
string1<string2If string1 is less than string2 result is true
string1>string2If string1 is greater than string2 result is true
string1>=string2If string1 greater than or equal to string2 result is true
(test_condition)If test_condition TRUE result is true
!test_conditionIf test_condition FALSE result is true
test_condition1 && test_condition2If test_condition1 AND test_condition2 are true result is true
test_condition1 || test_condition2If test_condition1 OR test_condition2 are true result is true

"=" and "!=" bind more tightly than "&&" and "||". "!" binds most tightly.



Anything that is not recognisable as a variable or an operator is treated as a string. To ensure your variable is treated as a variable you should use the format ${var_name}

If a string is written as /string/ it will be treated as a regular expression so to test something of the form /home/path you will need to use an escape sequence \"/home/path\".



Using the above information you can create quite complex tests. Here's a simple example to get you started:



<!--#if expr="(${HTTP_USER_AGENT} = /Mozilla\/4/) &&
(${HTTP_USER_AGENT} != /MSIE/)" -->
 Netscape styles
<!--#elif expr="(${HTTP_USER_AGENT} = /Mozilla\/4/) &&
(${HTTP_USER_AGENT} = /MSIE/)" -->
   <!--#if expr="${DOCUMENT_URI}=/flashy-site/" -->
     Flasy MSIE stlyes
   <!--#elif expr="${DOCUMENT_URI}=/plain-site/" -->
     Plain MSIE styles
   <!--#endif -->
<!--#else -->
You must be using Opera or sumfin?
<!--#endif -->