此类支持循环替换,支持嵌套循环替换,支持多模板。
类的代码就不贴不来啰嗦了,在后面有下载,只贴出demo的代码。
一、模板只有一个循环块
模板文件主要代码
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>{myname}模板-{webName}</title>
</head>
<body>
<table width="500" border="0" cellspacing="0" cellpadding="0">
<!-- begin a_block -->
<tr>
<td width="50" height="30">第一列</td>
<td width="50" height="30">{tpl_1}</td>
<td width="50" height="30">{tpl_2}</td>
</tr>
<!-- end a_block -->
</table>
</body>
替换模板的代码
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<!--#include file="tplCls.asp"-->
<%
response.Charset="utf-8"
set newTpl=new tpl
newTpl.setTpl="tpl.html"
newTpl.setVar "myname","一个块循环的"
newTpl.setVar "webName","亮亮的博客"
newTpl.setBlock="a_block"
for tt=0 to 2
newTpl.setBloVar "tpl_1","第二列_"&tt
newTpl.setBloVar "tpl_2","第三列_"&tt
newTpl.outBloVar
next
newTpl.outBlock "a_block"
newTpl.out
set newTpl=nothing
%>
二、两个块循环,并且在一个模板中加载另一个模板
主模板文件主要代码
<title>{myname}模板-{webName}</title>
</head>
<body>
<table cellspacing="2" border="1">
<tr><td>下面的动物您喜欢哪一种</td></tr>
<!-- begin list1 -->
<tr><td><input type="radio" name="chk">{animal}</td></tr>
<tr bgcolor="#3366FF"><td height="10"></td>
</tr>
<!-- end list1 -->
<!-- begin list2 -->
<tr><td><input type="radio" name="chk">{animal2}</td></tr>
<tr bgcolor="#3366FF"><td height="10"></td>
</tr>
<!-- end list2 -->
<tr><td colspan="2">{PageNo}</td></tr>
</table>
</body>
副模板文件主要代码
<table width="500" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="30">这是嵌套进来的模板</td>
</tr>
<tr>
<td height="30"><!-- begin inList -->{inStr} <!-- end inList --></td>
</tr>
</table>
替换模板的代码
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<!--#include file="tplCls.asp"-->
<%
response.Charset="utf-8"
set newTpl=new tpl
newTpl.setTpl="tpl2.html"
newTpl.setVar "myname","两个块循环/嵌套的"
newTpl.setVar "webName","亮亮的博客"
newTpl.setBlock="list1"
for i=0 to 2
newTpl.setBloVar "animal","animal"&i
newTpl.outBloVar
next
newTpl.outBlock "list1"
newTpl.setBlock="list2"
for m=0 to 2
newTpl.setBloVar "animal2","动物"&m
newTpl.outBloVar
next
newTpl.outBlock "list2"
'加载副模板
newTpl.setVarTpl "PageNo","inTpl.html"
'替换副模板中的内容
newTpl.setBlock="inList"
for k=0 to 3
newTpl.setBloVar "inStr","menu"&k
newTpl.outBloVar
next
newTpl.outBlock "inList"
newTpl.out
set newTpl=nothing
%>
三、块里有嵌套的循环
模板文件主要代码
<title>{myname}模板-{webName}</title>
</head>
<body>
<table width="400" border="1" bordercolor="#000000">
<tr><td><div align="center">{myname}测试</div></td></tr>
<tr><td>动物:</td> </tr>
<!-- begin list -->
<tr><td>{animal}</td></tr>
<!-- begin list_1 -->
<tr><td> {plant}_{num}</td></tr>
<!-- begin list_1_1 -->
<tr><td> {plant2}</td></tr>
<!-- end list_1_1 -->
<!-- end list_1 -->
<!-- end list -->
</table>
</body>
替换模板文件的代码
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<!--#include file="tplCls.asp"-->
<%
response.Charset="utf-8"
set newTpl=new tpl
newTpl.setTpl="tpl3.html"
newTpl.setVar "myname","嵌套循环的"
newTpl.setVar "webName","亮亮的博客"
newTpl.setBlock="list"
for i=0 to 2
newTpl.setBloVar "animal","动物大类"&i
newTpl.outBloVar
newTpl.setBlock="list_1"
for m=0 to 2
newTpl.setBloVar "plant","动物类"
newTpl.setBloVar "num",m
newTpl.outBloVar
newTpl.setBlock="list_1_1"
for k=0 to 1
newTpl.setBloVar "plant2","动物"&k
newTpl.outBloVar
next
newTpl.outInBlock "list_1","list_1_1"
newTpl.resetBlock="list_1"
next
newTpl.outInBlock "list","list_1"
'注意resetBlock的使用
newTpl.resetBlock="list"
next
newTpl.outBlock "list"
newTpl.out
set newTpl=nothing
%>
注意resetBlock使用的地方,在块里还有循环的时候,在next之前用这个方法。
四、模板里有循环块没有单变量
模板文件主要代码
<title>模板-{webName}</title>
</head>
<body>
<table width="400" border="1" bordercolor="#000000">
<tr><td><div align="center">{myname}测试</div></td></tr>
<tr><td>看看测试</td> </tr>
<!-- begin list -->
<tr><td>{animal}</td></tr>
<!-- begin list_1 -->
<tr><td> <!-- begin list_1_1 --> {plant} <!-- end list_1_1 --></td></tr>
<!-- end list_1 -->
<tr><td>{animal2}</td></tr>
<!-- end list -->
<tr><td><div align="center">{myname2}</div></td></tr>
</table>
</body>
注意在list_1块中没有像在list块中的animal这样的要替换的单变量
替换模板的代码
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<!--#include file="tplCls.asp"-->
<%
response.Charset="utf-8"
set newTpl=new tpl
newTpl.setTpl="tpl4.html"
newTpl.setVar "myname","有一个块没有变量要替换的嵌套循环的"
newTpl.setVar "webName","亮亮的博客"
newTpl.setBlock="list"
for i=0 to 2
newTpl.setBloVar "animal","动物类别_"&i
newTpl.setBloVar "animal2","动物类别_"&i&"结束"
newTpl.outBloVar
newTpl.setBlock="list_1"
for m=0 to 2
'list_1块里没有单变量要替换也要执行setBloVar和outBloVar,参数用空代替
newTpl.setBloVar "",""
newTpl.outBloVar
newTpl.setBlock="list_1_1"
for k=0 to 1
newTpl.setBloVar "plant","动物"&k
newTpl.outBloVar
next
newTpl.outInBlock "list_1","list_1_1"
newTpl.resetBlock="list_1"
next
newTpl.outInBlock "list","list_1"
newTpl.resetBlock="list"
next
newTpl.outBlock "list"
newTpl.setVar "myname2","测试结束"
newTpl.out
set newTpl=nothing
%>
最后介绍一下类里的主要方法
newTpl.setTpl="tpl.html" 设置主要模板文件
newTpl.setVar "myname","一个块循环的" 替换模板文件中块之外的变量
newTpl.setVarTpl "PageNo","inTpl.html" 用副模板文件替换单变量即加载副模板
newTpl.setBloVar "tpl_2","第三列_"&tt 替换块里的变量
newTpl.outBloVar 输入块里所有替换后的内容
newTpl.setBlock="a_block" 设置循环快
newTpl.outBlock "a_block" 输出循环块
newTpl.out 输出所有
newTpl.resetBlock="list" 这是个比较特殊的,若list块里还有要循环的块,在循环list块的代码的next之前要用它。
asp_tplClass.rar