Open GoogleCodeExporter opened 9 years ago
clstemple.asp:
<%
Class Template
Private FSO,re
public last_error,halt_on_error,debug
Private m_root,unKnowns,m_nfile
Private varkeys,varvals
Private Sub class_Initialize
Set FSO=CreateObject("Scripting.FileSystemObject")
Set re = new regexp : re.IgnoreCase = true : re.Global = true
Set m_nfile=CreateObject("Scripting.Dictionary")
Set varkeys=CreateObject("Scripting.Dictionary")
Set varvals=CreateObject("Scripting.Dictionary")
m_root="templates/"
unknowns="keep"
debug="false"
halt_on_error="yes"
End Sub
Public Sub Set_Root(Byval root)
If FSO.FolderExists(Server.MapPath(root)) Then
m_root=replace(root,"\","/")
If m_root<>"" and not Right(m_root,1)="/" Then
m_root=m_root & "/"
If debug Then response.write "Set_Root:Root
is "&m_root&".<br>"
Else
Call halt("模版目录名有错.")
Exit Sub
End If
Else
Call halt("找不到模版目录.")
Exit Sub
End If
End Sub
Public Property Get Root
Root=m_root
End Property
Public Sub set_unknowns(ByVal A_strUnknowns)
Unknowns = A_strUnknowns
End Sub
Public Sub set_file(handle,str_filename)
Dim str_tmp
If NOT IsArray(handle) Then
If str_filename="" Then
Call halt("set_file: For handle"& handle &"filename is
empty.<br>")
Exit Sub
End If
str_tmp=filename(str_filename)
If debug Then response.write "set_file:set " & handle & "=" &
Str_tmp & ".<br>"
m_nfile.add handle,str_tmp
Else
Call setFiles(handle)
End If
End Sub
Public Sub setfiles(s_handle)
Dim num,i
If IsArray(s_handle) Then
num=UBOUND(s_handle)
If((num+1) mod 2)<>0 Then
Call halt("set_file:For handle's array have some
error")
Exit Sub
Else
For i=0 to num step 2
Call Set_file(s_handle(i),s_handle(i+1))
Next
End If
End If
End Sub
Public Function get_undefined(handle)
Dim Str,i,result
If Not LoadFile(handle) Then
Call halt("get_undefined: unable to load "& handle&".<br>")
get_undefined=false
Exit Function
End If
Str=get_var(handle)
re.Pattern = "({)([^ \t\r\n}]+)}"
Set matches=re.execute(Str)
i=0
For each match in matches
If not varvals.exists(match.submatches(1)) Then
result(i)=match.submatches(1)
i=i+1
End If
Next
If IsArray(result) Then
get_undefined=result
Else
get_undefined=false
End If
End Function
Public Sub set_block(parent,handle,str_Name)
Dim Str,matches,matche,str_matche
If Not LoadFile(parent) Then
Call halt("subst: unable to load"& parent &".")
Exit Sub
End If
If str_name="" Then str_name=handle
Str=Get_var(parent)
re.Pattern = "<!" & "--\s+BEGIN\s+(" & handle & ")\s+-->([\s\S.]*)<!" & "--
\s+END\s+" & handle & "\s+-->"
Set Matches = re.Execute(Str)
For each matche In matches
'response.write matche.value &"dd"
str_matche=matche.submatches(1)
Str=re.replace(Str,"{" & str_name & "}")
Call Set_Var(handle,str_Matche)
Call Set_Var(parent,Str)
Next
End Sub
Public Sub Set_var(byval str_varname,byval value)
Dim Str
If Not IsArray(str_varname) Then
If str_varname<>"" Then
If debug Then response.write "scalar: set "& str_varname &"
to "& value &"<br>"&vbCRLF
End If
Str=varname(str_varname)
If varkeys.Exists(str_varname) Then
varkeys.remove(str_varname)
varkeys.add str_varname,"/"& Str &"/"
Else
varkeys.add str_varname,"/"& Str &"/"
End If
If debug Then
response.write "set_var:varkeys.add "&str_varname&","&str&".<br>"
If varvals.Exists(str_varname) Then
varvals.remove(str_varname)
varvals.add str_varname,value
Else
varvals.add str_varname,value
End If
Else
Call Setvars(str_varname)
End If
End Sub
Public Sub SetVars(ByVal A_varname)
Dim i, num
If IsArray(A_varname) Then
num = Ubound(A_varname)
if ((num +1) mod 2) <> 0 Then
Call halt("SetVars: For varname array's element not gemination.<br>")
Exit Sub
Else
For i = 0 To num Step 2
Call Set_Var(A_varname(i), A_varname(i+1))
Next
End If
Else
Call Set_Var(A_varname, "")
End If
End Sub
Public Function subst(handle)
Dim Str
If not LoadFile(handle) Then
Call halt("subst:unableto load "& handle &".<br>")
subst=false
End If
Str=get_var(handle)
re.Pattern="({)([^\t\r\n}]+)}"
Set matches=re.execute(Str)
For each match in matches
If varvals.exists(match.submatches(1)) Then
re.pattern=match.value
Str=re.replace(Str,varvals.item
(match.submatches(1)))
End If
Next
subst=Str
End Function
Public Sub psubst(handle)
response.write subst(handle)
End Sub
Public Function myparse(target,handle,Append)
Dim Str
If NOT IsArray(handle) Then
Str=subst(handle)
If(Append) Then
Call set_var(target,Get_var(target)&Str)
Else
Call set_var(target,Str)
End If
Else
For i=0 to UBound(handle)
Str=subst(handle(i))
If(Append) Then
Call set_var(target,Get_var(target)&Str)
Else
Call set_var(target,Str)
End If
Next
End If
myparse=Str
End Function
Public Function pparse(target,handle,Append)
response.write myparse(target,handle,Append)
End Function
Public Function p(varname)
response.write finish(get_var(varname))
End Function
Public Function vGet(varname)
vGet=finish(get_var(varname))
End Function
Public Function finish(Str)
Dim str_trmp
Select Case unknowns
Case "keep"
str_tmp=Str
Case "remove"
re.Pattern = "({)([^ \t\r\n}]+)}"
str_tmp=re.replace(Str,"")
Case "comment"
re.Pattern = "({)([^ \t\r\n}]+)}"
Set mathces=re.execute(Str)
For each match in mathces
str_tmp=Replace(Str, "<!"&"-- Template variable " &
Match.SubMatches(1) &" undefined -->")
Next
End Select
finish=str_tmp
End Function
Private Function varname(Str)
varname="{" & Str & "}"
End Function
Public Function get_vars()
Set get_vars=varvals
End Function
Public Function Get_Var(varname)
Dim tmp,Count,Str
If NOT IsArray(varname) Then
If varvals.Exists(varname) Then
get_var=varvals.Item(varname)
End If
Else
Set tmp=CreateObject("scripting.Dictionary")
Count=UBound(varname)
For i=0 to Count
If varvals.Exists(varname(i)) Then
Str=varvals.Item(varname(i))
tmp.add varname(i),Str
If debug Then response.write "Get_var: set "&
varname(i) &" to "& Str &"<br>"&vbCRLF
End If
Next
Set getvar=tmp
End If
End Function
Private Function LoadFile(handle)
Dim Str_filename,str_file,Str
If varkeys.Exists(handle) and varkeys.Item(handle)<>"" Then
LoadFile=true
Exit Function
End If
If Not m_nfile.Exists(handle) then
call halt("loadfile:"& handle &" is not a valid handle.<br>")
LoadFile=False
Exit Function
End If
str_filename=m_nfile.Item(handle)
str_filename=Server.Mappath(str_filename)
If debug Then response.write str_filename &"<br>"
If Not FSO.FileExists(str_filename) Then
Call halt("loadfile:"& str_filename &" is not a valid
file.<br>")
LoadFile=false
Exit Function
End If
Set str_file=FSO.OpenTextFile(Str_filename)
Str=str_file.readall()
str_file.close
Set str_file=nothing
If Str="" Then
Call halt("loadfile: While loading"& handle &","&
str_filename &"does not exist or is empty.<br>")
LoadFile=false
Exit Function
End If
Call set_var(handle,Str)
LoadFile=true
End Function
Private Function FileName(str_filename)
FileName=m_root & str_filename
End Function
Public Sub Halt(msg)
last_error=msg
If halt_on_error<>"no" Then Call haltmsg(msg)
If halt_on_error="yes" Then
response.write "Halted."
response.end
End If
End Sub
Public Sub haltmsg(msg)
Response.Write "Template Error:" & msg & "<br>"
End Sub
Public Sub viewcache()
response.write "varkeys:<br>"
a=varkeys.keys
For d=0 to varkeys.count-1
response.write "key:"&a(d) &" value:"& varkeys.item(a(d))
&"<br>"
Next
response.write "varvals:<br>"
a=varvals.keys
For d=0 to varvals.count-1
response.write "key:"&a(d) &" value:"& varvals.item(a(d))
&"<br>"
Next
End Sub
Private Sub class_Terminate
Set FSO = Nothing
Set m_nfile=nothing
Set VarKeys = Nothing
Set VarVals = Nothing
Set re = Nothing
End Sub
'使用方法如:Call Navagationlist
("Category","Category_ID,Category_Name","Category_ID ASC","yubo")
'必须提前调用yuclsdbctrl.asp
Public Function Navagationlist1(ByVal TableName,ByVal FieldsList,ByVal
OrderField,ByVal fname)
Dim sql
Call OpenConn() '打开数据库连接
Dim db : Set db = New DbCtrl '建立对象
sql = db.wGetRecord(TableName,FieldsList,"",OrderField,"-1")
Set rs = db.GetRecordBySQL(sql)
temparray=rs.getrows
response.write "<SELECT SIZE=""1"" name="&fname&" width=""200"">"
response.write "<OPTION>选择类别</OPTION>"
For i = 0 To ubound(temparray,2)
response.write "<OPTION VALUE= "&temparray(0,i)&" > "&temparray(1,i)
&"</OPTION>"
If i > rs.recordcount Then
Exit For
End if
Next
Set rs = nothing
call Closemyobj(db)
Call closeconn()
End Function
'使用方法如:Call Navagationlist
("Category","Category_ID,Category_Name","Category_ID ASC","yubo")
'必须提前调用yuclsdbctrl.asp
Public Function Navagationlist(ByVal TableName,ByVal FieldsList,ByVal
OrderField,ByVal fname)
Dim rs,sql,str
OpenConn() '打开数据库连接
Dim db : Set db = New DbCtrl '建立对象
sql = db.wGetRecord(TableName,FieldsList,"",OrderField,"-1")
Set rs = db.GetRecordBySQL(sql)
temparray=rs.getrows
str= "<SELECT SIZE=""1"" name="&fname&" width=""200"">"
str= str &"<OPTION>选择类别</OPTION>"
For i = 0 To ubound(temparray,2)
str= str &"<OPTION VALUE= "&temparray(0,i)&" > "&temparray(1,i)
&"</OPTION>"
If i > rs.recordcount Then
Exit For
End if
Next
Navagationlist=str
Set rs = nothing
call Closemyobj(db)
Call closeconn()
End function
End Class
%>
Original comment by jioudaot...@gmail.com
on 6 Nov 2009 at 2:24
clsdbctrl.asp内容如附件
Original comment by jioudaot...@gmail.com
on 6 Nov 2009 at 2:29
Attachments:
@jioudaotian
你不是说要放个例子出来么~
Original comment by cxxk...@gmail.com
on 21 Nov 2009 at 6:41
Original issue reported on code.google.com by
jioudaot...@gmail.com
on 6 Nov 2009 at 2:24