ASP 101 - Active Server Pages 101 - Web03
The Place ASP Developers Go!



Windows Technology Windows Technology
15 Seconds
4GuysFromRolla.com
ASP 101
ASP Wire
VB Forums
VB Wire
WinDrivers.com
internet.commerce internet.commerce
Partners & Affiliates
ASP 101 is an
internet.com site
ASP 101 is an internet.com site
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

ASP 101 News Flash ASP 101 News Flash



 Top ASP 101 Stories Top ASP 101 Stories
What is Adovbs.inc and Why Do I Need It?
An Overview of ASP.NET
Connections, Commands, And Procedures

QUICK TIP:
Server-Side vs. Client-Side Scripting
Show All Tips >>
ASP 101 RSS Feed ASP 101 Updates


Dynamic Include Files

Dynamic Include Files

by Michael Qualls

Introduction

This is an advanced example and it assumes that you are already familiar with HTML, ASP, and using ActiveX Objects. In it I use the FileSystemObject and TextStream Objects to create "dynamic" includes.

Dynamic Include Files

I think that at one time or another all of us have attempted to create dynamic include files in our ASP applications only to find out that it would not work. The usual approach was to use a variable to hold the name of the file that you wanted to include and then pass the name of that variable to the include directive.

Let us be clear: The following code will not work:

<%
'A variable is declared to hold a file name
Dim MyFile
'The desired file name is passed to the variable.
MyFile = Request("SomeFileName")
'Then the variable holding the file name is passed
'to the include directive.
%>
<!--#include file=<%=MyFile%>-->

The reason that this code listing will not work is because in Active Server Pages the include directives are resolved before the server-side script portions are processed. The above code listing will trigger a nasty error telling you that the include file cannot be found.

One of the reasons to use an include file is to be a container for static HTML that does not normally change (such as a standard header or footer). This file is then referenced using a normal include directive. However, sometimes, different content is needed based upon user input or some other condition. Rather than create an entirely different web page for each and every possible condition, the idea of using "dynamic include" files was born. However, as noted in the code listing above, the most logical way to implement this idea does not work.

In order to get around this problem, the Microsoft FileSystemObject Object can be used to load and pass the content of the desired include file into a string variable which can be inserted into the page that is being sent to the client. The following function, "getFileContents", aids in this process by loading a file that is passed to the function as an input parameter and returning its contents as a string.

<%
'Pass the name of the file to the function.
Function getFileContents(strIncludeFile)
  Dim objFSO
  Dim objText
  Dim strPage
  'Instantiate the FileSystemObject Object.
  Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
  'Open the file and pass it to a TextStream Object (objText). The
  '"MapPath" function of the Server Object is used to get the
  'physical path for the file.
  Set objText = objFSO.OpenTextFile(Server.MapPath(strIncludeFile))
  'Read and return the contents of the file as a string.
  getFileContents = objText.ReadAll
  objText.Close
  Set objText = Nothing
  Set objFSO = Nothing
End Function
%>

Using this function "dynamic include" files can be achieved. First, the main page (a template file containing the page layout and any static content) would be loaded and passed to a string variable. Then, the contents of the include file would be loaded and passed to a string variable. Finally, the variable containing the contents of the include file would be inserted into the contents of the variable containing the main page.

Example: Dynamic Include Files

First, lets take a look at the "template" file. In this listing, there is an HTML comment, "<!-- INCLUDE FILE HERE -->". We will replace this HTML comment with the content of the include file.

<html>
<body>
<h2>Welcome to my web page!</h2>
<table width="500" border="1">
 <tr>
  <td>
   <!-- INCLUDE FILE HERE -->
  </td>
 </tr>
</table>
</body>
</html>

Now, lets look at the include files that will be used for this example. The first include file is the default include file. The default include file is a form that allows the client to choose which of three include files to load. Notice that the "action" attribute of the form is omitted. This is because when the form will be submitting to itself (causing "dynamicinc3.asp" to reload).

<!-- BEGIN DEFAULT INCLUDE -->
<form method="post">
<h3>Select the name of the file you wish to load</h3>
<p>
<select id=cboFile name=cboFile>
 <option value="includefile1.inc">File #1</option>
 <option value="includefile2.inc">File #2</option>
 <option value="includefile3.inc">File #3</option>
</select>
<input type="submit" value="Submit">
</p>
</form>
<!-- END DEFAULT INCLUDE -->

For the sake of this example, the content of the other three include files has been kept very simple.

<!-- BEGIN INCLUDE FILE #1 -->
 <h2 style="color:red">FILE #1 CONTENTS</h2>
 <br>
 <a href="dynamicinc3.asp">Return to default page</a>
<!-- END INCLUDE FILE #1 -->
<!-- BEGIN INCLUDE FILE #2 -->
 <h2 style="color:green">FILE #2 CONTENTS</h2>
 <br>
 <a href="dynamicinc3.asp">Return to default page</a>
<!-- END INCLUDE FILE #2 -->
<!-- BEGIN INCLUDE FILE #3 -->
 <h2 style="color:blue">FILE #3 CONTENTS</h2>
 <br>
 <a href="dynamicinc3.asp">Return to default page</a>
<!-- END INCLUDE FILE #3 -->

Finally, consider the ASP file that makes the whole example run, "dynamicinc3.asp".

<%
'-------------------------------------------------------------
'The "getFileContents" function should be included at the top
'of the ASP file.
'-------------------------------------------------------------
'Declare variables to hold the content of the main page and
'the include file.
Dim strMain, strInclude
'Get the contents of the main page and pass them to the "strMain"
'variable.
strMain = getFileContents("maintemplate.inc")
'Test to see if the "cboFile" select box is being submitted. If so,
'load the requested include file. If not, load the default include.
If Request.form("cboFile") = "" Then
  strInclude = getFileContents("includedefault.inc")
Else
  strInclude = getFileContents(Request.form("cboFile"))
End If
'After the proper include file contents are loaded ("strInclude"),
'then insert it into the main page ("strMain") using the "Replace"
'function.
strMain = replace(strMain,"<!-- INCLUDE FILE HERE -->",strInclude)
'Use the "Response" Object to "Write" the completed page to the client.
Response.Write strMain
%>

This sample works, and, in effect, creates the ability to use dynamic include files because it does not use the include directive. Instead this example uses the FileSystemObject Object.

This technique can be useful if you wish to keep your layout separated from your content. You can create a template which contains the layout for your website and include files which contain the content of the website. Then using ASP, you can combine the two for client viewing. This is exactly the way that I put my website together!

Let me give you one last thing to think about. How would using techniques similar to this aid you if you are storing your data in XML files?

Related Links


Home |  News |  Samples |  Articles |  Lessons |  Resources |  Forum |  Links |  Search |  Feedback

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers