当前位置:K88软件开发文章中心编程语言AspAsp01 → 文章内容

asp实现静态页面上加自定义的if语句

减小字体 增大字体 作者:佚名     来源:asp编程网  发布时间:2018-12-30 8:32:31

php中有一个叫smarty,可以实现在静态页面上加一些自定义的if语句,运行后实现自己想要的效果,现在我想将这种思想用asp来实现,经过一天的测试,终于搞定,现在放到这里与大家共享。
静态页面:1.html 

 
 
<
TABLE 
border="1" 
width="500">

 
 
<
TR 
bgcolor="<
!-- 
if 
1>
22 
then 
-->

#ff0000<
!-- 
else 
-->

#cccccc<
!-- 
end 
if 
-->
">

 
<
TD>
aspprogram.cn<
/TD>

 
<
TD>
aaaaaaa<
/TD>

 
 
<
/TR>

 
 
<
TR 
bgcolor="<
!-- 
if 
2>

then 
-->

#ff0000<
!-- 
end 
if 
-->
">

 
<
TD>
bbbbbbb<
/TD>

 
<
TD>
cccccc<
/TD>

 
 
<
/TR>

 
 
<
/TABLE>

要得到的效果是:
 
 
<
TABLE 
border="1" 
width="500">

 
 
<
TR 
bgcolor="
#cccccc">

 
<
TD>
aspprogram.cn<
/TD>

 
<
TD>
aaaaaaa<
/TD>

 
 
<
/TR>

 
 
<
TR 
bgcolor="
#ff0000">

 
<
TD>
bbbbbbb<
/TD>

 
<
TD>
cccccc<
/TD>

 
 
<
/TR>

 
 
<
/TABLE>


实现的代码是:1.asp

<
 
%
’===========================================
’ 
代码功能:asp实现静态页面上加自定义的if语句
’ 
作 
 
 
 
者:wangsdong
’ 
网 
 
 
 
站: 
http://www.aspprogram.cn  

’ 
文章为作者支持!
’===========================================
Set 
MyFileObject=Server.CreateObject("Scripting.FileSystemObject")
path=server.mappath("1.html")
Set 
MyTextFile=MyFileObject.OpenTextFile(path)
str=MyTextFile.readall
MyTextFile.Close
Set 
MyFileObject=Nothing
s="<
!-- 
if 
.* 
end 
if 
-->
"
response.write 
RegExpTest(str,s)
Function 
regExReplace(sSource,patrn, 
replStr) 

Dim 
regEx, 
str1 

str1 

sSource 

Set 
regEx 

New 
RegExp 

regEx.Pattern 

patrn 

regEx.IgnoreCase 

True 

regEx.Global 

True 

regExReplace 

regEx.Replace(str1, 
replStr) 

End 
Function
Function 
RegExpTest(strng,s)
strng1=strng
Dim 
regEx, 
Match, 
Matches 
 
 
’ 
建立变量。
Set 
regEx 

New 
RegExp 
 
 
’ 
建立正则表达式。
regEx.Pattern 


 
 
’ 
设置模式。
regEx.IgnoreCase 

True 
 
 
’ 
设置是否区分大小写。
regEx.Global 

True 
 
 
’ 
设置全局替换。
Set 
Matches 

regEx.Execute(strng) 
 
 
’ 
执行搜索。
For 
Each 
Match 
in 
Matches 
 
 
’ 
遍历 
Matches 
集合。
 
str0=match.value
 
str0=Right(str0,Len(str0)-4)
 
str0=Left(str0,Len(str0)-4) 

 
str2=Replace(str0,"<
!--","""")
 
str2=Replace(str2,"-->
","kkk=""")
 
str2=Replace(str2,"end 
if","")
 
execute(str2)
 
response.write 
vbnewline
 
 
 
 
strng1=regExReplace(strng1,Match.value,kkk)
Next
RegExpTest 

strng1
end 
Function

>


asp实现静态页面上加自定义的if语句