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
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:
- %% 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...."
- 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.
- %% else %%
The else keyword marks the beginning of what you want executed if the condition is not true.
- 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.
- %% 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.
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.
|
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. |
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:
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 |
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 %%
Note: Actually, all values in WCTL are character strings. For example, try the following WCTL in your test folder header:
Look at the result (a + b = 11). All values in WCTL are just character strings. Strings that look like numbers can also be used for numeric calculations. When you "set" a variable to a number you can leave out the double-quote marks around the number since the meaning is unambiguous. (In other words %% set a 5 %% is a convenient short-cut for %% set a "5" %%, though both mean the same thing.) However, when you set a character string like "Doug" to a variable, you must use double-quote marks around "Doug" because just Doug written by itself could also be a variable name. (Variable names always start with alphabetic characters, like Doug, D123, etc. On the other hand, you cannot have a variable name consisting just of numeric digits. For example, 123 cannot be misinterpreted as being a variable name - it is just a value.) By the way, this is different from most computer languages, like C and Java, which consider numeric values and alphabetic character strings to be different, and store them differently in variables. In C for example, the number 5 and the character "5" would be stored differently internally. In WCTL both are stored as just the character "5". WCTL will automatically convert the character 5 to a numeric value as needed when making numerical calculations. |
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:
mm
- a two digit monthDD
- a two digit dateyyyy
- a four digit yearhh
- a two digit hourmm
- a two digit minuteSs
- a two digit secondFor 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
The next section will show you how to automate repetitive tasks using programmable loops.
01/03/2000.18:24:40
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?
Web Crossing FAQ:
Sysop docs: