|
As we covered in our eXtended SSI
Tutorial: Part I, tailoring content to your reader's browser
is a very useful function.
In this tutorial, you will be introduced to several new and important
concepts:
- The ELIF directive. For non-programmers, a funky concept,
but I know you will get the hang of it.
- The SET directive that allows you to place values into
variables that can be used later in your page.
- More complex logic, testing for more than one condition with
&& and ||. (AND and OR)
Before we get to work, of course I have a parlour trick for your
enjoyment!
|
Your Customized Page Segment
|
|
Oh well, your browser did not match any of the tests that
my server side includes are checking for. I do hope that your
browsing experience is nonetheless pleasurable.
|
|
What Exactly is an ELIF?
Why its the last thing a person says as an elephant sits on him!
It should only be that simple... The ELIF is used to nest
logic. It literally means ELSE IF. Thinking outloud:
If it is a dog
then bark
else if its a cat
then meow
else if its a bird
then chirp
else
then don't say anything
end if
This is easier than creating a seperate IF-ENDIF for every test. It
also allows us to have our handly ELSE catch-all at the end. In the
source code box that follows, you will see why this type of logic
is required.
Both Netscape and MSIE use the string Mozilla in their
HTTP_USER_AGENT. With WebTV, they use the string MSIE
in addition to WebTV. Getting some idea how much fun
this logic stuff can be? Here is how we attack the problem.
It is easiest to start working with the value that is most
unique. In this case it is WebTV. If we placed the WebTV
test below the MSIE tests, it could match on MSIE 3
and return the wrong result. So, if is not WebTV, we start
checking for MSIE.
Why MSIE first? As we just discovered, both MSIE and Netscape use
the word Mozilla in their HTTP_USER_AGENT. Therefore we
must again pick the most unique case first.
After we have exhausted our tests for MSIE, we then start checking
for Netscape by looking for Mozilla where we do not have
MSIE. Before we go further, please take a moment to examine
the source code below.
Note: The example below is general in nature. There are many
variations in browser names, and this example may not be specific
enough for your needs.
Be careful reading the source code. The \/ make look
like a V, but it is really backslash-forwardslash.
I See A Lot Of Weird Stuff in there
Sorry about that. Sometimes the best way to eat is to start with
desert. (at least my kids think so) Here is what is happening:
For each possible browser that we are testing for, we use a SSI
SET directive to place a value in a variable, or temporary
holding bucket. In this example our bucket is named brtype.
Once we are done checking for MSIE, we start looking for Mozilla
where our brtype does not match with MSIE. These hopefully are our
Netscapes. The != means NOT MATCHING, and the && is
computerese for AND. We do not use it in this example but
you should be aware that || (two pipe characters) means OR.
AND means that both conditions must be TRUE, and OR
means that one or the other, or both must be TRUE for the IF
to happen.
After we are done with our testing, we have an else to
handle anyother possible result such as Lynx and other less
often seen browsers and the ever growing family of spiders.
Finally, we have some sort of value in our brtype bucket.
The final line then uses that value to create an INCLUDE
directive to merge in a conveniently named file.
What Else Can I Do With This?
A great deal! You can use a logic structure such as this to set
values for everything from colors to text to whether or not to
embed JavaScript.
Keep in mind that since the page is parsed and created before
being sent to the browser, that you can even tailor the <HEAD>
section of your page <BODY> tag. This is an easy way to
customize the major settings of a page for each browser.
If you were to place that source code at the very top of your file,
you could very easily do this:
<!--#if expr="$brtype = /msie/" -->
<BODY BACKGROUND="I-Love-MS.gif">
<!--#elif expr="$brtype = /netscape/" -->
<BODY BACKGROUND="I-Love-Netscape.gif">
<!--#else -->
<BODY BACKGROUND="Get_a_Browser.gif">
<!--#endif -->
What Happens If I Do Not Use the Slashes?
Using the /slashes/ means you want to match a pattern. If
you leave the slashes off, then it must be a perfect match
to be TRUE. In our example code, we have three possible
values for netscape: 1, 2, 3,& 4. Therefore if we do
not use the /slashes/ we must test for netscape4 or netscape2
exactly.
Note: Browser versions change all the time, so even keeping
up with this can be a chore. This example does not take all browser
possibilites into account.
|