2

I have a classic ASP page, and I need to create a loop for each row on a table and then create an html document and save it to the hard drive, but I want to create a template so I just send the two variables to the template so I don't have to write the HTML document each time on the loop.

This is what I have so far:

SQL = "select Title, Article from [ASPTest].[dbo].[articles]"
  set rs = conn.execute(SQL)
  arrRecs = rs.GetRows
  For row = 0 To UBound(arrRecs, 2) 'Rows
      For col = 0 To UBound(arrRecs, 1) 'Columns
          Response.Write rs.Fields(col).Name & " = " & arrRecs(col, row) & " "
          dim fs,f
          set fs=Server.CreateObject("Scripting.FileSystemObject")
          set f=fs.CreateTextFile("C:\Users\User\Documents\ASP Pages\"+arrRecs(col, row)+".html",true)
          f.write("<html><body><div>It kinda works</div></body></html>")
          f.close
          set f=nothing
          set fs=nothing
      Next
      Response.Write "<br />"
  Next

Is there a way to use a template that has 2 variable holders and send the article name and title to the template and then save it to the disk?

Thank you.

Riquelmy Melara
  • 849
  • 2
  • 11
  • 28
  • I don't quite understand what you are asking for? Can you explain further or provide an example of the result? – johna Jul 15 '15 at 01:40

2 Answers2

3

I think you could probably achieve what you want using a template stored as a text file, and the Replace function.

Your template should be a fully-formed html page, but with placeholder values for the title and article. The placeholders need to be unique, so something like [[[~~~Title~~~]]] or a similar sequence that will not occur in your actual titles, articles, or the template itself.

<html>
<head><title>[[[~~~Title~~~]]]</title></head>
<body>
<h1>[[[~~~Title~~~]]]</h1>
<div id="article">[[[~~~Article~~~]]]</div>
</body>
</html>

In your code, read the template from the file and store it in a variable. (So technically, you could just write it to a variable in the first place, but VBScript is bad at string concatenation... anyway.) Get your array of titles & articles and loop through it (though only once: I'm not sure why you're looping through both rows and columns in your attempt). For each row, make a copy of the template, replace the title placeholder with the current row's title, replace the article placeholder with the current row's article, and write the result to a file.

Dim template, t
Dim fso, file
Dim rs, conn, SQL
Dim records, row

SQL = "SELECT ID, Title, Article FROM [ASPTest].[dbo].[articles]"
'[...database stuff...]
records = rs.GetRows
'[...close database...]

Set fso = Server.CreateObject("Scripting.FileSystemObject")
Set file = fso.OpenTextFile("path/to/template.txt",1) '- 1 = For reading
template = file.ReadAll
file.Close
Set file = Nothing
For row = 0 to UBound(records,2) 
    t = template
    t = Replace(t,"[[[~~~Title~~~]]]",records(1,row))
    t = Replace(t,"[[[~~~Article~~~]]]",records(2,row))
    Set file = fso.CreateTextFile("path/to/html/" & records(0,row) & ".html")
    file.Write(t)
    file.Close
    Set file = Nothing
Next
Set fso = Nothing
Martha
  • 3,932
  • 3
  • 33
  • 42
  • 1
    I use a similar technique to replace email templates with meaningful information such as the body of the text without having to worry about the actually html. Great for standardised templates. – user692942 Jul 15 '15 at 07:41
  • This works like a charm! I am just wondering how can I name the html document based on the row Id of the table, for example 100.html, 101.html and so on. Thank you for your help :D – Riquelmy Melara Jul 15 '15 at 16:40
  • @RiquelmyMelara: just include the row ID in your SQL query. Hold on a sec, I'll edit. – Martha Jul 15 '15 at 16:43
  • Than you so much! this is what I was looking for!! – Riquelmy Melara Jul 15 '15 at 17:04
  • Provided [an example](http://stackoverflow.com/a/36007779/692942) of the approach I use that utilises `ADODB.Stream` over `Scripting.FileSystemObject`. – user692942 Mar 15 '16 at 10:15
1

Back in the day I created the KudzuASP template engine to solve this rather complex deficiency in Classic ASP. In KudzuASP you can have ASP code pages that have absolutely NO HTML in them.

KudzuASP is as small include file roughly under 1000 lines of code that turns your hosting ASP page into an event driven object used by the template engine.

In short you create an instance of the template engine, set some variables, install custom code objects, and invoke it after which the template engine reads your template and make callbacks to your ASP page when and where appropriate. It has a library system so you can load libraries of custom tags handlers/components via code or by tags placed in your HTML template.

One of the best features is that for those still under the Classic ASP umbrella it makes 100% separation of application code and logic from presentation possible. Coding Classic ASP pages using KudzuASP is much easier than without and because of the way ASP compiles pages the callbacks are "native" and very fast.

You can find it here KudzuASP where the project is still maintained.

Andrew
  • 307
  • 3
  • 10