Help Search Campus Map Directories Webmail Home Alumnae Academics Admission Athletics Student Life Offices & Services Library & Technology News & Events About the College Navigation Bar
MHC Home Using the Document Interface

Conditional Commands

Syntax:

.IF condition or .IFDEF name or .IFNDEF name
...code...
.ELSIF condition or .ELSIFDEF name or .ELSIFNDEF name
...code...
.ELSE
...code...
.ENDIF

Entire sections of HTML code can be removed from final output based on some condition by using the .IF commands and their other components.

The .IF and .ELSIF commands test for a true logical condition. The .IFDEF and .ELSIFDEF commands test to see if a variable has been defined (created) yet. .IFNDEF and .ELSIFNDEF test to see if a variable has not been defined yet.

A condition is passed to .IF or .ELSIF. This is usually the result of some logical expression, but can actually be any value at all. The code contained within the .IF will not be evaluated if the condition is either the number 0, an empty string, or any of the strings "no", "off", or "false" (case does not matter.) This means that all of the following examples produce the same result:

<!--.if 0-->
<!--.if fALse-->
<!--.if 1==99 || 4+4==9-->

In its simplest form, the .IF command is followed later on by just an .ENDIF command:

<!--.if $count==1000-->
   Hey! You're the 1000th visitor!
<!--.endif-->

One or more .ELSIF and .ELSE commands can also be added. .IF commands can even be nested. This example gets the day of the month from the $Date variable and translates it into an ordinal numer (1st, 2nd, etc.) Don't worry if you don't know what the first command does; it sets the $DateFmt variable so that $Date will be only the day of the month. The second command gets just the last digit of the day of the month ($Date modulo 10):

<!--.set datefmt="\%d"-->
<!--.set digit=$date%10-->
<!--.if $date>=4 && $date<=19 || $digit>=4 && $digit<=9 || $digit==0--> 
   <!--.set suffix="th"-->
<!--.elsif $digit==1-->
   <!--.set suffix="st"-->
<!--.elsif $digit==2-->
   <!--.set suffix="nd"-->
<!--.else-->
   <!--.set suffix="rd"-->
<!--.endif-->
<!--.echo "Today is the $date$suffix day of the month."-->

Today is the 13th day of the month.

Of course, the wonderful thing about the way this works is that the extra code never even gets sent to the person reading the document. This means that anyone who tries to read the source to your file can't see all of the code above. They just see the result.

The other versions of .IF -- .IFDEF and .IFNDEF -- use the name of a variable as their argument. This is the name of the variable, not its value, so do not include a $ in front of the name. These test to see if a variable with the given name exists. When using the .IFDEF command, the statement following it is executed if the variable does exist. .IFNDEF (if NOT defined) reverses this logic.

The most common time you would want to use this option is in conjunction with the .CGIVARS command. For instance, if you wanted to execute some code depending on whether or not the user had passed the parameter myname to the document as part of a CGI form, you could do this:

<!--.cgivars-->
<!--.ifdef myname-->
   <!--.echo "Your name is $myname"--> 
<!--.else-->
   <!--.echo "You didn't tell me your name"-->
<!--.endif-->

For another example of using the .IF command, see the examples in the section on Variables.

----------------------------------------

Home | Directories | Web Email | Calendar | Campus Map | Search | Help

About the College | Admission | Academics | Student Life | Athletics
Offices & Services | Giving | News & Events | Alumnae | Library & Technology

Copyright © 2000 Mount Holyoke College. This page created by the OIS Operations Group and maintained by Webmaster. Last modified on July 11, 2000.