以前写的一个template class的一个方法,现在看到论坛上有人问我就写了一个示例方法出来,就是一个函数,我不太喜欢写注释,代码的易读性应该不会太差的,如果有什么不懂的,请mail我,我的mail:Bluedestiny [at] 1 6 3 .com
简单介绍一下用法吧:
templateStream是模板数据的变体,为字符串型,当然可以是任何字符型的,读出xml,或是用fso读文件都可以,模板中以<template:rs>数据库中字段</template:rs>表示模板数据,当然我就不把自定义替换等等东西写出来了,这个应该不是很难的,这里只是给个示例以及一个思路。
这个我就不写注释了。其实就是一个替换字符串的一个过程,如果有不懂的,留言给我
<!--#include file="inc/connection.asp"-->
<%
'script Written By Bluedestiny
'option explicit
dim templateStream
dim rsObj,sql
templateStream=""
templateStream=templateStream+"id=<template:rs>hotel_id</template:rs>" & vbnewline
templateStream=templateStream+"<h1 align='center'>酒店名称:<template:rs>hotel_name</template:rs></h1>" & vbnewline
templateStream=templateStream+"<h5>浏览量:<template:rs>hotel_clicked</template:rs>" & vbnewline
templateStream=templateStream+"星级:<template:rs>hotel_level</template:rs>" & vbnewline
templateStream=templateStream+"</h5>"
function loadtemplate(stream,rst)
dim T_Stream
dim startemplate,endtemplate
dim T_RS_S, T_RS_E, T_RS_A, T_RS_V
dim replacedStream
dim startloop, maxloop
T_RS_S="<template:rs>"
T_RS_E="</template:rs>"
replacedStream=""
if isObject(rst) then
Set rs=rst.clone
else
response.Write "recordset not initialize."
exit function
end if
for i=1 to rs.recordcount
T_Stream=stream
startemplate=1
maxloop=1000
startloop=1
startemplate=1
do
if startloop>=maxloop then
response.Write "overflow"
exit do
end if
startemplate=instr(startemplate,stream,T_RS_S,0)
if startemplate=0 then exit do
endtemplate=instr(startemplate,stream,T_RS_E,0)
if endtemplate=0 then exit do
startemplate=startemplate+len(T_RS_S)
T_RS_V=mid(stream,startemplate,endtemplate-startemplate)
T_RS_A=T_RS_S & T_RS_V & T_RS_E
T_Stream=replace(T_Stream,T_RS_A,rs(T_RS_V),1,1,1)
startloop=startloop+1
loop
replacedStream=replacedStream & T_Stream
rs.movenext
next
response.Write replacedStream
end function
function convertTemplateTag(byVal stream)
dim tmp
tmp=stream
tmp=replace(tmp,"<","<")
tmp=replace(tmp,">",">")
convertTemplateTag=tmp
end function
response.Write "<pre>"+convertTemplateTag(templateStream)+"</pre>" & vbnewline
Set rsObj=Server.CreateObject("ADODB.recordset")
sql="SELECT TOP 3 * FROM [NVR_hotel] ORDER BY hotel_id ASC"
rsObj.open sql,conn,1,1
call loadtemplate(templateStream, rsObj)
%>
发表于 @ 2006年01月29日 23:37:00|评论(loading...)|编辑