If-Then-Else Logic Part 2
 
    Understanding RXML Syntax for If-Then-Else Logic Part 2
By Joe Follansbee, joef@follansbeeconsulting.com
Introduction
As discussed in Part 1 of this tutorial, nearly every programming language makes use of conditional statements. Pages written for the Caudium Web server include tags written in Roxen eXtensible Markup Language (RXML), which make the pages behave like simple programs. In Part 1, I outlined the basic syntax for RXML if-then-else logic. In Part II, we'll look at some more advanced features which allow Web developers to test various specific conditions.
Assumptions
This tutorial assumes the reader has read part 1 and has a basic understanding of programming and markup languages, such as HTML. However, I've written the document so that a beginning developer can copy and paste code that will work immediately.
Requirements
  • Caudium 1.0.34 (or later)
  • Access to a Caudium virtual server file system (i.e., a directory where Web pages are stored)
  • A text editor
I suggest using the examples in this tutorial to practice using the RXML tags discussed. To test your code, simply create an HTML file containing the tags and load the page in your browser. Caudium or the browser will often tell you when something is wrong.
A Brief Review
The <if> tag is one of a group of RXML tags called "containers", which have an opening and closing. You can spot a container tag by the use of the forward slash, "/", in the closing tag. ("Standard" tags do not have a closing.) Let's return to the hungry programmer we met in part 1. He might write, for example:

<if variable="topping is veggie">You ordered the vegetarian topping for your pizza.</if>

Note the "variable=" syntax, which is an example of an "attribute". These attributes allow you to test many types of environmental and variable conditions.

Working With Variables
Variables are the heart of any programming language, and RXML includes some powerful tools for checking variables, including wildcards "*" and "?". "*" means "match zero or more characters here", while "?" means "match only one character in this spot." The absence of any character can be used to check for NULL values.

variable="name is pattern"

Programmers can check whether a variable exists and if so, they can test the value.

<set variable="pizza_crust" value="thick">
<if variable="pizza_crust is thick">You ordered thick crust pizza.</if>


Some variables have NULL values, meaning no information is available. You can check a NULL value with this code:

<if variable="deliver is ">Please go back and check whether you want your pizza delivered.</if>

Note the absence of any character after the space following "is".

match="string is pattern"

The "match" attribute works in a way similar to the "variable" attribute. In the following example, the programmer is doing some pattern matching on a value from a Web form, thus the use of the <formoutput> tags. The "?*" in the third line means match any character followed by any number of characters, i.e., match anything in the variable that's not a NULL value.

<formoutput>
<if match="#topping# is sausage">You prefer a sausage topping on your pizza.<if>
<elseif match="#topping#" is ?*">You don't care what topping is available. You just want pizza!</elseif>
</formoutput>
Authenticating Users
The <if> tag includes a number of attributes that can be used for authenticating users.

user="name" or user="any"

Coders use the "user" attribute to check for a specific authenticated user or for any authenticated user. This tag is most often used with Caudium's authentication modules.

<if user="any">
    <echo REMOTE_USER> is logged in.
    (Put secret information here)
</if>
<else>
<auth-required realm="authorization">
</else>
Don't put secret infromation here

This code displays the userid of the browser user, using the REMOTE_USER environment variable. The <auth-required> tag prompts the user for a login and password.

file="path/filename"

A developer can also use an external file to check whether a user is authenticated. The path to the file should reflect the computer's true file system, rather than the virtual server's virtual file system. The file is in this format:

user name : encrypted password
user name : encrypted password


Here's an example:

<if user="joe" file="/home/web/authenticate">You've been authenticated.</if>

wwwfile

If you include this attribute in your <if> tag, Caudium will look for the authentication file in the virtual server's virtual file system, rather than the computer's actual file system.

<if not user="joe" wwwfile file="/users/authenticate/groups">Only Joe is allowed to see these pages!</if>

group="group, groupfile path"

You can check to see whether a user is a member of an authenticated group, as listed in a file. The group file uses this format:

group1 : user1, user2, user3
group2 : user1, user2


For example:

<if user="joe" group="group1, /home/users/authenticate">You're in group1. Welcome!</if>
Working With date and time
The values of particular dates and times are often important to a Web developer. In RXML, you can display material depending on the date. For example:

<if date="20020210">You've won a free pizza!</if>
<if time="0000">It's the witching hour.</if>


before, after and inclusive

RXML developers test whether a user is looking at a Web page before or after a specified date or time.

<if before date="20021225">You still have time to finish your Christmas shopping.</if>

<if after date="20020214">You're girlfriend is mad at you for missing Valentine's Day!</if>

<if after inclusive date="20020101">Happy New Year!</if> ("Inclusive" means the text will be displayed on January 1, as well as after January 1.)

<if before date="20020601">
    This site will be launched on June 1, 2002 at 12:00. Please return at that time.
</if>
<elseif date="20020601"
    <if before time="1200">
        This site will be launched today at 12:00. Please return at that time.
    </if>
</else>
Looking at Browsers
Large Web sites that see traffic from hundreds or thousands of users a day have to cope with a variety of Web browsers, some of which may not support the features your site requires.

accept="type"

The "accept" attributes returns true if the browser accepts certain content types (also called "MIME types") as specified by the browser's "Accept" header.

<if accept="image/jpg">Your browser can display .jpg images</if>

For a comprehensive list and description of registered content types, see ftp://ftp.isi.edu/in-notes/iana/assignments/media-types/

cookie="name is value"

Web developers use browser cookies to store certain kinds of information on the user's hard drive, such as an encrypted user name. The "cookie" attribute lets you test whether a named cookie exists and whether the cookie's value is something in particular.

<if cookie="user">You have already registered
<if cookie="user is Joe">You have registered with the name "Joe".</if>
</if>


Note that a browser must be set to accept cookies for the second test to work properly.

domain="pattern"

Web developers may want to show certain pages to users from a specific domain. The "domain" attribute lets you test for this.

<if domain="www.caudium.*">You are very cool.</if>

This example lets the developer see whether the user came from www.caudium.org or www.caudium.net.

Note that domain names are resolved asynchronously. This means that the first time someone views a page, the domain name may not have been resolved.

host="pattern"

Similar to the domain attribute, you can test the IP address of the computer making the Web page request with the "host" attribute.

<if host="192.168.*">You are not allowed to view this page!</if>

language="language"

Designers can direct users to pages in the user's language using the "language" attribute.

<if language="fr">Parley vous français?</if>

You can find a list of ISO 639 standard language codes at http://www.see-search.com/webdesign/iso639codes.htm

name="browser"

Many sites redirect users to specific pages depending on the browser they use, especially Netscape Navigator versus Microsoft Internet Explorer. Also, some developers redirect blind users with special browsers to text-only pages. The "name" attribute checks the "User-Agent" header in the browser.

<if name="microsoft_internet_explorer/???*"></if>
<if successful or name="mozilla*"></if>
<else>
You need to use Internet Explorer or Netscape Navigator to view this site.
</else


You can find a list of user agents at http://www.browserlist.browser.org/ .

referrer="pattern"

Many Web sites track traffic by looking at which search engines and their links referred users to a site. The "referrer" attribute can help you do this.

<if referrer="www.google.*">You were referred by the Google search engine.</if>

supports="feature"

Many Web sites require browsers to support frames for proper navigation. The "supports" attribute can check for specific features such as frames in the user's browser.

<if not supports="frames">You must use a browser that supports frames.</if>

There's a list of features ("supports classes") that Caudium can test at http://docs.roxen.com/roxen/1.3/creator/supports/classes.html .
Other </if> Attributes
config="name"

Caudium can store specific session configuration variables in a user's cookie. RXML developers do this with the <aconf> tag. The <if> attribute "config" can check for these variables.

<if config="joe"><set variable="pizza" value="pepperoni"></if>

define="define"

Many RXML developers write their own custom tags using the <define> tag. The "define" attribute checks whether a new tag exists.

<if define="new_tag">The tag "new_tag" has been defined.</if>

eval="RXML expression"

The attribute "eval" returns true if the RXML expression returns a string that evaluates to true if cast to an integer in Pike, the programming language Caudium was written in. The string would begin with numerals 1 through 9 or a number of zeroes followed by numerals 1 through 7 (octal greater than zero).

exists="path/filename"

The attribute "exists" checks to see if the file exists in the named path. If the path does not begin with a "/", Caudium assumes it exists relative to the directory containing the page with the <if> statement.

<if exists="/home/user/mbox">The user's mailbox exists</if>

filename="filepattern"

The "filename" attribute helps you to know whether a file is in the right place.

<if filename="/foo/*"><set variable="branch" value="pub-foo"></if>

prestate="option"

A prestate is a way to present options inside a URL that remain persistent for a user over several pages. Prestates can store session variables without setting cookies. A prestate is imbedded inside a URL. For example, http://www.caudium.org/(en)/mypage.html stores the prestate language option "en" in the URL. Prestate options only work in relative links.

To use the prestate attribute in an <if> tag, try this:

<if prestate="en">mypage.html</if>
Conclusion
Caudium's <if> tag contains nearly twenty different tests on a variety of common user variables and can be a powerful tool in your RXML bag.

Copyright 2002, Joseph G. Follansbee. The RXML code in this document is released under terms of the GPL. Thanks to Thomas Koester for his code suggestions.
 
HTML OK CSS