| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Dynamical file include
Hello,
the code below will allow you to include files dynamically. Code:
<%
'Funtions to load Application
'FileExist Function
Function FileExist(strPath)
Set fs=Server.CreateObject("Scripting.FileSystemObject")
FileExist = fs.FileExists(Server.MapPath(strPath))
set fs=nothing
end function
'Include File Function
Function getMappedFileAsString(byVal strFilename)
Const ForReading = 1
Set fs = Server.CreateObject("Scripting.FilesystemObject")
Set ts = fs.OpenTextFile(Server.MapPath(strFilename), ForReading)
getMappedFileAsString = ts.ReadAll
ts.close
Set ts = nothing
Set fs = Nothing
End Function
'Fix Include File to include HTML source as well
function fixInclude(content)
out=""
'position for aspStartTag
pos1=instr(content,"<%")
'position for aspEndTag
pos2=instr(content,"%"& ">")
'if there exists aspStartTag
if pos1>0 then
'text content before aspStartTag
before= mid(content,1,pos1-1)
'remove linebreaks
before=replace(before,vbcrlf,"")
'put content into a response.write
before=vbcrlf & "response.write "" " & before & """" &vbcrlf
'get code content between aspStartTag and aspEndTag
middle= mid(content,pos1+2,(pos2-pos1-2))
'get text content after aspEndTag
after=mid(content,pos2+2,len(content))
'recurse through the rest
out=before & middle & fixInclude(after)
'did not find any aspStartTags
else
'remove linebreaks in file
content=replace(content,vbcrlf,"")
out=vbcrlf & "response.write""" & content &""""
end if
fixInclude=out
end function
Function IncludeFile(strPath)
if FileExist(strPath) then
Execute fixInclude(getMappedFileAsString(strPath))
end if
end function
%>
To include a file you simply do this: Code:
IncludeFile(strPath) This example redirects the user if the file can't be found: Code:
strFile = "library/lang.asp"
if FileExist(strFile) then
Execute fixInclude(getMappedFileAsString(strFile))
else
Response.Redirect("Error.asp?error=Unable to locate lang.asp, unable to generate text.")
end if
To include all files in a folder, for instance a function folder you can do this: Code:
<%
'Gather avaliable function
set fs=Server.CreateObject("Scripting.FileSystemObject")
set fo=fs.GetFolder(Server.MapPath("library/functions/"))
'execute files
for each x in fo.files
'.asp only
If right(x.Name, 4) = ".asp" then
Execute fixInclude(getMappedFileAsString("library/functions/"&x.name))
end if
next
set fo=nothing
set fs=nothing
%>
The possibilities are many. Just keep in mind that you CAN NOT use the " symbol in the HTML part, only in the ASP parts of the files. For the HTML you must replace this with ' also code like: Code:
<input type='Password' value='<% response.Write(GenText(GlobalLanguage, 18)) %>' name='Password' /> will look like this: Code:
<input type='Password' value=' Password ' name='Password' /> *Spaces are added before and after <% %> Not all javascript may work becuase you can't use both " and ' but you can have the javascript functions loaded in a file on its own, like: Code:
<script src='<%= Site&"/script.js" %>' language='javascript' type='text/javascript'></script> Also keep in mind if you have a dynamical Traget in the <a> tag, don't use the ' around the target in the HTML. IE: Code:
<a target='<% response.write("_top") %>' >[code] then it will look for the frame " _top " which doest exist and it will be opened in a new window. Just remove the ' and it should help.
[code]<a target=<% response.write("_top") %> >[code]
And a last thing to keep in mind, though this shouldn't casue much trouble, just some replacing. Replace This:
[code]<%= MyString %>
with: Code:
<% response.write(MyString) %> And just to mention it, the IncludeFile function is not a code I have made myself. Found it on google. think it was on the site 4guysfromrolla.com or something like that. Although, i made the FileExist function, not bad, eh? xD (J/K) Feel free to use this code. (And if you got a solution to fix the spaces or the " inside the HTML, feel free to post it here )KiReSt |
|
#2
|
||||
|
||||
|
nice, thanks for sharing!
![]() |
![]() |
| Viewing: ASP Free Forums > Programming > Code Bank > Dynamical file include |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|