Monday, July 9, 2007

Improve efficiency with server-side includes

Many web sites consist of repetitive information, such as headers, or content that rotates on a regular basis, such as a Tip of the Week. By using server-side includes, you can reduce the repetitiveness and some of the hassle that comes with rotating content, and your code will look much leaner as a result. A server-side include works in much the same way as the C/C++ #include directive, telling ASP to insert the contents of the specified file in place of the include. Your include file can contain HTML, server-side script, client-side script, or anything else you'd normally find in an Active Server page. The syntax looks like this:

<!--#include virtual="header.asp"-->

There are two flavors of includes--file and virtual. When using the file include, you must specify the file's path in relation to the including file. With the virtual include, however, you must specify the file's path in relation to the virtual root. Therefore, if the folder myweb is in your virtual root, and your file structure looks like this: myweb\pages\default.asp myweb\includes\header.asp you'd use the following statements for file and virtual includes, respectively.

 <!-- #INCLUDE FILE="../includes/header.asp" --> <!-- #INCLUDE VIRTUAL="/myweb/includes/header.asp" -->

No comments: