views:

108

answers:

2

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.

+2  A: 

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
AnthonyWJones
That fits my original question to a tee, but my motivation was to *get rid of* the SSI. +1, and question expanded to clarify.
Thom Smith
A: 

Dynamic include is not possible in classic asp.

Bugeo