欢迎光临
我们一直在努力

ASP模板类[实现一维循环和二维循环,可以从文件、数据库、变量取摸板]-ASP教程,ASP应用

建站超值云服务器,限时71元/月

<%

=========================================================

file: class_template.asp

version:1.0

date: 2004-5-7

script written by r.h

description: asp template class

=========================================================

copyright (c) 2004 interflower studios. all rights reserved.

web: http://www.interflower.cn

need help? contact: ranhuan@msn.com

=========================================================

=========================================================

模板中替换的部分用{{%}}表示

模板中的循环用<!– begin % –>开始 <!– end % –>结束 支持一次嵌套

class template

private tmp

private tpl_dir, tpl, tpl_blk

private var_list, blk_list, blk_var_list

private re, match, matchs

private sub class_initialize

sql = ""

tpl_dir = "templates/"

tpl = ""

blk=""

set var_list = server.createobject("scripting.dictionary")

set blk_list = server.createobject("scripting.dictionary")

set blk_var_list = server.createobject("scripting.dictionary")

set re = new regexp

end sub

取得主体模板

========================

从变量取出

public sub settpl(tplvar)

tpl = tplvar

end sub

从db中取出,自己修改sql语句

public sub settpldb(tplname)

dim sql, rs

set rs = server.createobject("adodb.recordset")

sql = "select content from templates where name = "&tplname&""

rs.open sql,conn,1,1

if rs.recordcount <> 1 then

response.write("数据库错误!<br>")

response.end()

end if

tpl = rs("content")

rs.close

set rs = nothing

end sub

从文件取出

public sub settplfile(tplfile)

dim fso, ofile

set fso = server.createobject("scripting.filesystemobject")

if fso.fileexists(server.mappath(tpl_dir & tplfile)) then

set ofile = fso.opentextfile(server.mappath(tpl_dir & tplfile))

tpl = ofile.readall

ofile.close

set ofile = nothing

else

response.write "模板文件不存在!<br>"

end if

set fso = nothing

end sub

取得区块模板

========================

从变量取出

public sub setblk(blkname, tplvar)

re.ignorecase = true

re.global = true

re.pattern = {{ & blkname & }}

tpl = re.replace(tpl, tplvar)

rs.close

end sub

从数据库取出

public sub setblkdb(blkname, tplname)

dim sql, rs

set rs = server.createobject("adodb.recordset")

sql = "select * from templates where name = "&tplname&""

rs.open sql,conn,1,1

tmp = rs("content")

rs.close

setblk blkname, tmp

set rs = nothing

end sub

从文件取出

public sub setblkfile(blkname, tplfile)

dim fso, ofile

set fso = createobject("scripting.filesystemobject")

if fso.fileexists(server.mappath(tpl_dir &tplfile)) then

set ofile = fso.opentextfile(server.mappath(tpl_dir &tplfile))

tmp = ofile.readal

setblock blkname, tmp

ofile.close

set ofile = nothing

else

response.write "区块模板文件不存在!<br>"

end if

set fso = nothing

end sub

设置变量替换值

========================

简单替换

public sub setvar(sname, svalue)

if var_list.exists(sname) then

var_list.remove sname

var_list.add sname, svalue

else

var_list.add sname, svalue

end if

end sub

简单替换 追加数据

public sub appendvar(sname, svalue)

if var_list.exists(sname) then

tmp = var_list.item(sname) & svalue

var_list.remove sname

var_list.add sname, tmp

else

var_list.add sname, svalue

end if

end sub

循环替换

========================

一维循环开始

public sub udblk(blkname)

tpl_blk = blkname

re.ignorecase = true

re.global = true

re.pattern = "<!–\s+begin\s+(" & blkname & ")\s+–>([\s\s.]*)<!–\s+end\s+\1\s+–>"

set matches = re.execute(tpl)

if matches.count > 0 then

set match = matches

for each match in matches

blk_list.add blkname, match.submatches(1)

var_list.add blkname, ""

tpl = re.replace(tpl, "{{"&blkname&"}}")

next

else

response.write "block " & blkname & " does not exists!"

end if

end sub

一维循环结束

public sub psblk(blkname)

tmp = blk_list.item(blkname)

re.ignorecase = true

re.global = true

re.pattern = "(" & p_var_tag_o & ")([^}]+)" & p_var_tag_c

set matches = re.execute(tmp)

for each match in matches

if blk_var_list.exists(match.submatches(1)) then

re.pattern = match.value

tmp = re.replace(tmp, blk_var_list.item(match.submatches(1)))

end if

next

tmp = var_list.item(blkname) & tmp

var_list.remove blkname

var_list.add blkname, tmp

blk_var_list.removeall

end sub

二维循环开始

public sub udblk2(blkname)

tmp = blk_list.item(tpl_blk)

re.ignorecase = true

re.global = true

re.pattern = "<!–\s+begin\s+(" & blkname & ")\s+–>([\s\s.]*)<!–\s+end\s+\1\s+–>"

set matches = re.execute(tmp)

if matches.count > 0 then

set match = matches

for each match in matches

blk_list.add blkname, match.submatches(1)

response.write match.submatches(1)

blk_var_list.add blkname, ""

tmp = re.replace(tmp, "{{"&blkname&"}}")

blk_list.remove tpl_blk

blk_list.add tpl_blk, tmp

next

else

response.write "block " & blkname & " does not exists!"

end if

end sub

二维循环结束

public sub psblk2(blkname)

tmp = blk_list.item(blkname)

re.ignorecase = true

re.global = true

re.pattern = "(" & p_var_tag_o & ")([^}]+)" & p_var_tag_c

set matches = re.execute(tmp)

for each match in matches

if blk_var_list.exists(match.submatches(1)) then

re.pattern = match.value

tmp = re.replace(tmp, blk_var_list.item(match.submatches(1)))

end if

next

tmp = blk_var_list.item(blkname) & tmp

blk_var_list.removeall

blk_var_list.add blkname, tmp

end sub

循环中的替换

public sub setblkvar(s, v)

if blk_var_list.exists(s) then

blk_var_list.remove s

blk_var_list.add s, v

else

blk_var_list.add s, v

end if

end sub

解析模板和输出内容

========================

执行解析

可以通过下面的过程取得网页内容,结合fso可以生成静态页面

public property gettpl

re.ignorecase = true

re.global = true

re.pattern = "(" & {{ & ")([^}]+)" & }}

set matches = re.execute(tpl)

for each match in matches

if var_list.exists(match.submatches(1)) then

re.pattern = match.value

tpl = re.replace(tpl, var_list.item(match.submatches(1)))

end if

next

gettpl = tpl

end property

输出内容

public sub parse

response.write tpl

end sub

end class

%><%

=========================================================

file: class_template.asp

version:1.0

date: 2004-5-7

script written by r.h

description: asp template class

=========================================================

copyright (c) 2004 interflower studios. all rights reserved.

web: http://www.interflower.cn

need help? contact: ranhuan@msn.com

=========================================================

=========================================================

模板中替换的部分用{{%}}表示

模板中的循环用<!– begin % –>开始 <!– end % –>结束 支持一次嵌套

class template

private tmp

private tpl_dir, tpl, tpl_blk

private var_list, blk_list, blk_var_list

private re, match, matchs

private sub class_initialize

sql = ""

tpl_dir = "templates/"

tpl = ""

blk=""

set var_list = server.createobject("scripting.dictionary")

set blk_list = server.createobject("scripting.dictionary")

set blk_var_list = server.createobject("scripting.dictionary")

set re = new regexp

end sub

取得主体模板

========================

从变量取出

public sub settpl(tplvar)

tpl = tplvar

end sub

从db中取出,自己修改sql语句

public sub settpldb(tplname)

dim sql, rs

set rs = server.createobject("adodb.recordset")

sql = "select content from templates where name = "&tplname&""

rs.open sql,conn,1,1

if rs.recordcount <> 1 then

response.write("数据库错误!<br>")

response.end()

end if

tpl = rs("content")

rs.close

set rs = nothing

end sub

从文件取出

public sub settplfile(tplfile)

dim fso, ofile

set fso = server.createobject("scripting.filesystemobject")

if fso.fileexists(server.mappath(tpl_dir & tplfile)) then

set ofile = fso.opentextfile(server.mappath(tpl_dir & tplfile))

tpl = ofile.readall

ofile.close

set ofile = nothing

else

response.write "模板文件不存在!<br>"

end if

set fso = nothing

end sub

取得区块模板

========================

从变量取出

public sub setblk(blkname, tplvar)

re.ignorecase = true

re.global = true

re.pattern = {{ & blkname & }}

tpl = re.replace(tpl, tplvar)

rs.close

end sub

从数据库取出

public sub setblkdb(blkname, tplname)

dim sql, rs

set rs = server.createobject("adodb.recordset")

sql = "select * from templates where name = "&tplname&""

rs.open sql,conn,1,1

tmp = rs("content")

rs.close

setblk blkname, tmp

set rs = nothing

end sub

从文件取出

public sub setblkfile(blkname, tplfile)

dim fso, ofile

set fso = createobject("scripting.filesystemobject")

if fso.fileexists(server.mappath(tpl_dir &tplfile)) then

set ofile = fso.opentextfile(server.mappath(tpl_dir &tplfile))

tmp = ofile.readal

setblock blkname, tmp

ofile.close

set ofile = nothing

else

response.write "区块模板文件不存在!<br>"

end if

set fso = nothing

end sub

设置变量替换值

========================

简单替换

public sub setvar(sname, svalue)

if var_list.exists(sname) then

var_list.remove sname

var_list.add sname, svalue

else

var_list.add sname, svalue

end if

end sub

简单替换 追加数据

public sub appendvar(sname, svalue)

if var_list.exists(sname) then

tmp = var_list.item(sname) & svalue

var_list.remove sname

var_list.add sname, tmp

else

var_list.add sname, svalue

end if

end sub

循环替换

========================

一维循环开始

public sub udblk(blkname)

tpl_blk = blkname

re.ignorecase = true

re.global = true

re.pattern = "<!–\s+begin\s+(" & blkname & ")\s+–>([\s\s.]*)<!–\s+end\s+\1\s+–>"

set matches = re.execute(tpl)

if matches.count > 0 then

set match = matches

for each match in matches

blk_list.add blkname, match.submatches(1)

var_list.add blkname, ""

tpl = re.replace(tpl, "{{"&blkname&"}}")

next

else

response.write "block " & blkname & " does not exists!"

end if

end sub

一维循环结束

public sub psblk(blkname)

tmp = blk_list.item(blkname)

re.ignorecase = true

re.global = true

re.pattern = "(" & p_var_tag_o & ")([^}]+)" & p_var_tag_c

set matches = re.execute(tmp)

for each match in matches

if blk_var_list.exists(match.submatches(1)) then

re.pattern = match.value

tmp = re.replace(tmp, blk_var_list.item(match.submatches(1)))

end if

next

tmp = var_list.item(blkname) & tmp

var_list.remove blkname

var_list.add blkname, tmp

blk_var_list.removeall

end sub

二维循环开始

public sub udblk2(blkname)

tmp = blk_list.item(tpl_blk)

re.ignorecase = true

re.global = true

re.pattern = "<!–\s+begin\s+(" & blkname & ")\s+–>([\s\s.]*)<!–\s+end\s+\1\s+–>"

set matches = re.execute(tmp)

if matches.count > 0 then

set match = matches

for each match in matches

blk_list.add blkname, match.submatches(1)

response.write match.submatches(1)

blk_var_list.add blkname, ""

tmp = re.replace(tmp, "{{"&blkname&"}}")

blk_list.remove tpl_blk

blk_list.add tpl_blk, tmp

next

else

response.write "block " & blkname & " does not exists!"

end if

end sub

二维循环结束

public sub psblk2(blkname)

tmp = blk_list.item(blkname)

re.ignorecase = true

re.global = true

re.pattern = "(" & p_var_tag_o & ")([^}]+)" & p_var_tag_c

set matches = re.execute(tmp)

for each match in matches

if blk_var_list.exists(match.submatches(1)) then

re.pattern = match.value

tmp = re.replace(tmp, blk_var_list.item(match.submatches(1)))

end if

next

tmp = blk_var_list.item(blkname) & tmp

blk_var_list.removeall

blk_var_list.add blkname, tmp

end sub

循环中的替换

public sub setblkvar(s, v)

if blk_var_list.exists(s) then

blk_var_list.remove s

blk_var_list.add s, v

else

blk_var_list.add s, v

end if

end sub

解析模板和输出内容

========================

执行解析

可以通过下面的过程取得网页内容,结合fso可以生成静态页面

public property gettpl

re.ignorecase = true

re.global = true

re.pattern = "(" & {{ & ")([^}]+)" & }}

set matches = re.execute(tpl)

for each match in matches

if var_list.exists(match.submatches(1)) then

re.pattern = match.value

tpl = re.replace(tpl, var_list.item(match.submatches(1)))

end if

next

gettpl = tpl

end property

输出内容

public sub parse

response.write tpl

end sub

end class

%><%

=========================================================

file: class_template.asp

version:1.0

date: 2004-5-7

script written by r.h

description: asp template class

=========================================================

copyright (c) 2004 interflower studios. all rights reserved.

web: http://www.interflower.cn

need help? contact: ranhuan@msn.com

=========================================================

=========================================================

模板中替换的部分用{{%}}表示

模板中的循环用<!– begin % –>开始 <!– end % –>结束 支持一次嵌套

class template

private tmp

private tpl_dir, tpl, tpl_blk

private var_list, blk_list, blk_var_list

private re, match, matchs

private sub class_initialize

sql = ""

tpl_dir = "templates/"

tpl = ""

blk=""

set var_list = server.createobject("scripting.dictionary")

set blk_list = server.createobject("scripting.dictionary")

set blk_var_list = server.createobject("scripting.dictionary")

set re = new regexp

end sub

取得主体模板

========================

从变量取出

public sub settpl(tplvar)

tpl = tplvar

end sub

从db中取出,自己修改sql语句

public sub settpldb(tplname)

dim sql, rs

set rs = server.createobject("adodb.recordset")

sql = "select content from templates where name = "&tplname&""

rs.open sql,conn,1,1

if rs.recordcount <> 1 then

response.write("数据库错误!<br>")

response.end()

end if

tpl = rs("content")

rs.close

set rs = nothing

end sub

从文件取出

public sub settplfile(tplfile)

dim fso, ofile

set fso = server.createobject("scripting.filesystemobject")

if fso.fileexists(server.mappath(tpl_dir & tplfile)) then

set ofile = fso.opentextfile(server.mappath(tpl_dir & tplfile))

tpl = ofile.readall

ofile.close

set ofile = nothing

else

response.write "模板文件不存在!<br>"

end if

set fso = nothing

end sub

取得区块模板

========================

从变量取出

public sub setblk(blkname, tplvar)

re.ignorecase = true

re.global = true

re.pattern = {{ & blkname & }}

tpl = re.replace(tpl, tplvar)

rs.close

end sub

从数据库取出

public sub setblkdb(blkname, tplname)

dim sql, rs

set rs = server.createobject("adodb.recordset")

sql = "select * from templates where name = "&tplname&""

rs.open sql,conn,1,1

tmp = rs("content")

rs.close

setblk blkname, tmp

set rs = nothing

end sub

从文件取出

public sub setblkfile(blkname, tplfile)

dim fso, ofile

set fso = createobject("scripting.filesystemobject")

if fso.fileexists(server.mappath(tpl_dir &tplfile)) then

set ofile = fso.opentextfile(server.mappath(tpl_dir &tplfile))

tmp = ofile.readal

setblock blkname, tmp

ofile.close

set ofile = nothing

else

response.write "区块模板文件不存在!<br>"

end if

set fso = nothing

end sub

设置变量替换值

========================

简单替换

public sub setvar(sname, svalue)

if var_list.exists(sname) then

var_list.remove sname

var_list.add sname, svalue

else

var_list.add sname, svalue

end if

end sub

简单替换 追加数据

public sub appendvar(sname, svalue)

if var_list.exists(sname) then

tmp = var_list.item(sname) & svalue

var_list.remove sname

var_list.add sname, tmp

else

var_list.add sname, svalue

end if

end sub

循环替换

========================

一维循环开始

public sub udblk(blkname)

tpl_blk = blkname

re.ignorecase = true

re.global = true

re.pattern = "<!–\s+begin\s+(" & blkname & ")\s+–>([\s\s.]*)<!–\s+end\s+\1\s+–>"

set matches = re.execute(tpl)

if matches.count > 0 then

set match = matches

for each match in matches

blk_list.add blkname, match.submatches(1)

var_list.add blkname, ""

tpl = re.replace(tpl, "{{"&blkname&"}}")

next

else

response.write "block " & blkname & " does not exists!"

end if

end sub

一维循环结束

public sub psblk(blkname)

tmp = blk_list.item(blkname)

re.ignorecase = true

re.global = true

re.pattern = "(" & p_var_tag_o & ")([^}]+)" & p_var_tag_c

set matches = re.execute(tmp)

for each match in matches

if blk_var_list.exists(match.submatches(1)) then

re.pattern = match.value

tmp = re.replace(tmp, blk_var_list.item(match.submatches(1)))

end if

next

tmp = var_list.item(blkname) & tmp

var_list.remove blkname

var_list.add blkname, tmp

blk_var_list.removeall

end sub

二维循环开始

public sub udblk2(blkname)

tmp = blk_list.item(tpl_blk)

re.ignorecase = true

re.global = true

re.pattern = "<!–\s+begin\s+(" & blkname & ")\s+–>([\s\s.]*)<!–\s+end\s+\1\s+–>"

set matches = re.execute(tmp)

if matches.count > 0 then

set match = matches

for each match in matches

blk_list.add blkname, match.submatches(1)

response.write match.submatches(1)

blk_var_list.add blkname, ""

tmp = re.replace(tmp, "{{"&blkname&"}}")

blk_list.remove tpl_blk

blk_list.add tpl_blk, tmp

next

else

response.write "block " & blkname & " does not exists!"

end if

end sub

二维循环结束

public sub psblk2(blkname)

tmp = blk_list.item(blkname)

re.ignorecase = true

re.global = true

re.pattern = "(" & p_var_tag_o & ")([^}]+)" & p_var_tag_c

set matches = re.execute(tmp)

for each match in matches

if blk_var_list.exists(match.submatches(1)) then

re.pattern = match.value

tmp = re.replace(tmp, blk_var_list.item(match.submatches(1)))

end if

next

tmp = blk_var_list.item(blkname) & tmp

blk_var_list.removeall

blk_var_list.add blkname, tmp

end sub

循环中的替换

public sub setblkvar(s, v)

if blk_var_list.exists(s) then

blk_var_list.remove s

blk_var_list.add s, v

else

blk_var_list.add s, v

end if

end sub

解析模板和输出内容

========================

执行解析

可以通过下面的过程取得网页内容,结合fso可以生成静态页面

public property gettpl

re.ignorecase = true

re.global = true

re.pattern = "(" & {{ & ")([^}]+)" & }}

set matches = re.execute(tpl)

for each match in matches

if var_list.exists(match.submatches(1)) then

re.pattern = match.value

tpl = re.replace(tpl, var_list.item(match.submatches(1)))

end if

next

gettpl = tpl

end property

输出内容

public sub parse

response.write tpl

end sub

end class

%>

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » ASP模板类[实现一维循环和二维循环,可以从文件、数据库、变量取摸板]-ASP教程,ASP应用
分享到: 更多 (0)