We've taken a brief look at variables when looking at @NAV files, but lets take a closer look at them.
In part 8 we introduced @IF and
@ELSE for basic checking (and selection) of variables, but there
is more we can do.
There may be times when you just don't know if you have a variable set or not.
This is something that can happen for a variety of reasons. One way round it would be to define every single variable you need in advance, set to a default value, and then change it as and when you need to.
This however is messy, and a much better way is to define a variable when you need it, and check it exists when you want to use it.
For example, suppose on the homepage we wanted a copyright statement to appear at the bottom of the page. We could of course put it with the main content, but what if we want to move it away from the content?
A very easy way would be simply to set a variable on the pages where we want the copyright statement to appear. So in contents.hitop, we'd set near the top, a variable like this:
<@SET NAME="PAGETYPE"
VALUE="INDEX">
We can then build into our template.hitop a routine which will only display our copyright statement if the page has the PAGETYPE variable set to be INDEX.
<@IF NAME="PAGETYPE" VALUE="INDEX">
<hr> ©2002 Andrew Bowden
</@IF>
Of course the PAGETYPE variable doesn't exist anywhere else, and if we tried to make the page now, we'd get the following error for the rest of the pages:
Undefined variable 'PAGETYPE' in "<@IF NAME="PAGETYPE"
VALUE="INDEX">
The way we check the variable exists is very easy. We simply use
<@IF NAME="PAGETYPE">
...
</@IF>
This simply checks if PAGETYPE exists, and if it does, proceeds to process the code inbetween the @IF and /@IF. So for our copyright example, all we need to do is extend the code slightly.
<@IF NAME="PAGETYPE">
<@IF NAME="PAGETYPE" VALUE="INDEX">
<hr> ©2002 Andrew Bowden
</@IF>
</@IF>
And there we go. If you take a look at our revised contents.html you'll see the new copyright statement in all its glory, and of course, only on that page!
Example files for this page are here