티스토리 뷰

asp 3.0 에서는
<!-- #include file="파일명" -->
<!-- #include virtual="파일명" -->
Server.Execute("파일명")
으로 다른 파일을 포함시켜 실행할 수 있다.

다른점은
위의 2개는  변수가 공유된다 하지만 파일명에 변수값을 줄 수 없다.
Server.Execute 는 변수가 공유되지 않는다 하지만 파일명을 변수값으로 줄 수 있다.

php 와 비교를 해보면 너무도 허접하고 불편한 언어가 asp 이다.

해결책은 없을까? 하면서도 찾지 못하다가 그나마 최선책을 찾아내었다.

Page1.asp :
<%
dim strTest : strTest = "hello"
response.write "Variable ""strTest"" on Page1= " & strTest & "
" include("page2.asp") response.write "We are back in Page1 and ""strTest"" is now= " & strTest & "
" Function include(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 %>

Page2.asp :
<%
strTest = "goodbye"

response.write "We are in Page2 and have just modified Page1's ""strTest"" variable 
" %>

출력 결과 :
Variable "strTest" on Page1= hello
We are in Page2 and have just modified Page1's "strTest" variable
We are back in Page1 and "strTest" is now= goodbye


다른 방법 :
SSDI 서버측 COM Object 이용


다른 방법 :

<% ' **** Dynamic ASP include v.2.2
' **** by TFI
' usage: include(filename)

Function fixInclude(content)
   out=""   
   content = regreplace(content,"","<"&"%include(""$1"")%"&">",false)
   'content = regreplace(content,"","
ERROR: 'include virtual' not supported!
",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 %>


댓글