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
    asp
  » Dynamic File Includes
      by Sarlok
 Page 1 of 1 
   

(Login to remove green text ads)
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>





 
 Page 1 of 1 
   

Rate This Article
1 2 3 4 5 6 7 8 9 10





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