Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using script code as a string

Status
Not open for further replies.

jasonsalas

IS-IT--Management
Jun 20, 2001
480
GU
Hi all,

I'm looking to use the following script block as a string in a subroutine:

<!-- XML content starts here -->
<%
Dim strFileName
strFileName = Request.QueryString(&quot;filename&quot;)


' Load the XML document
Set source = Server.CreateObject(&quot;Microsoft.XMLDOM&quot;)
source.async = false
source.load(Server.MapPath(&quot;&quot; & strFileName & &quot;.xml&quot;))

' Load the XSL stylesheet
Set style = Server.CreateObject(&quot;Microsoft.XMLDOM&quot;)
style.async = false
style.load(Server.MapPath(&quot;news-article.xsl&quot;))

' Write out the page contents
Response.Write(source.transformNode(style))
%>
<!-- XML content ends here -->
<!--#include file=&quot;footer.asp&quot;-->

*******************************

What I'd like to do is be able to insert that exact script so that it will be executed in the following SUB:

<%
Sub CreateNewASP

Dim FSO, strFileName
strFileName = Request.QueryString(&quot;FILENAME&quot;)
Set FSO = CreateObject(Scripting.FileSystemObject)
Set newStory = FSO.CreateTextFile(&quot;c:\inetpub\ & strFileName & &quot;.asp&quot;, False)
newStory.Write(&quot;SHOULD_GO_IN_HERE_AS_A_SCRIPT&quot;)
newStory.Close

End Sub
%>

*************************************

Got any ideas...or better way to do this? I'm trying to develop an automated method for writing files on my server using ASP/XML/XSL.
 
This is what I use in code to write code.
Code:
' Module level
Private maryProg()          
Private mlngNxt             
Redim maryProg(0)
' Use single quote as double quote
Addstring 4,&quot;strFileName = Request.QueryString('filename')&quot;
AddString .......
'=========
ReDim Preserve maryProg(mlngNxt - 1) ' Resize
if lngNxt >= 0 then
    newStory.Write Join(maryProg, vbCrLf)
End if

'=====================================
Private Sub AddString(ByVal lngSpaces, strIn)
    If mlngNxt > UBound(maryProg) Then
        ReDim Preserve maryProg(mlngNxt + 20)
    End If
    maryProg(mlngNxt) = Space(lngSpaces) & _
                               Replace(strIn,&quot;'&quot;,&quot;&quot;&quot;&quot;)
    mlngNxt = mlngNxt + 1
End Sub
 
Can you express this in VBScript? I don't really know VB syntax, and this is for a Web app.

Thanks!
 
Oops! Guess that shows hos much I know about programming. I actually did it in another fashion in a few minutes....the code doesn't need to be reusable so it's OK that the scriptblock is so long.

*********************************

<% @ Language = VBScript %>

<%
Sub CreateNewASP

' Dim FSO, strFileName
strFileName = Request.QueryString(&quot;filename&quot;)
Set FSO = CreateObject(&quot;Scripting.FileSystemObject&quot;)
Set newStory = FSO.CreateTextFile(&quot;c:\inetpub\ & strFileName & &quot;.asp&quot;, True)
newStory.Write(&quot;<!-- XML content starts here -->&quot;)
newStory.Write(vbcrlf & &quot;<%&quot; & vbcrlf)
newStory.Write(&quot;' Load the XML document&quot;)
newStory.Write(vbcrlf & &quot;Set source = Server.CreateObject(&quot;)
newStory.Write(Chr(34) & &quot;Microsoft.XMLDOM&quot;)
newStory.Write(Chr(34)& &quot;)&quot;)
newStory.Write(vbcrlf & &quot;source.async = false&quot;)
newStory.Write(vbcrlf & &quot;source.load(Server.MapPath(&quot;)
newStory.Write(Chr(34) & strFileName)
newStory.Write(&quot;.xml&quot;)
newStory.Write(Chr(34) & &quot;)&quot;)
newStory.Write(&quot;)&quot;)
newStory.Write(vbcrlf & vbcrlf)
newStory.Write(&quot;' Load the XSL stylesheet&quot;)
newStory.Write(vbcrlf & &quot;Set style = Server.CreateObject(&quot;)
newStory.Write(Chr(34) & &quot;Microsoft.XMLDOM&quot;)
newStory.Write(Chr(34) & &quot;)&quot;)
newStory.Write(vbcrlf & &quot;style.async = false&quot;)
newStory.Write(vbcrlf & &quot;style.load(Server.MapPath(&quot;)
newStory.Write(Chr(34) & &quot;news-article.xsl&quot;)
newStory.Write(Chr(34) & &quot;)&quot;)
newStory.Write(&quot;)&quot;)
newStory.Write(vbcrlf & vbcrlf)
newStory.Write(&quot;' Write out the page contents&quot;)
newStory.Write(vbcrlf & &quot;Response.Write(source.transformNode(style))&quot;)
newStory.Write(vbcrlf & &quot;%&quot; & &quot;>&quot;)
newStory.Write(vbcrlf & &quot;<!-- XML content ends here -->&quot;)
newStory.Write(vbcrlf & &quot;<!--#include file=&quot;)
newStory.Write(Chr(34) & &quot;footer.asp&quot; & Chr(34))
newStory.Write(&quot;-->&quot;)
newStory.Close

End Sub
%>



<%
Call CreateNewASP
%>
 
I see you handled the &quot;%&quot; & &quot;>&quot; where I would have had &quot;%>&quot; causing a problem. My code came from VB code writng VB Code. I just removed the As type e,g As String. But I think I'll use &quot;<Script>&quot; and </Script> instead of &quot;%&quot; & &quot;>&quot;.
 
Yeah...I'm doing everything server-side without much cross-scripting action...not mixing VBScript with JScript, so it works.

It came in handy for a publishing system I'm building because of the small fize sizes...each page contains only script code and gets rendered really fast.

That mixup with the VBScript script delimiter (&quot;%>&quot;) expressed as &quot;%&quot; & &quot;>&quot; was something I found only after debugging and tweaking.

Thanks for your awesome help...I solved it witha variation on a theme, but your input pointed me in the right direction!

Jason
 
Thanks. Not to drag it out but I DID mean <Script RunAtServer> </Script>. You don't HAVE to use &quot;<%&quot;. It is just short hand when ASP and HTML are all intermingled.
<Script> Blocks might &quot;clean up&quot; some code. Think about a SUB and use ^ for &quot; to get rid of Chr(34). The Sub can at least add the Vbcrlf.
From the MSDN.
Code:
When you write server script in an .asp file, you distinguish it from other text (including client script) in one of two ways: 

Within the delimiters <% and %>. Any text between these two tags is processed as inline server script by IIS. The <% %> delimiters are often used to enclose expressions that are evaluated and inserted into the HTML text of a page. For example, the following server script displays the current time on a page:
<% response.write time %>

In a <SCRIPT> tag (as with client scripts), but with the RUNAT=SERVER property, which is used to enclose stand-alone procedures such as functions and subroutines. The following example shows the RUNAT property:
<SCRIPT RUNAT=SERVER>
   Function GetDate
      [some script lines here]
   End Function
</SCRIPT>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top