Results 1 to 9 of 9

Thread: Dynamic include file in ASP

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2000
    Posts
    142
    I want to let my users paste in some text as an include file for their page.

    When I then display their page I want to include this file.

    If I were to include a static include file the HTML would be:
    Code:
    <!--#include virtual=".../filename.inc" -->
    but I want to dynamically specify this filename with some ASP, something like:
    Code:
    <!-- #include virtual=".../include_<%=firstname>_<%=lastname%>.inc" -->
    Problem is, the ASP does not get processed because it is encased in a comment.

    I have not been able to think of a workaround...

    suggestions?
    tx

  2. #2
    Frenzied Member Mark Sreeves's Avatar
    Join Date
    Nov 1999
    Location
    UK
    Posts
    1,845
    How about:

    <%
    dim quote
    quote = chr(34)

    Response.Write "<!-- #include virtual=" & quote & ".../include_" & firstname & "_" & lastname & ".inc" & quote & "-->"

    %>
    Mark
    -------------------

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2000
    Posts
    142

    nice

    good call mark.

    i feel dumb..shoulda thought of that!!
    thanx

  4. #4
    Frenzied Member Mark Sreeves's Avatar
    Join Date
    Nov 1999
    Location
    UK
    Posts
    1,845
    No worries!

    It somtimes helps to be removed from a problem to see it more clearly.


    Does that make sense?




    I've got a day off tomorrow so you'll be by yourself until Thursday!
    Mark
    -------------------

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    May 2000
    Posts
    142
    i'll try to manage

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    May 2000
    Posts
    142

    DID NOT WORK

    hey mark..

    i tried it out, and it didn't work...

    I have the following (ugly--but correct) statement in my ASP

    Code:
    <%
    Response.Write "<!-- #include virtual="&chr(34)&"/EmployeeHomepages/"&fn&"_"&ln&"/include_"&fn&"_"&ln&".inc"&chr(34)&" -->"
    %>
    But when the page loads, I see the following statement in the source:

    Code:
    <!-- #include virtual="/EmployeeHomepages/testxy_case/include_testxy_case.inc" -->
    It's not a good sign that I see this in the source--it should be the stuff in the file!

    HOWEVER, when I paste the statement given above (in the second code block) as static html it works fine.

    I think the problem here has to do with when the html it produced by the server....

    I don't know how to get around this.... ideas?
    tx

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    May 2000
    Posts
    142

    SOLVED

    found a solution:

    http://www.activeserverpages.com/lea...udedynamic.asp

    it works!!!

    I don't understand why though....

    If someone looks at it and understands it, could you maybe tell me what's going on.

    tx

  8. #8
    New Member
    Join Date
    Nov 2012
    Posts
    1

    Smile Re: SOLVED

    This code works pretty well (Dynamic ASP include by TFI):

    Code:
    <%
    ' **** 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 use: -->
    
    <% Include("included.asp") %>

  9. #9

    Re: Dynamic include file in ASP

    Quote Originally Posted by dvst8 View Post
    I want to let my users paste in some text as an include file for their page.

    When I then display their page I want to include this file.

    If I were to include a static include file the HTML would be:
    Code:
    <!--#include virtual=".../filename.inc" -->
    but I want to dynamically specify this filename with some ASP, something like:
    Code:
    <!-- #include virtual=".../include_<%=firstname>_<%=lastname%>.inc" -->
    Problem is, the ASP does not get processed because it is encased in a comment.

    I have not been able to think of a workaround...

    suggestions?
    tx
    I also had this problem. Thanks for this great post!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width