Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Go Back   Code Forums > Code Newbie > Submit Tutorials > ASP Classic
User Name
Password

Reply
 
LinkBack Thread Tools Display Modes
Old 02-24-2003, 12:49 PM   #1 (permalink)
Sarlok
Registered User
 
Join Date: Feb 2003
Location: Nowhere special
Posts: 10
Sarlok is on a distinguished road
Send a message via ICQ to Sarlok
Dynamic File Includes

This will let you include a file line for line, into a webpage. It's handy for use with query strings. And you can pretend you're using PHP!

I also made this because I found out that regular file includes ( !--include file=""-- ) are executed before ASP is, so dynamic file includes that way don't work without a lot of time and effort.
A quick example of a querystring is this:
http://www.foo.com/foo.htm?target=news
Now, say you clicked on that link, and wanted to load some news into the page you're looking at. If it has this code in it, you can do that.

Here's the code we'll be using:
Code:
<% set FS=Server.CreateObject("Scripting.FileSystemObject") dim vpath, path vpath=("src/" & Request.QueryString("target") & ".html") path="src/main.html" if not (FS.FileExists(Server.MapPath(vpath))) then set f=FS.OpenTextFile(Server.MapPath(path), 1, FALSE) else set f=FS.OpenTextFile(Server.MapPath(vpath), 1, FALSE) end if do while not f.AtEndOfStream ch=f.Readline Response.Write ch loop %>
First off, the <% marks the beginning of the code. So that the server knows where the code starts, and the %> tells it where it ends.

set FS=Server.CreateObject("Scripting.FileSystemObject ")
This, creates a File Scripting Object. For the sake of simplicity, let's just say it lets us access files later, with "FS".
You can change FS to whatever you want, I just use FS because it's short and easy to remember (FS - FileSystemobject).

Then, we have declared some variables using dim - vpath, and path.
These will let us store some information later, to make it easier to make this work.

vpath=("src/" & Request.QueryString("target") & ".h")
This fills our declared variable "vpath", with the address to the page we want to put into our document.
The Request.QueryString("target") will get whatever comes after target in the link, and store that.
So, if I clicked on a link that was like this:
a href="mypage.htm?target=foo"
then vpath would be filled with "src/foo.h"

if not (FS.FileExists(Server.MapPath(vpath))) then
set f=FS.OpenTextFile(Server.MapPath(path), 1, FALSE)
else
set f=FS.OpenTextFile(Server.MapPath(vpath), 1, FALSE)
end if

Now, this if checks to see if "src/foo.h" exists. If it does, open it.
If you decided to change FS as I mentioned you could above, then you would do the same here.
Now, if for some reason src/foo.h doesn't exist, it will goto the second line, and by default use src/main.h. You can change this to whatever you want it to use by default where path is set at the top (the path="" line).
Then, if the file does exist (else), we set f to point to that file, so we can then open it. The 1 on the end of the line means For Reading, so we can't write to the file. But that's okay, we don't need to. If you do need to write to it for some reason, change the 1 to a 2, and then you can. Also, again, you can change "f" to whatever you wish. I use that, because it's short and easy to remember (f - File).

do while not f.AtEndOfStream
ch=f.Readline
Response.Write ch
loop

This is the last part!
If you decided to change f above, then do the same here.
This little loop will place each line of src/foo.h where we want it one line at a time, until it has done the whole thing. ch=f.ReadLine, will read the first line of the file, then place it into "ch". And the line below it will write it to our page. Then, if the file has more stuff in it, it will do it again, starting from the next line down.
You can place these lines wherever in the page you want the file to be included. Just make sure that the rest of the code is with it.

And that's it!
You don't have to use querystrings for this code, but it's the most practical use I can think of. Otherwise you'd be better off using regular includes ( !--include file=""-- ).
There is probably a better way to do this, especially if you tried to include a big file, but this worked fine for me.
If you find any fatal flaws, feel free to point and laugh accordingly.
Check below for an example of how to place this code in a webpage.

- Sarlok

Code:
<% set FS=Server.CreateObject("Scripting.FileSystemObject") dim vpath, path vpath=("src/" & Request.QueryString("target") & ".html") path="src/main.html" if not (FS.FileExists(Server.MapPath(vpath))) then set f=FS.OpenTextFile(Server.MapPath(path), 1, FALSE) else set f=FS.OpenTextFile(Server.MapPath(vpath), 1, FALSE) end if %> <html> <body> my file will be included just below here.<br> <% do while not f.AtEndOfStream ch=f.Readline Response.Write ch loop %> </body> </html>
__________________
Sarlok is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
.htaccess -- Image Hotlinking Prevention DavH27 HTML / CSS 0 08-27-2004 03:43 AM
.htaccess -- Custom Error Pages v1.1 DavH27 HTML / CSS 0 08-26-2004 04:40 PM
Read and Write to a Text File sde C# 0 02-23-2004 06:44 AM
File Input/Output carrja99 Carrja's C++ Workshop 5 03-11-2003 09:34 AM
Using Includes to streamline the design process sde PHP 0 02-16-2003 11:49 PM


All times are GMT -8. The time now is 06:29 PM.


Powered by vBulletin Version 3.6.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle