WCTL Server-side Scripting

WCTL Conditionals

Introduction
Distinguishing between Guests and Registered Users
WCTL Conditionals
More Ways to Test to See If a User is a Guest
Other Conditional Tests
All the Operators
Comparing Strings
Comparing Dates
Troubleshooting
Resources
Next Section

Introduction

In the previous section, you learned to set a variable and create and display a visitor counter using Web Crossing Template Language (WCTL).

In this section you will gain further control over Web Crossing behavior by using conditional expressions. Using conditionals you can, for example, control what different users see depending whether they are registered users or guests, by what group they are a member of, etc.

Distinguishing between Guests and Registered Users

In the first section, we saw we could display a customized greeting to a user by editing a folder header and including a line such as:

Welcome, %% user.userName %%!

The WCTL expression %% user.userName %% gets replaced automatically by the current user's user name. For example, if the user's name is Doug, then Doug would see

Welcome, Doug!

upon entering the folder.

But suppose the current user is a guest user - somebody who has not logged in to your Web Crossing site? What happens then? In that case there is no user.userName, so a guest user entering the folder would see the rather ungainly greeting:

Welcome, !

What you would really like is to display a message appropriate for guest users, such as message telling them to please login or register.

You can accomplish this by testing to see whether the current user is a guest or not and showing one message to a guest and the customized welcome message to a registered user. There are various ways of doing this. One way is simply to test to see whether user.userName has a value before using it.

Enter the following WCTL into a test folder header as sysop and enter the folder to see the result:

%% if user.userName %%
Welcome, %% user.userName %%!
%% else %%
You are visiting as a guest user. Please login or register.
%% endif %%

While you are still logged in as sysop, you will see the greeting message

Welcome, sysop!

But what will a guest user see? To test this, you want to enter the same folder, but this time as a guest user - somebody who is not yet logged in. There are various ways of accomplishing this, but for now just quit your browser and start it up again, then reenter the folder. Quitting your browser will ensure that you are no longer logged in as sysop. This time, when you enter the room you will see the message

You are visiting as a guest user. Please login or register.

Note: Very often when testing WCTL or setting access permissions you will want to see what the results look like both as sysop and also as an ordinary registered user or guest. There are several ways of accomplishing this. One way (though a bit cumbersome) is to do what we did above - just quit the browser and restart it. This assures that you get logged off and start over as a guest user. Another way is to keep two browsers open - for example one Internet Explorer browser and one Netscape browser. Each browser accesses using its own Web Crossing certificate so you can be "in two places at the same time." A third way is to to turn off cookies at your site and then access at the top level. This will force you to login again or login as a guest user. A fourth way is to actually "logoff" using the logoff macro. We will show you how to do that in a later section, when you learn how to add your own customized macros to Web Crossing.

Let's go through the 5 lines of WCTL to see how this worked:

  1. %% if user.userName %%
    This line is the beginning of a conditional test. The if keyword means that a condition is being tested to see whether it is true or not. In this case, we are seeing "if user.userName", which just means "if user.userName has a value...."

  2. Welcome, %% user.userName %%!
    If the condition is true (in this case, if user.userName has a value) then this next line is executed and the welcome message is displayed.

  3. %% else %%
    The else keyword marks the beginning of what you want executed if the condition is not true.

  4. You are visiting as a guest user. Please login or register.
    In this case, if there is no user.userName value then we know the current user is a guest and display an appropriate message.

  5. %% endif %%
    The endif keyword marks the end of the conditional.

So a conditional reads, in English, like "If such-and-such is true then do this, else do this other thing." If you think about it like that it is easy to remember.

WCTL Conditionals

In general, the conditional has the format:

%% if condition-to-be-tested %%
   do this
   and this
   and this too
   as many things as you want to do
%% else %%
   do this other thing
   and this other thing too
   I can do as many other things as I want
%% endif %%

If the condition-to-be-tested is true then everything between the if line and the else line are executed. Otherwise everything between the else line and the endif line are executed.

The else part is optional. You can decide to do nothing at all if the condition-to-be-tested is not true. In that case, the conditional looks just like:

%% if condition-to-be-tested %%
   do this
   and this
   and this too
   as many things as you want to do
%% endif %%

Note: To make things easier to read it is common practice to write your separate WCTL statements on separate lines and indent separate sections inside conditionals. But this is just a style convention. Web Crossing actually ignores all extra blank spaces and line breaks (unless they are inside quotes). So you could actually write the above WCTL all on one long line if you wanted to. We recommend keeping your WCTL as easy to read as possible. It doesn't take any more space or time, and it will make things easier for you to edit later on.

Let's say you wanted to print a message only in the case in which the current user is not a registered user, but nothing to a registered user. In other words, you want to execute only the else part of the conditional above.

To do something like this you can test to see if the user.userName does not have a value. There is a special operator (a symbol used for conditional tests or doing numerical calculations, like the plus operator in addition: +) that means "not." The symbol for "not" is the exclamation point - !.

Try replacing your test folder's header with the following WCTL:

%% if !user.userName %%
   You are visiting as a guest user. Please login or register.
%% endif %%

If you enter the folder as sysop, or as any other registered user, you will see no header message at all. However, if you enter as a guest user you will see the message inviting you to login or register.

The first line

%% if !user.userName %%

is the key to it all. In English we would read this line as, "if not user.userName". In other words, if there is no value for the user.userName this express is true.

Note: OK. We admit it. You have to think in double-negatives to fully understand that not nothing is true. This is one of those things that confuses people who start programming, because in ordinary English we usually do not use double-negatives. But in math and programming we often do. And it makes sense, logically, if you think about it.

One day while going up the stair,
I met a man who wasn't there.
He wasn't there again today.
I wish to God he'd go away.

More Ways to Test to See If a User is a Guest

By the way, there are several different ways of testing to see whether a user is a guest or not. We looked to see if the userName had a value or not. We could also have tested to see if the user was registered directly by testing the user property userIsRegistered:

%% if !user.userIsRegistered %%
   You are visiting as a guest user. Please login or register.
%% endif %%

Or we could have avoided using the ! not operator by directly testing to see if the user was unknown by using the userIsUnknown property:

%% if user.userIsUnknown %%
   You are visiting as a guest user. Please login or register.
%% endif %%

For a complete list of all the built-in variables, see the sysop reference page on Web Crossing Commands and Variables. You will find a complete list there of all the WCTL commands and variables by function and also in alphabetical order.

Note: Do not test for an unknown user by using the userIsGuest variable. In WCTL a "guest" actually means something a bit different from an "unknown" user. The expression %% if user.userIsGuest %% is only true if the current user has not logged in and has posted a message as a guest user - which is allowed if you allow guest users to post messages on your system.

Other Conditional Expressions

So far we have just tested to see if a value exists or doesn't exist. While writing your WCTL scripts you will find that you want more flexibility over your conditional tests. You will often want to see if two values are equal, if one value is larger than another value, and so on. There are special operators besides the not (!) operator that help you with these tests.

Comparing two values for equality

Place the following WCTL into a test folder header and look at the result by entering the folder:

%% if user.userName == "Doug" %%
   You are Doug!
%% else %%
   You are not Doug!
%% endif %%

If you are looking at the test folder as sysop then you should see the "You are not Doug!" message. If you create a test user called "Doug" (always handy!) then you should see the message "You are Doug!".

The conditional operator used here is called the equality operator and is a double-equals mark: ==

Comparing two values for Inequality

The opposite of the equality operator is the inequality operator !=, which looks like a hybrid between the not operator ! and the equality operator ==. Try switching the logic in your test folder header, as in the following WCTL:

%% if user.userName != "Doug" %%
   You are not Doug!
%% else %%
   You are Doug!
%% endif %%

Note: If you have done any C or JavaScript or Java programming, the conditional operators used in WCTL are the same operators.

Comparing two values to see which is greater (larger)

You can also compare two values to see if one is larger or smaller than the other. Place the following WCTL in your test folder header. In this example we are also setting a couple of local variables to get more experience in creating and handling variables.

%% set a 5 %%
%% set b 6 %%
%% if a < b %%
   a is less than b
%% elseif a == b %%
   a equals b
%% else %%
   a is greater than b
%% endif %%

Try changing the values of the variables a and b and reentering the folder to see the different results.

In this conditional two new things are introduced:

  1. The < operator is called the less-than operator and is used to test to see if the left-hand value is less than the right-hand value. In other words, a < b is true if the value of a is less than the value of b.

  2. In addition to the if section and else section, we have included an elseif section. This is useful if you want to have several sections of WCTL and execute just one of them depending on multiple conditional tests. In plain English this conditional just reads like, "if a is less than b do this, else if a equals b do this other thing, else (a is neither less than or equal to b) do this last thing."

All the Operators

Here is a summary of the all the conditional operators you can use for testing equality, inequality and comparing which value is greater or smaller:

Operator

Name

Example

Meaning

!

not

!user.userName

returns true if the value being tested has no value or is zero

==

equality

user.userName == "Doug"

returns true if the two values being compared are equal

>

greater than

a > b

returns true if the first value is greater than the second value

<

less than

a < b

returns true if the first value is less than the second value

>=

greater than or equal to

a >=b

returns true if the first value is greater than or equal to the second value

<=

less than or equal to

a <=b

returns true if the first value is less than or equal to the second value


Comparing Strings

We used the equality operator == to check to see if two character strings were equal to each other with the expression %% if user.userName == "Doug" %%. You can also use the > and < operators with character strings to see which character string comes first in alphabetical order.

Try the following WCTL in your test folder header and see the results. Try changing the character strings to test different outcomes:

%% set a "Doug" %%
%% set b "Sue" %%
%% if a < b %%
   %% a %% comes before %% b %%
%% elseif a == b %%
   %% a %% and %% b %% are the same
%% else %%
   %% a %% comes after %% b %%
%% endif %%

Comparing Dates

Since Web Crossing is a web conferencing system at its core, you will likely find it necessary to do conditional tests on dates. For example, you might want to test a message creation date to see if it is before or after a certain date. Or you might want to see if a user has logged in after a certain date.

For example the expression,

%% if user.lastLogin > mm/dd/yyyy.hh:mm:ss %%

will test the last login date and time of the current user and return true if it is greater than (more recent than) the date and time specified by mm/DD/yyyy.hh:mm:Ss The different parts of the date expression are:

For example, right now (even as we write this!) it is January 3, 2000 at 6:24 pm (18:24) and, oh, 40 seconds. In WCTL this can be written as

01/03/2000.18:24:40
The next section will show you how to automate repetitive tasks using programmable loops.

Troubleshooting

I was not able to enter the test folder as a guest.

I tried the examples above, but when I look at the header all I see is the WCTL itself, with all the double-percent marks. What am I doing wrong?

Resources

Web Crossing FAQ:

Sysop docs:

WCTL Concept Reference Page

Web Crossing Tech Support Forum

Developer Center

WebX Harbor