VB icon

Dynamic ASP include

Email
Submitted on: 11/12/2015 3:03:36 PM
By: TFI 
Level: Advanced
User Rating: Unrated
Compatibility: ASP (Active Server Pages), HTML, VbScript (browser/client side)
Views: 3254
 
     This ASP function replaces the standard "include" statement, allowing a TRUE working dynamic include in classic ASP. Included files can contain a mix of vbscript / HTML / javascript. Usage is really easy: <% Include("included.asp") %>

 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: Dynamic ASP include
' Description:This ASP function replaces the standard "include" statement, allowing a TRUE working dynamic include in classic ASP.
Included files can contain a mix of vbscript / HTML / javascript.
Usage is really easy: <% Include("included.asp") %>
' By: TFI
'**************************************

<%
' **** Dynamic ASP include v.2.2
' **** by TFI
Function fixInclude(content)
out=""
content=regreplace(content,"<!-- *# *include file *= *""(.+)"" *-->","<"&"%include(""$1"")%"&">",false)
'content=regreplace(content,"<!-- *# *include virtual *= *"".+"" *-->","<BR><font color=red><B>ERROR: 'include virtual' not supported!</B></font><BR>",false)
content=replace(content,"<"&"%=","<"&"%response.write ")
content=replace(content,"<"&"% =","<"&"%response.write ")
'content=regreplace(content,"<"&"% *= *","<"&"%response.write ",false)
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 regreplace(strOriginalString, strPattern, strReplacement, varIgnoreCase) 
	dim objRegExp : set objRegExp = new RegExp
	with objRegExp
	.Pattern = strPattern
	.IgnoreCase = varIgnoreCase
	.Global = True
	end with
	regreplace = objRegExp.replace(strOriginalString, strReplacement)
	set objRegExp = nothing
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
Function Include(filename)	
	executeglobal (fixInclude(getMappedFileAsString(filename)))
End Function	
%>
<!-- Example of usage: -->
<% Include("included.asp") %>


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

9/30/2016 12:53:54 AMdlbHome

How would I use this script to include this file?
wwwroot> MyWebfolder>file.asp
Website is not in the wwwroot folder.
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.