网上找了一些关于5ucms if判断标签的帖子,出来的内容基本上一样,但是那个代码有错误。于是修正了一下:
5uCMS模板设计优秀,简练灵活,但IF标签不支持嵌套,有些场合不方便。本站给出一个简单的解决方案,以最小的改动实现IF的嵌套。
将 inc\class_template.asp 458行的 Parser_IF 函数整体注释掉,改成下面两个函数
Public Function Parser_IF()
Dim PreLB
PreLB = ""
Parser_IF2(PreLB)
End Function
' 判断标签
Public Function Parser_IF2(ByVal PreLB)
On Error Resume Next
Dim Matches, Match,TestIF,TestVal,TestTrue,TestFalse
Dim NewPreLB
Reg.Pattern = "{" & PreLB & "If:(.+?)}([\s\S]*?){" & PreLB & "End If}"
Set Matches = Reg.Execute(Content)
For Each Match In Matches
TestVal=Match.SubMatches(1)
If Instr(LCase(TestVal), "{" & PreLB & "else}")>0 Then
TestVal=Replace(TestVal, "{" & PreLB & "else}", "{" & PreLB & "else}",1,-1,1):TestTrue=Split(TestVal, "{" & PreLB & "else}")(0):TestFalse=Split(TestVal, "{" & PreLB & "else}")(1)
Else
TestTrue=TestVal:TestFalse=""
End If
Execute ("If " & replace(Match.SubMatches(0),"'","""") & " Then TestIf = True Else TestIf = False")
If TestIF Then Content = Replace(Content, Match.Value, TestTrue) Else Content = Replace(Content, Match.Value, TestFalse)
If Err Then Response.Write "<font color=red>" & Lang_Parser_IF_Error & "[" & Match.SubMatches(0) & "]" & Err.Description & "</font>": Err.Clear: Response.End
Next
NewPreLB = PreLB & "-"
If RegExists("{" & NewPreLB & "If:(.+?)}([\s\S]*?){" & NewPreLB & "End If}", Content) Then Call Parser_IF2(NewPreLB) ' 多次调用
End Function
前台模板用法
{if: XXXX}
{-if: XXXX}
{-else}
.....
{-End if}
{else}
.....
{end if}
即在嵌套的IF语句前加“-”,同理两级嵌套加两个,以此类推,层次足够你用哦
最小修改实现5UCMS IF语句的嵌套
5uCMS模板设计优秀,简练灵活,但IF标签不支持嵌套,有些场合不方便。本站给出一个简单的解决方案,以最小的改动实现IF的嵌套。
将 inc\class_template.asp 458行的 Parser_IF 函数整体注释掉,改成下面两个函数
Public Function Parser_IF()
Dim PreLB
PreLB = ""
Parser_IF2(PreLB)
End Function
' 判断标签
Public Function Parser_IF2(ByVal PreLB)
On Error Resume Next
Dim Matches, Match,TestIF,TestVal,TestTrue,TestFalse
Dim NewPreLB
Reg.Pattern = "{" & PreLB & "If:(.+?)}([\s\S]*?){" & PreLB & "End If}"
Set Matches = Reg.Execute(Content)
For Each Match In Matches
TestVal=Match.SubMatches(1)
If Instr(LCase(TestVal), "{" & PreLB & "else}")>0 Then
TestVal=Replace(TestVal, "{" & PreLB & "else}", "{" & PreLB & "else}",1,-1,1):TestTrue=Split(TestVal, "{" & PreLB & "else}")(0):TestFalse=Split(TestVal, "{" & PreLB & "else}")(1)
Else
TestTrue=TestVal:TestFalse=""
End If
Execute ("If " & replace(Match.SubMatches(0),"'","""") & " Then TestIf = True Else TestIf = False")
If TestIF Then Content = Replace(Content, Match.Value, TestTrue) Else Content = Replace(Content, Match.Value, TestFalse)
If Err Then Response.Write "<font color=red>" & Lang_Parser_IF_Error & "[" & Match.SubMatches(0) & "]" & Err.Description & "</font>": Err.Clear: Response.End
Next
NewPreLB = PreLB & "-"
If RegExists("{" & NewPreLB & "If:(.+?)}([\s\S]*?){ & NewPreLB & "End If}", Content) Then Call Parser_IF2(NewPreLB) ' 多次调用
End Function
前台模板用法
{if: XXXX}
{-if: XXXX}
{-else}
.....
{-End if}
{else}
.....
{end if}
即在嵌套的IF语句前加“-”,同理两级嵌套加两个,以此类推,层次足够你用哦