您现在的位置: 雨哲在线 >> 文章教程 >> Web开发 >> Asp编程 >> 查看文章内容

ASP模板引擎代码

【字体: 】         ★★★ 作者:佚名    文章来源:本站原创    点击数:499    更新时间:2008-06-29    阅读点数:0

载入中

复制内容到剪贴板
雨[http://www.yz81.com]65031哲提示:代码片段
    1. <%   
    2. Class Cls_System   
    3.   
    4.  Dim Reg   
    5.  Dim Code   
    6.  Dim Page   
    7.  Dim Rule   
    8.  Dim Content   
    9.  Dim Template   
    10.  Dim Cachetime   
    11.  Dim DefCachetime   
    12.     
    13.  Private Sub Class_Initialize()   
    14.   Set Reg = New RegExp   
    15.   Reg.Ignorecase = True  
    16.   Reg.Global = True  
    17.   Code = "GB2312"  
    18.   Page = 0   
    19.   Rule = ""  
    20.   Content = ""  
    21.   Template = ""  
    22.   Cachetime = 0   
    23.   DefCachetime = Cachetime   
    24.  End Sub  
    25.     
    26.  Private Sub Class_Terminate()   
    27.   Set Reg = Nothing  
    28.  End Sub  
    29.   
    30.  Public Function Parser(Byval Templatefile)   
    31.   Template = Templatefile   
    32.   If ChkCache(Template) Then Parser = GetCache(Template) : Exit Function  
    33.   If Not IsNumeric(Page) Then Page = 0 Else Page = Int(Page)   
    34.   Loadfile   
    35.   Parser_my   
    36.   Parser_Sys   
    37.   Parser_Com   
    38.   Parser_Com   
    39.   Parser_IF   
    40.   SetCache Template,Content   
    41.   Parser = Content   
    42.  End Function  
    43.     
    44.  Public Function Parser_Sys()   
    45.   On Error Resume Next  
    46.   Dim Matche,SysValue   
    47.   Reg.Pattern = "{Sys:([\s\S]*?)}"  
    48.   Set Matches = Reg.Execute(Content)   
    49.   For Each Match In Matches   
    50.    If Len(Replace(Match.SubMatches(0)," ","")) > 0 Then Execute("SysValue = " & Replace(Match.SubMatches(0)," ","")) Else SysValue = ""  
    51.    Content = Replace(Content, Match.Value, SysValue) '# 替换   
    52.    If Err Then Err.Clear : Response.Write "<font color=red>执行变量标签失败[" & AspArr(i) & "]</font>" : Response.End  
    53.   Next  
    54.  End Function    
    55.     
    56.  Public Function Parser_My()   
    57.   On Error Resume Next  
    58.   Dim Rs,Ns,i   
    59.   Set Rs = DB("Select [Name],[Code] From [5U_Label]",1)   
    60.   If Not Rs.Eof Then Set Ns = Rs.Getrows()   
    61.   Dim Matche,MyValue   
    62.   Reg.Pattern = "{My:([\s\S]*?)}"  
    63.   Set Matches = Reg.Execute(Content)   
    64.   For Each Match In Matches   
    65.    If Not Rs.Eof Then  
    66.     If Len(Replace(Match.SubMatches(0)," ","")) > 0 Then  
    67.      MyValue = Replace(Match.SubMatches(0)," ","")   
    68.      For i = 0 To Ubound(Ns,2)   
    69.       If Lcase(Ns(0,i)) = Lcase(Replace(Match.SubMatches(0)," ","")) Then MyValue = Ns(1,i) : Exit For  
    70.      Next  
    71.     End If  
    72.    Else  
    73.     MyValue = ""  
    74.    End If  
    75.    Content = Replace(Content, Match.Value, MyValue) '# 替换   
    76.    If Err Then Err.Clear : Response.Write "<font color=red>执行自定义标签失败[" & AspArr(i) & "]</font>" : Response.End  
    77.   Next  
    78.   Rs.Close   
    79.   Set Rs = Nothing  
    80.  End Function  
    81.     
    82.  Public Function Parser_Com()   
    83.   On Error Resume Next  
    84.   Dim Rs,i,j   
    85.   Dim Matche,BackValue   
    86.   Dim Tagsstr,Loopstr   
    87.   Dim Tag_Cache,Tag_Row,Tag_Col,Tag_Width,Tag_Sql,Tag_From,Tag_Where,Tag_Order   
    88.   Reg.Pattern = "<!--(.+?):\{(.+?)\}-->([\s\S]*?)<!--\1-->"  
    89.   Set Matches = Reg.Execute(Content)   
    90.   For Each Match In Matches   
    91.    Cachetime    = DefCachetime   
    92.    Tagsstr      = Match.SubMatches(1)   
    93.    Loopstr      = Match.SubMatches(2)   
    94.    Tag_Cache    = GetAttr(Tagsstr,"cache")   
    95.    Tag_Row      = GetAttr(Tagsstr,"row")   
    96.    Tag_Col      = GetAttr(Tagsstr,"col")   
    97.    Tag_Width    = GetAttr(Tagsstr,"width")   
    98.    Tag_Sql      = GetAttr(Tagsstr,"sql")   
    99.    Tag_From     = GetAttr(Tagsstr,"from")   
    100.    Tag_Where    = GetAttr(Tagsstr,"where")   
    101.    Tag_Order    = GetAttr(Tagsstr,"order")   
    102.    If Len(Tag_Cache) = 0 Or Not IsNumeric(Tag_Cache) Then Tag_Cache = Cachetime   
    103.    If Len(Tag_Row) = 0 Or Not IsNumeric(Tag_Row) Then Tag_Row = 10   
    104.    If Int(Tag_Row) < 1 Then Tag_Row = 1   
    105.    If Len(Tag_Col) = 0 Or Not IsNumeric(Tag_Col) Then Tag_Col = 1   
    106.    If Int(Tag_Col) < 1 Then Tag_Col = 1   
    107.    If Len(Tag_Width) = 0 Then Tag_Width = "100%"  
    108.    If Len(Tag_Sql) < 5 Then  
    109.     If Len(Tag_From) = 0 Then Tag_From = "5U_Content" '# 默认表   
    110.     If Len(Tag_Where) > 0 Then Tag_Where = " Where " & Tag_Where '# 条件   
    111.     If Len(Tag_Order) > 0 Then Tag_Order = " Order By " & Tag_Order '# 条件   
    112.     Tag_Sql = "Select Top " & Tag_Row & " * From " & Tag_From & Tag_Where & Tag_Order   
    113.    End If  
    114.    Tag_Cache = Int(Tag_Cache)   
    115.    Tag_Row   = Int(Tag_Row)   
    116.    Tag_Col   = Int(Tag_Col)   
    117.    Cachetime = Tag_Cache   
    118.    If ChkCache(Tag_Sql) Then  
    119.     BackValue = GetCache(Tag_Sql)   
    120.    Else  
    121.     BackValue = ""    
    122.     If Page = 0 Then  
    123.      Set Rs = DB(Tag_Sql,1)   
    124.     Else  
    125.      Set Rs = DB(Tag_Sql,3)   
    126.      Rs.PageSize = Tag_Row * Tag_Col   
    127.      Rs.AbsolutePage = Page   
    128.     End If  
    129.     If Tag_Col > 1 Then BackValue = BackValue & "<table width=""" & Tag_Width & """ border=""0"" cellpadding=""0"" cellspacing=""0"">" & Vbcrlf : j = 0   
    130.     For i = 1 to Tag_Row * Tag_Col   
    131.      If Rs.Eof Then Exit For '# 不存在记录就退出   
    132.      j = j + 1   
    133.      If Tag_Col > 1 Then  
    134.       If j = 1 Then BackValue = BackValue & "  <tr>" & Vbcrlf        
    135.       BackValue = BackValue & "    <td>"  
    136.      End If  
    137.      BackValue = BackValue & Parser_Tags("\[field:(.+?)\]",Loopstr,Rs)    
    138.      If Tag_Col > 1 Then  
    139.       BackValue = BackValue & "    </td>" & Vbcrlf   
    140.       If j = Tag_Col Then BackValue = BackValue & "  </tr>" & Vbcrlf : j = 0   
    141.      End If  
    142.      Rs.MoveNext   
    143.     Next  
    144.     If Tag_Col > 1 Then  
    145.      If j < Tag_Col And j > 0 Then  
    146.       For i = 1 To Tag_Col - J   
    147.        BackValue = BackValue & "    <td></td>" & Vbcrlf   
    148.       Next  
    149.       BackValue = BackValue & "  </tr>" & Vbcrlf   
    150.      End If  
    151.      BackValue = BackValue & "</table>" & Vbcrlf    
    152.     End If  
    153.     If Page >= 1 Then RegReplace "{tag:pagelist}",PageListX(Rs.PageCount,Rs.RecordCount,Page,Tag_Row * Tag_Col,Rule)   
    154.     Rs.Close   
    155.     SetCache Tag_Sql,BackValue    
    156.    End If  
    157.    Content = Replace(Content, Match.Value, BackValue)   
    158.   Next  
    159.  End Function  
    160.     
    161.  Public Function Parser_Tags(Byval Pattern,Byval Temp,Byval Dat)   
    162.   Dim Matche   
    163.   Dim Tagsstr,Tagsval,Tagsvalt   
    164.   Dim Tag_Len,Tag_Lenext,Tag_Format,Tag_Replace,Tag_Clearhtml   
    165.   Dim Re,Re1,Re2   
    166.   Dim i,c,l   
    167.   Reg.Pattern = Pattern   
    168.   Set Matches = Reg.Execute(Temp)   
    169.   For Each Match In Matches   
    170.    Tagsstr        = Match.SubMatches(0)   
    171.    Tag_Len        = GetAttr(Tagsstr,"len")   
    172.    Tag_Lenext     = GetAttr(Tagsstr,"lenext")   
    173.    Tag_Format     = GetAttr(Tagsstr,"format")   
    174.    Tag_Replace    = GetAttr(Tagsstr,"replace")   
    175.    Tag_Clearhtml  = GetAttr(Tagsstr,"clearhtml")   
    176.       
    177.    Tagsval        = Split(Tagsstr," ")(0)   
    178.    Tagsval        = Dat(Tagsval)   
    179.    Tagsval        = Replace(Replace(Replace(Replace(Tagsval," "," "),""",chr(34)),">",">"),"<","<")   
    180.    If Len(Replace(Tag_Replace," ","")) > 0 Then  
    181.     Re = Split(Tag_Replace,"##")   
    182.     If Ubound(Re) >= 0 Then Re1 = Re(0) : Re2 = Re(1) Else Re1 = Re(0): Re2 = Re(0)   
    183.     Tagsval = Replace(Tagsval,Re1,Re2)   
    184.    End If  
    185.    If Len(Replace(Tag_Format," ","")) > 0 Then '# 格式化时间   
    186.     If IsDate(Tagsval) Then  
    187.      Tagsvalt = Tagsval : Tagsvalt = Lcase(Tag_Format)   
    188.      Tagsvalt = Replace(Tagsvalt,"yyyy",Year(Tagsval))                : Tagsvalt = Replace(Tagsvalt,"yy",Right(Year(Tagsval),2))   
    189.      Tagsvalt = Replace(Tagsvalt,"mm",Right("0" & Month(Tagsval),2))  : Tagsvalt = Replace(Tagsvalt,"m",Month(Tagsval))   
    190.      Tagsvalt = Replace(Tagsvalt,"dd",Right("0" & Day(Tagsval),2))    : Tagsvalt = Replace(Tagsvalt,"d",Day(Tagsval))   
    191.      Tagsvalt = Replace(Tagsvalt,"hh",Right("0" & Hour(Tagsval),2))   : Tagsvalt = Replace(Tagsvalt,"h",Hour(Tagsval))   
    192.      Tagsvalt = Replace(Tagsvalt,"nn",Right("0" & Minute(Tagsval),2)) : Tagsvalt = Replace(Tagsvalt,"n",Minute(Tagsval))   
    193.      Tagsvalt = Replace(Tagsvalt,"ss",Right("0" & Second(Tagsval),2)) : Tagsvalt = Replace(Tagsvalt,"s",Second(Tagsval))   
    194.      Tagsval  = Tagsvalt   
    195.     End If  
    196.    End If  
    197.    If Lcase(Replace(Tag_Clearhtml," ","")) = "true" Then  
    198.     Reg.Pattern = "(\<.+?\>)"  
    199.     Tagsval = Reg.Replace(Tagsval, " ")   
    200.     Tagsval = Trim(Tagsval)   
    201.    End If  
    202.    Tag_Len = Replace(Tag_Len," ","")   
    203.    If Len(Tag_Len) > 0 And IsNumeric(Tag_Len) Then  
    204.     For i = 1 To Len(Tagsval)   
    205.      c = Abs(Asc(Mid(str,i,1)))    
    206.      If c > 255  Or c < 1 Then t = t + 2 Else t = t + 1   
    207.      If t >= Tag_Len then Tagsval = Left(Tagsval, i) & Tag_Lenext : Exit For  
    208.     Next  
    209.    End If  
    210.    Temp = Replace(Temp, Match.Value, Tagsval)   
    211.   Next  
    212.   Parser_Tags = Temp   
    213.  End Function  
    214.     
    215.  Public Function Parser_IF()   
    216.   On Error Resume Next  
    217.   Dim TestIF   
    218.   Reg.Pattern = "{If:(.+?)}([\s\S]*?){Else}([\s\S]*?){End}"  
    219.   Set Matches = Reg.Execute(Content)   
    220.   For Each Match In Matches   
    221.    Execute("If" & Match.SubMatches(0) & " Then TestIf = True Else TestIf = False")   
    222.    If TestIf Then Content = Replace(Content, Match.Value, Match.SubMatches(1)) Else Content = Replace(Content, Match.Value, Match.SubMatches(2)) '# 替换   
    223.    If Err Then Err.Clear : Response.Write "<font color=red>执行IF标签失败[" & Match.SubMatches(0) & "]</font>" : Response.End  
    224.   Next  
    225.  End Function  
    226.     
    227.  Public Function RegReplace(Byval Pattern,Byval ReplaceVal)   
    228.   Reg.Pattern = Pattern   
    229.   Set Matches = Reg.Execute(Content)   
    230.   For Each Match In Matches   
    231.    Content = Replace(Content, Match.Value, ReplaceVal)   
    232.   Next  
    233.  End Function  
    234.     
    235.  Public Function GetAttr(Byval Tagsstr,Byval AttrName)   
    236.   Tagsstr = Tagsstr & " $"  
    237.   Reg.Pattern = "\$" & AttrName & "=(.+?) \$"  
    238.   Set Matches = Reg.Execute(Tagsstr)   
    239.   For Each Match In Matches   
    240.    GetAttr = Match.SubMatches(0)   
    241.   Next  
    242.  End Function  
    243.     
    244.  Function SetCache(Byval CacheName,Byval CacheValue)   
    245.   CacheName = Filterstr(CacheName)   
    246.   Application.Lock   
    247.   Application("Template_" & CacheName) = CacheValue : Application("Template_" & CacheName & ".Time") = Now()   
    248.   Application.UnLock   
    249.  End Function  
    250.     
    251.  Function ChkCache(Byval CacheName)   
    252.   Dim GetValue,GetTime   
    253.   GetValue = GetCache(CacheName) : GetTime  = GetCache(CacheName & ".Time")   
    254.   If IsNull(GetValue) Or IsEmpty(GetValue) Then ChkCache = False : Exit Function  
    255.   If Not IsDate(GetTime) Then ChkCache = False : Exit Function  
    256.   If DateDiff("s",CDate(GetTime),Now()) >= Cachetime Then ChkCache = False Else ChkCache = True  
    257.  End Function  
    258.     
    259.  Function GetCache(Byval CacheName)   
    260.   CacheName = Filterstr(CacheName) : GetCache = Application("Template_" & CacheName)   
    261.  End Function  
    262.     
    263.  Function Filterstr(Byval Str)   
    264.   Filterstr = LCase(Str) : Filterstr = Replace(Filterstr," ","") : Filterstr = Replace(Filterstr,"'","") : Filterstr = Replace(Filterstr,"""","") : Filterstr = Replace(Filterstr,"=","") : Filterstr = Replace(Filterstr,"*","")   
    265.  End Function  
    266.     
    267.  Public Function Loadfile()   
    268.   Dim Obj   
    269.   On Error Resume Next  
    270.   Set Obj = Server.Createobject("adodb.stream")   
    271.   With Obj   
    272.   .Type = 2 : .Mode = 3 : .Open : .Charset = Code : .Position = Obj.Size : .Loadfromfile Server.Mappath(Template) : Content = .ReadText : .Close   
    273.   End With  
    274.   Set Obj = Nothing  
    275.   If Err Then Response.Write "<font color=red>无法加载模板[" & Template & "]</font>" : Response.End  
    276.  End Function  
    277. End Class  
    278. %>

文章来源:雨#哲在线

网友看法
    数据载入中,请稍后……
    发表评论
    昵 称: 邮 箱:
    评 分:
    内 容:
    0/5000)
    插入链接
    验证码:

    雨哲提示:Alt+S快速发表
用户信息中心
与本文章相关内容
广告位招商QQ:425162221