Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 3 of 3
  1. #1
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    Hello all.

    Recently the question was brought up on this forum of whether ASP could do runtime inclusion. The answer was no. I wrote a script to make this possible to a great extent in ASP VBScript. Let me explain what runtime inclusion is and the benefits thereof a little first, and then I'll get to the script and explaining it (for those just looking for the script, see the Usage section at the end of this post; in addition, you should probably look at the Limitations section).

    Explanation
    In ASP, similarly to in C/C++, inclusion of other files is done by a preprocessor which runs through the file before the code is interpreted. An include looks something like this:
    Code:
    <!--#include file="file.asp"-->
    The preprocessor runs through the ASP file and replaces all <!--#include directives with the contents of the specified file. For example:
    Code:
    --- file.asp ---
    <% test = request.querystring("test") %>
    <a href="<%=test%>"><%=test%></a>
    --- end file.asp ---
    
    --- test.asp ---
    <% test = powertest %>
    <!--#include file="file.asp">
    <br />
    --- end test.asp ---
    The above code would be handled by the preprocessor first, yielding the following for test.asp:
    Code:
    <% test = powertest %>
    <% test = request.querystring("test") %>
     <a href="<%=test%>"><%=test%></a>
    <br />
    This code is then processed as if it were all on one page.

    This is a very useful functionality already, but it misses some key powerful features that runtime inclusion, as implemented in PHP's include, include_once, require, and require_once directives, provides. A good example of this is that you can't do either of these:
    Code:
    --- This code fails ---
    <% file = "file1.asp" %>
    <!--#include file=file-->
    --- As does this code ---
    <% file = "file1.asp" %>
    <!--#include file="<%=file%>"-->
    This is because the preprocessor goes through the ASP file independently of the interpreter, so it doesn't know about variables or anything of that sort. This also means you can't include a different file depending on something in the query string (uri.asp?include=file.asp, for example, is completely useless in ASP).

    The script I wrote, at the most basic level, reads the file you want to include and passes it to ASP VBScript's executeGlobal function. If you want to learn more about the magic it performs, there are two ways to go about it: going through the code yourself, or posting a reply to this thread; if a couple of people ask for it, I'll go into a more detailed explanation and post it as a tutorial in the tutorials forum.

    Limitations
    The script currently has some limitations. The most obvious one is a performance limitation: this is a script. It does some amount of parsing on the included code, which may take a while. This is detrimental to performance. If you need an extremely high-performance page, DON'T USE THIS! It does file reading, line-by-line analysis, and other things that eat up CPU cycles on the server that you probably don't want eaten. Naturally, the best way to check whether the load is unacceptable would be testing it.

    The second limitation is one that I hope to eliminate in the near future. It involves includes in other files. If a file that has been included via the script contains regular (<!--#include) includes, the script doesn't process them.

    This is currently not implemented, but as soon as I have time, I'll be fixing it by replacing all <!--#includes with calls to the include function inside the code.

    Finally, one last limitation is that the script currently has trouble with certain files. I suspect this has to do with UNIX newlines (vbLf, or \n) being left in strings when using readLine. Because I'm not sure, I currently deal with this with a parameter when calling the function. More information on the parameter is given in the Usage section. If someone else can confirm to me that this is an issue with UNIX newlines (and potentially Mac newlines, vbCr or \r), then I have something in mind to fix the script quickly.

    Usage
    The first step to using this script is including the file (the old-fashioned way):
    Code:
    <!--#include file="include.asp"-->
    From there, including a file is as simple as calling the function:
    Code:
    call include( "file.asp", false )
    Now, you're probably wondering what the hell the second argument to the function is; a peek in the file will tell you:
    Code:
    ' Includes a file at runtime.
    '
    ' @param filename The name of the file to include (passed to server.mapPath to
    '	 determine real location).
    ' @param removeNewLines True if newlines must be removed from the ends of the
    '	 lines read in, false to assume newlines have already been stripped
    removeNewLines is the parameter we're interested in. The reason this paramater is there is that I observed that on certain test files, reading the file line-by-line would introduce a newline (most likely when the newline was a UNIX newline, \n, or vbLf). In these cases, you can activate removeNewLines. Doing so will automatically crop off the last character on every line, which should be the newline character.

    One last note: although I have not tested this, nested includes should work fine. Nested includes in this case are cases where include() is called from within an included file. Again, I haven't tried it, but there is absolutely no reason for which this wouldn't work.

    The script is attached, so please, download it and test it. Let me know of any bugs you find so I can see what I can do about fixing them. The include-within-an-include problem (see Limitations) is something I'll try and fix as soon as I get time. If someone else wants to fix it first and email me the file, please go for it :-).

  2.  

  3. #2
    Member
    Join Date
    May 2005
    Location
    Rochester, NY
    Posts
    83
    Member #
    10054
    Shadowfiend-
    I'm assuming this post is a result of your research based on my original post. I definitely appreciate the help and I'm going to get around to taking advantage of your hard work, I've just had some "emergencies" crop up lately. I expect to test all this out this week and I'll let you know how it works.
    Thanks again,
    Mike
    michael murphy
    Professional Web Development
    http://www.letsgomurphys.com/

  4. #3
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    Indeed it is. I thought it deserved its own thread, since there are probably others looking for this kind of functionality; it had nothing to do with your lack of reply to my original post. We all get busy, so just do it whenever you get a chance :-).


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 03:53 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com