1
votes

I want to write a function that will include an external file, much like Server.Execute, but will pass along parameters. I'm aware that Server.Execute will pass along query parameters, but I'd like to pass data more generally. For instance:

' main.asp
MyInclude("external.inc", Array("mykey", "myval"))

' external.inc
Response.Write mykey

I doubt I can get quite that far without reading the external fine, twiddling it, and executing, but I'd like to get as close as reasonably possible. Also, if possible, I'd prefer avoiding using the query passing option for security reasons.

EDIT:

The criteria I'd like to fulfill, if possible, are:

  1. Dynamic inclusion (i.e. no SSI)
  2. Simple syntax to invoke (preferably a single function call)
  3. Simple include files (just HTML/ASP snippets)
  4. Passing as much data across as possible.

SSI fits 2, 3, and 4, and I'd use it in a heartbeat except that I don't want to hard-code explicit paths into my files. If I can use the machinery of VBScript, I can just specify the include's name and have the include function find it.

Reading a file and executing would satisfy 1, 2, and 4. 3 is violated because you couldn't put straight HTML in the include. I use this approach to import scripts because I don't need that ability, but with snippets, it's a must-have.

Server.Execute has 1, 2, and 3, but the only way (AFAICT) to pass the include data is to munge the query string. Preservation of the caller's execution context is impossible.

Writing the includes as classes would satisfy 1, 2, and 4. The include files would then be made vastly more complicated.

5
Maybe I don't get what you are really after but it sounds like you should either put inc functionality into a COM component or create a webservice. - Filburt

5 Answers

1
votes

Use a function or a class in your include:-

'' # main.asp
<!-- #include virtual="/myincludes/external.asp" -->
<%
  External "myval"
%>

'' # external.asp
Function External(myval)
   Response.Write myval
End Function

For more sophisticated scenarios I tend to use a class:-

'' # main.asp
<!-- #include virtual="/myincludes/external.asp" -->
<%
  Dim external : Set external = CreateExternal("myval")
  ... some other code..
  external.DoStuff
%>

'' # external.asp
Class External
   Private myval

   Public Sub Init(val)
      myval = val
   End Sub

   Public Sub DoStuff()
     Response.Write myval
   End Sub    
End Class

Function CreateExternal(mval)
  Set CreateExternal = new External
  CreateExternal.Init myval
End Function
1
votes

Sorry to prove you wrong Bugeo, but a form of Dynamic/conditional include is very possible;

Have a look at WSC's Thom, they are XML based components you can code in vbscript and call from asp pages. They act as if they are COM components, but they can be edited and are interpreted on the fly like normal ASP pages.

They are not very well known in classic ASP, but they are in fact very useful and even allow you to separate presentation and business logic. A WSC can also call other WSC's, so you could program an N-tier application.

I have an example on my weblog that shows you how to call a WSC from ASP. There are also two WSC's you can download and modify. They are actually quite simple, you define the properties and methods in XML, and in the same file you can code getters and setters for those properties, aswell as all of your methods in vbscript:

http://precompiled.wordpress.com/2007/11/26/hmac-sha1-encryptie-onder-classic-asp/

Some more detailed information is here: http://aspalliance.com/414_Windows_Scripting_Components_WSC_in_ASP

And a wizard from Microsoft to create WSC's:

http://www.microsoft.com/downloads/en/details.aspx?familyid=408024ed-faad-4835-8e68-773ccc951a6b&displaylang=en

Hope this helps,

Erik

0
votes

Dynamic include is not possible in classic asp.

0
votes

Dynamic include is possible in ASP classic:

<%
 ' **** Dynamic ASP include v.2

function fixInclude(content)
   out=""   
   if instr(content,"#include ")>0 then
        response.write "Error: include directive not permitted!"
        response.end
   end if     
   content=replace(content,"<"&"%=","<"&"%response.write ")   
   pos1=instr(content,"<%")
   pos2=instr(content,"%"& ">")
   if pos1>0 then
      before= mid(content,1,pos1-1)
      before=replace(before,"""","""""")
      before=replace(before,vbcrlf,""""&vbcrlf&"response.write vbcrlf&""")
      before=vbcrlf & "response.write """ & before & """" &vbcrlf
      middle= mid(content,pos1+2,(pos2-pos1-2))
      after=mid(content,pos2+2,len(content))
      out=before & middle & fixInclude(after)
   else
      content=replace(content,"""","""""")
      content=replace(content,vbcrlf,""""&vbcrlf&"response.write vbcrlf&""")
      out=vbcrlf & "response.write """ & content &""""
   end if
   fixInclude=out
end function

Function getMappedFileAsString(byVal strFilename)
  Dim fso,td
  Set fso = Server.CreateObject("Scripting.FilesystemObject")
  Set ts = fso.OpenTextFile(Server.MapPath(strFilename), 1)
  getMappedFileAsString = ts.ReadAll
  ts.close  
  Set ts = nothing
  Set fso = Nothing
End Function

execute (fixInclude(getMappedFileAsString("included.asp")))
%>
0
votes

Beats me as what is considered "dynamic" when all of my includes pass variables to other includes in the page. For example on every page I use

<!--#include file="inc_common_functions.asp"-->

Then later I can use functions like getUserName(UserID) to display the User's name wherever I please.

Using a session cookie, all of the user's information can be available for logging, etc.