Rick Curtis - Tutorial 5
Including Text Files Dynamically
Server Side Includes (SSI) are a great way of building your Web site efficiently. An SSI is a file that contains HTML, ASP Code, or other code that you want to include in another file. By using the #Include statement you can be more more efficient in your code writing (and less error prone) by using the "Write once, use again and again" principal. Since an SSI can include other ASP code, you can compartmentalize your pages, writing a piece of code that is reused in different pages and having to only maintain it in one place. Pretty cool.
I won't go into the details of SSI in this article since as I learned, a little painfully (after coding in circles for a few hours), you can't create a dynamic Server Side Include file driven by your database. The reason for this is that all SSI's are evaluated before the ASP code is interpreted (thanks Owen). So if the SSI is dependent on the ASP code (for example a value from a recordset) it never gets to the SSI. In UltraDev you actually can select Server Side Include from the Insert Menu and bind it to a database record value, but it doesn't do anything. What you get is
<!-- #include virtual = "<% = (rsRecordset.Fields.Item("FieldName").Value %>" -->
Which never pulls the SSI across. So how can you dynamically pull a text file into your page. It involves a few tricks using the FileSystemObject. This is a component built into Windows that allows you to access the file system on your Web Server. I am still getting the hang of using it so for more details, check out the articles and FAQ's on 4guysfromrolla.com.
The Article Database
In this case I have a site where users can post articles. I decided I didn't want to store the full article in the database since it is more of a hassle to get a new version into the database than uploading a new file. Here is the basic table design.
The Web Site
Like lots of database driven sites there is a Search Page where users can search for articles. The Search page redirects to a List page that shows article number, title and author. By clicking on the Title link the Go to Detail server behavior is fired and the user is taken to ArticleInc.asp. Now let's take a closer look at this page.
The first thing to do is to put a recordset on the page that shows the
articles and authors. Below all of the UltraDev recordset code and just
above the <html>
tag add the following code:' The script below needs the file enclosed
in quotes, adding Chr(34) includes quotes around the filename
First you define two variables, ArticleLink and ArticleFile. Set the value of ArticleLink to the recordset value of the first text page to be included. The second variable, ArticleFile simply uses the code for double-quote (Chr(34))and the concatenation character (&) to create a string value. So if the filename is 100.asp then the value of ArticleFile is "100.asp" I'll explain below why you need to wrap this recordset value in double quotes.
At the end of your page you need to add two pieces of code. I need to acknowledge Charles Carroll at www.learnasp.com/learn/includedynamic.asp for this great code. The bulk of the code goes at the end of the page below the </html> tag.
There is one more piece of code to add to the location on your page where you want to merge the text file. Here's that code:
Okay so what's happening here? Let's look at Charles' code line by line.
The code in the upper body of the page calls the function ReadDisplayFile and sets the value of whichfile to the dynamic value of ArticleLink which is passed from the previous List page. Now as in most code:value situations, the ArticleLink needs to be in double quotes. But if we do that (whichfile = "ArticleLink") the ASP page tries to find a file called ArticleLink which doesn't exist. This is why we defined two variables initially (ArticleURL and ArticleLink). Since we have already "pasted" double quotes around ArticleURL to get ArticleLink then the value that is placed into the statement whichfile = ArticleLink is, for example, whichfile = "100.asp" which is the name of the text file that we want to include dynamically into our ASP page. So this is the dynamic value that is passed to Charles' FileScriptingObject code and pulls the correct text file into the middle of our page. Note: All of this code assumes that the text file is in the same directory as the ASP page that is calling it (ArticleInc.asp). If you want to have the file someplace else you need to do more magic (which I don't compeltely understand) with the FileScriptingObject.
I hope this is helpful to people. Happy coding.
|Copyright © 2000 All rights reserved Rick Curtis, Princeton,
Macromedia and UltraDev are trademarks of the Macromedia Corporation.