j.********@find-a-part.com wrote on Mon, 15 Oct 2007 09:18:18 -0700:
is there a way of having a file that's name is a variable (eg dependant
on the user name) act like a include.
i know that you cant define the file for an include asp tag using a
variable and that reading the file using "Response.Write
FSO.OpenTextFile(ppp, 1, False, False).readall" prints the file, and
treating it like a html file. can anyone suggest another way of doing
this?
While Server.Execute is fine if the code in the "include" file is
self-contained, it won't work if you need to reference variables from the
"parent" code. You could pass all variables from the "parent" to the
"include" via the Session object, but with a large number of variables this
is impractical. Try Server.Execute and see if it'll fit your requirements,
if not then read on.
I had to do something similar recently and kludged a function together which
works so far with all the "include" files I've thrown at it - all my
"includes" are mixed ASP and HTML, and use variables from the "parent" page,
so Server.Execute and Server.Transfer were not suitable. My code isn't
pretty, and it's probably not very efficient either, but it works for me.
What this does is takes a filename, reads the file into a variable, and then
takes any inlined HTML and wraps it in Response.Write calls. It then
executes the resulting string which is now all ASP using the Execute call.
If the function returns False then the "include" file is empty or could not
be found. There's no error checking in the function, so you'll have to add
this yourself once you're happy it works.
To use the function, just do something like this:
<html>
<body>
<p>Blah blah</p>
<%
ExecInclude "myfile.asp"
%>
<p>more blah</p>
</body>
</html>
Here's the function:
Function ExecInclude(sFile)
dim mfo, mf, sTemp, arTemp, arTemp2, lTemp, sTemp2, lTemp2, sFile2
If InStr(1,sFile,":") = 0 Then
sFile = Server.MapPath(sFile)
End If
'first read the file into a variable use FSO
set mfo = Server.CreateObject("Scripting.FileSystemObject")
'does file exist?
If mfo.FileExists(sFile) Then
'read it
set mf = mfo.OpenTextFile(sFile, 1, false, -2)
sTemp = mf.ReadAll
mf.close
set mfo = nothing
Else
sTemp = ""
End If
If sTemp <"" Then
'sTemp contains the mixed ASP and HTML, so the next task is to
dynamically replace the inline HTML with response.write statements
arTemp = Split(sTemp,"<" & "%")
sTemp = ""
For lTemp = LBound(arTemp) to UBound(arTemp)
If InStr(1,arTemp(lTemp),"%" & ">") 0 Then
'inline asp
arTemp2 = Split(arTemp(lTemp),"%" & ">")
'everything up to the % is ASP code
sTemp2 = trim(arTemp2(0))
If Left(sTemp2,1) = "=" Then
'need to replace with response.write
sTemp2 = "Response.Write " & mid(sTemp2,2)
End If
sTemp = sTemp & sTemp2 & vbCrLf
'everything after the % is HTML
sTemp2 = arTemp2(1)
Else
'inline html only
sTemp2 = arTemp(lTemp)
End If
arTemp2 = Split(sTemp2,vbCrLf)
For lTemp2 = LBound(arTemp2) to UBound(arTemp2)
sTemp2 = Replace(arTemp2(lTemp2),"""","""""") 'replace
quotes with doubled quotes
sTemp2 = "Response.Write """ & sTemp2 & """" 'add
response.write and quoting
If lTemp2 < Ubound(arTemp2) Then
sTemp2 = sTemp2 & " & vbCrLf" 'add cr+lf if not the
last line inlined
End If
sTemp = sTemp & sTemp2 & vbCrLf 'add to running variable
Next
Next
Execute sTemp
ExecInclude = True
End If
end Function
Use this code at your own risk. It shouldn't cause any problems, but I can't
guarantee it. If anyone can suggest improvements to this I'd be happy to
hear them.
--
Dan