经典ASP渲染模板并发送变量 [英] Classic ASP render a template and send variables

查看:107
本文介绍了经典ASP渲染模板并发送变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个经典的ASP页面,我需要为表上的每一行创建一个循环,然后创建一个html文档并将其保存到硬盘驱动器,但是我想创建一个模板,所以我只发送两个变量的模板,这样我就不必每次都在循环中编写HTML文档.

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

是否可以使用具有2个变量持有者的模板并将商品名称和标题发送到模板,然后将其保存到磁盘?

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?

谢谢.

推荐答案

我认为您可以使用存储为文本文件的模板和Replace函数来实现所需的功能.

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

您的模板应为格式完整的html页面,但标题和文章应使用占位符值.占位符必须是唯一的,因此[[[~~~Title~~~]]]之类的东西或类似的序列不会出现在您的实际标题,文章或模板本身中.

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>

在您的代码中,从文件中读取模板并将其存储在变量中. (因此,从技术上讲,您可以首先将其写入变量,但是VBScript在字符串连接方面还是很糟糕的...)文章并循环遍历(尽管只有一次:我不确定为什么您在尝试遍历行和列时都如此).对于每一行,请制作一个模板副本,将标题占位符替换为当前行的标题,将文章占位符替换为当前行的文章,然后将结果写入文件.

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

这篇关于经典ASP渲染模板并发送变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆