日期:2013-08-25  浏览次数:20849 次

<%
'=========================================================
' File: class_template.asp
' Version:1.0
' Date: 2004-5-7
' Script Written by R.H
' Description: ASP Template Class
'=========================================================
' Copyright (C) 2004 Interflower Studios. All rights reserved.
' Web: http://www.interflower.cn
' Need help? Contact: ranhuan@msn.com
'=========================================================

'=========================================================
'模板中替换的部分用{{%}}表示
'模板中的循环用<!-- BEGIN % -->开始 <!-- END % -->结束 支持一次嵌套


Class Template
Private tmp
Private tpl_dir, tpl, tpl_blk
Private var_list, blk_list, blk_var_list
Private re, match, matchs
Private Sub class_Initialize
sql = ""
tpl_dir = "templates/"
tpl = ""
blk=""
Set var_list = Server.CreateObject("Scripting.Dictionary")
Set blk_list = Server.CreateObject("Scripting.Dictionary")
Set blk_var_list = Server.CreateObject("Scripting.Dictionary")
Set re = New RegExp
End Sub

'取得主体模板
'========================

'从变量取出
Public Sub SetTpl(tplvar)
tpl = tplvar
End Sub

' 从DB中取出,自己修改sql语句
Public Sub SetTplDb(tplname)
Dim sql, rs
Set rs = Server.CreateObject("ADODB.RecordSet")
sql = "SELECT content FROM templates WHERE name = '"&tplname&"'"
rs.Open sql,conn,1,1
If rs.RecordCount <> 1 Then
Response.Write("数据库错误!<br>")
Response.End()
End If
tpl = rs("content")
rs.Close
Set rs = Nothing
End Sub

'从文件取出
Public Sub SetTplFile(tplfile)
Dim FSO, oFile
Set FSO = Server.Createobject("Scripting.FileSystemObject")
If FSO.FileExists(Server.Mappath(tpl_dir & tplfile)) then
Set oFile = FSO.OpenTextFile(Server.Mappath(tpl_dir & tplfile))
tpl = oFile.ReadAll
oFile.Close
Set oFile = Nothing
Else
Response.Write "模板文件不存在!<br>"
End if
Set FSO = nothing
End Sub

'取得区块模板
'========================

'从变量取出
Public sub SetBlk(blkname, tplvar)
re.IgnoreCase = True
re.Global = True
re.Pattern = {{ & blkname & }}
tpl = re.Replace(tpl, tplvar)
rs.Close
End Sub

'从数据库取出
Public sub SetBlkDb(blkname, tplname)
Dim sql, rs
Set rs = Server.CreateObject("ADODB.RecordSet")
sql = "SELECT * FROM templates WHERE name = '"&tplname&"'"
rs.Open sql,conn,1,1
tmp = rs("content")
rs.Close
SetBlk blkname, tmp
set rs = Nothing
End Sub

'从文件取出
Public sub SetBlkFile(blkname, tplfile)
Dim FSO, oFile
Set FSO = createobject("Scripting.FileSystemObject")
If FSO.FileExists(server.mappath(tpl_dir &tplfile)) Then
Set oFile = FSO.OpenTextFile(Server.MapPath(tpl_dir &tplfile))
tmp = oFile.ReadAl
SetBlock blkname, tmp
oFile.Close
set oFile = Nothing
Else
Response.Write "区块模板文件不存在!<br