Classic ASP render a template and send variables

假装没事ソ 提交于 2019-12-11 04:30:49

问题


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.


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/31417496/classic-asp-render-a-template-and-send-variables

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!