One popular and simple way to insert content into a page is through a Server-Side Include (SSI) directive. SSI has long been a feature of Web servers, allowing the server to read information about files and its environment, execute server-based programs, and dynamically insert information into the HTML stream it is sending back to the client.
The bad news is that, in general, ASP and SSI don’t mix. You can use one technique or the other, but not both together, because the special file extension for SSI files (.stm
) doesn’t give the ASP interpreter a chance to see the file as well. However, one SSI command is available in ASP, and will execute successfully within an ASP page. This is the #include
directive.
We can include the contents of any text or HTML file in an ASP page using #include
within an HTML comment tag. We just have to provide the virtual or physical location of the file:
<!-- #include file="d:\inetpub\wwwroot\includes\myfile.inc" -->
<!-- #include file="anotherfile.txt" -->
<!-- #include virtual="/includes/myfile.inc" -->
The text file can have any name, and can contain almost anything. There are no limitations, except when it contains script code that will be executed on the server in ASP. In this case, the include file must contain the correct combination of script delimiters (either <%..%>
or <SCRIPT LANGUAGE=".." RUNAT=SERVER>..</SCRIPT>
). It can't just be fragments of server-side script.
One other consideration is that the contents of the #include
instruction itself cannot be manipulated in ASP, so you can't, for example, create the file name or virtual URL dynamically. The included file is inserted into the ASP page, replacing the entire comment tag, before the ASP script code interpreter processes the page. In this case, ASP will not even recognize that the content of the page is made up of inserted text files.
At first sight, SSI seems to provide only a limited opportunity for creating dynamic content. After all, it's just a text file that is inserted into the page. However, it is ideal for providing common features in the page, such as headers and footers, legal copyright statements, text links to other pages, or just the email address of the webmaster.
This is the file pagefoot.inc that we use on our site:
<hr>
<CENTER>
<font FACE="Arial" SIZE="2">
<A HREF="http://webdev.wrox.co.uk/default.asp" TARGET="_top">Home</A> |
<A HREF="/webdev/booklist.asp">Samples</A> |
<A HREF="/webdev/preview.asp">Preview</A> |
<A HREF="/resources/resources.asp">Resources</A> |
<A HREF="/reference/reference.asp">Reference</A> |
<A HREF="/resources/links/links.asp">Links</A> |
<A HREF="http://www.wrox.com/store.asp" TARGET="_top">Order</A> |
<A HREF="/search/search.asp">Search</A> |
<A HREF="/webdev/sitemap.asp">SiteMap</A>
</font>
</CENTER><BR>
<font FACE="Arial" SIZE="2">For more information and details of our full range visit our main sites at <a HREF="http://www.wrox.com" TARGET="_top"> Wrox USA</a> or <a HREF="http://www.wrox.co.uk" TARGET="_top">Wrox Europe</a>. To contact us with any comments or suggestions about our Web site, e-mail to:
<a HREF="mailto:webmaster@wrox.com">feedback</a>.</font>
<p><font FACE="Arial" SIZE="1">©1998 Wrox Press Ltd.</font></p>
Each page can then include this file using just:
<!-- #include virtual="/common/pagefoot.inc" -->
The result is quick, effective, and most importantly allows instant and automatic updating in all the pages that use it when we need to make a change to the content:
In fact, SSI is also a great way to insert standard blocks of script code into several pages, for just this reason. If you make a change to the code in the text file, the new version is automatically used in all the pages that include this file. The code can be server-side (ASP), or client-side code. Remember, you can insert anything that will be interpreted by ASP, or which will end up as part of the page on the client; including text, HTML tags and attributes, and client-side script.
One particularly useful application of this technique is in providing connection strings to ADO on the server (we looked at examples of connection strings at the end of Chapter 1). Because they are server-specific, and contain the database or data source name, changing the connection string in every page when you change the server name or database name, or move to a different server is hard work
Instead, we can store each connection string in a text file in a common folder on the server, and include it in each page where it is required with SSI. If we need to make any changes to the connection string, we only have one copy to update. The new version is then automatically used in all the pages to which it applies. The code below is the content of a file connect.inc
. This is the connection string we use to provide anonymous access to a database named GlobalExamples on our server:
<%
strConnect = "SERVER=WEBDEV3;DRIVER={SQL Server};" _
& "DATABASE=GlobalExamples;UID=examples;pwd=;"
%>
Then, in each page that needs to access the GlobalExamples database as an anonymous user, we can simply insert the line:
<!-- #include virtual="/common/connect.inc" -->
However, just because SSI uses a disk-based text file doesn't mean to say it has to be a static text file. ASP can create text files on the server's disk, so we could use ASP script to periodically update a text file that is inserted into another ASP page. In fact, this is a technique we use on the Web-Developer site's What's New? page:
SSI is fast and efficient at runtime, when compared with other techniques for inserting content into pages dynamically. So it is perfectly suited to situations like this, where the content changes only occasionally. The page you see is built up of HTML plus some server-side ASP script that inserts the current date, followed by a table that includes a mixture of static images and SSI text files.
Building up the current date is easy enough:
...
<%
datToday = Date()
intThisDay = Day(datToday)
strWeekDayName = WeekDayName(WeekDay(datToday), False, vbSunday)
intThisMonth = Month(datToday)
strThisMonth = MonthName(intThisMonth)
suffix = "th"
Select Case intThisDay
Case 1,21,31: Suffix = "st"
Case 2,22: Suffix = "nd"
Case 3,23: Suffix = "rd"
End Select
Response.Write strWeekDayName & " " & intThisDay & suffix _
& " " & strThisMonth
%>
...
The tables in the body of the page contain the include files, for example the following code creates the main section of the page visible in the previous screenshot—the lines that insert the 'include' files are highlighted:
...
<table BORDER="0" CELLPADDING="5" WIDTH="100%">
<tr>
<td ALIGN="LEFT" VALIGN="MIDDLE">
<!-- #include file="hotnews/thisweek.txt" -->
</td>
<td ALIGN="RIGHT" VALIGN="MIDDLE">
<!-- #include file="hotnews/bookcover.txt" -->
</td>
</tr>
</table>
...
We have a separate administration page that uses ASP to update these two text files, making it possible for the contents of the page to be changed whenever necessary. As the text files are updated, the What's New? page automatically reflects the changes the next time it is viewed. You'll see the administration page in Chapter 6, when we come to look at the whole topic of remote site administration.