spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / ssi / intro To page 1current page
[previous]

Webmaster's Guide to Server Side Includes

ASP 3.0/.NET Developer
Jupitermedia
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume
Developer News
SaaS Tool Offers Custom Database Development
Microsoft’s Automated Agent: Can We Talk?
Borland Finally Sells CodeGear

Conditional SSI

Most of the commands above are useful, but still haven't scratched the surface of the power of SSI. What if you are having trouble with CSS/browser incompatibility and need some relief? This kind of problem is perfect for conditional SSI. Conditional SSI use the same if/else control structures that many other programming languages use. However, since HTML doesn't have logic of its own, the if/else statements need to be commented out, just like in other SSI directives. Here is an example of a Conditional SSI that displays one style sheet for non -MSIE version 4 browsers (Netscape 4) and another style sheet for all other browsers.

<!--#if expr="(${HTTP_USER_AGENT} = /Mozilla\/4/) && (${HTTP_USER_AGENT} != /MSIE/)" -->
   <LINK rel="stylesheet" type="text/css"   
   href="netstyle.css">
<!--#else -->
   <LINK rel="stylesheet" type="text/css"  
   href="nonnetstyles.css">
<!--#endif -->

When looking at this example, first notice the <!--#endif --> directive at the end of the conditional logic. The meaning of this directive is obvious; however, note that the logic won't work if <!--#endif --> is left off. The other thing to notice is that the "if" statement says, "if Mozilla 4 and not MSIE(Internet Explorer or Opera)." The "not Internet Explorer" part is necessary since the HTTP_USER_AGENT value for IE 4 and above contains the string Mozilla/4.0. To make things a little clearer, here is the output of the HTTP_USER_AGENT environment variable for Internet Explorer 5.5:

Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)

and Opera 5: [but only when Opera is set to IE compatibility mode - Ed.]

Mozilla/4.0 (compatible; MSIE 5.0; Windows NT 5.0) Opera 5.02  [en]. 

To output the value of the HTTP_USER_AGENT variable to your browser you can use the echo command mentioned above.

The browser example above can easily be extended; consider the case where you want three different style sheets for three different environments. Let's say you want one style sheet for all HTML files displayed in Netscape, one for HTML files in your /graphics directory when displayed in non-Netscape browsers and one for HTML files in all other directories when displayed in non-Netscape browsers.

<!--#if expr="(${HTTP_USER_AGENT} = /Mozilla/) && (${HTTP_USER_AGENT} != /MSIE/)" -->
   < !--#include virtual="netstyle.css">
<!--#elif expr="${DOCUMENT_URI} = /^\/graphics\/.*/"-->
   < !--#include virtual="graphstyles.css">
<!--#else -->
   < !--#include virtual="nonnetstyles.css">
<!--#endif -->

There can only be one else directive in each if/else control structure. If you want more conditions you need to use the elif directive. Unlike the else directive, the elif directive can be used as many times as you like. In addition, the syntax of the elif directive is more similar to the if directive than it is to the else directive. The elif directive takes arguments just like the if directive. In the example above the DOCUMENT_URI environment variable is used to match the /graphics directory. Note that you can use regular expressions in conditional SSI statements. Note also that the style sheets are embedded style sheets saved as separate SSI, instead of linked style sheets like in the first example

On Including an Include

By now you can probably see the power of SSI, but you're thinking, "what if I want to include an include in an include?" The answer is, "you can and it'll work" and you can include an include in an included include too. You can recursively include includes until your server cuts you off. That is, the number of layers of inclusion you are allowed is determined by a setting in the config files of your Web server. While it might be fun to think about infinite levels of inclusion, it certainly won't help the performance of your Web site. I know from experience that two layers of inclusion won't cause a problem. Here's an extension to the style sheet example above using two layers of inclusion. Say you don't want to include the long list of directives below in your HTML page:

<!--#if expr="(${HTTP_USER_AGENT} = /Mozilla/) && (${HTTP_USER_AGENT} != /MSIE/)" -->
   <!--#include virtual="/netstyle.css">
<!--#elif expr="${DOCUMENT_URI} = /^\/graphics\/.*/"-->
   < !--#include virtual="graphstyles.css">
<!--#else -->
   < !--#include virtual="nonnetstyles.css">
<!--#endif -->.

Instead, you could save the long list in a file named include.html. Then you could include the below directive in place of the long list above in your HTML page:

<!--#include virtual="/directory/include.html" -->

The end result of the doubly included file will be the same as the first triple style sheet example. But in this case, you will have made your site easier to maintain by putting your logic and style sheet names in an external file. This way, if you change the logic or name of your style sheets, you only need to edit one file instead of many. Again, the benefits of SSI are easily recognizable. However, be careful when you write SSI.

Common Pitfalls

There are two common pitfalls when trying to use SSI.

Summary

In conclusion, SSI are a very useful tool for Web development. SSI can provide simple dynamic content without the use of CGI or ASP. SSI allow you to make site-wide changes by updating only one include file. In addition, SSI syntax has simple branching logic allowing you to tailor your Web pages to the environment in which they are displayed.

Further Reading

Here are some links to some useful tutorials on SSI in you are interested in knowing more.

Regular SSI Reading

NCSA HTTPd Server Side Includes (SSI)

UseForeSite.com

BigNoseBird.com

Conditional SSI articles

University of Kansas Academic Computing Services

Siteexperts.com

Leland conditional SSI extensions

# # #

Alexander Rylance is Assistant Editor for WebReference.com. Prior to working for WebReference he was a Java T.A. at the University of Michigan - Ann Arbor and a Business Analyst at Banc of America Securites. His primary interests are Web Development, Asian culture and pretty much anything to do with the Internet. He can be contacted at: arylance@internet.com


home / programming / ssi / intro To page 1current page
[previous]



FREE Clients Resources from Intel:
Brief:
Microsoft System Center and PCs with Intel Centrino Pro and Intel vPro Processor Technology
Demo:
Take Business PCs to the Next Level
Whitepaper:
IT Community Resources
JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
Microsoft Article: 7.0, Microsoft's Lucky Version?
Microsoft Article: Hyper-V--The Killer Feature in Windows Server 2008
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Windows Server 2008
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES
webref The latest from WebReference.com Browse >
How to Create an Ajax Autocomplete Text Field: Part 6 · Software Engineering for Ajax · Perl Pragma Primer
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Using File Virtualization for Disaster Recovery · VoIP Security: SIP—Versatile but Vulnerable · Around the World in 80 Nodes


Created: March 30, 2001
Revised: March 30, 2001


URL: http://webreference.com/programming/ssi/intro/