当前位置: 移动技术网 > IT编程>开发语言>Asp > ASP Crazy 模版操作类(最简单的模板类、仅提供交流)

ASP Crazy 模版操作类(最简单的模板类、仅提供交流)

2017年12月12日  | 移动技术网IT编程  | 我要评论
文件名:awa_temp.class.asp
复制代码 代码如下:

<%
'crazy蛙!模板操作类

'作者crazy~蛙! qq:379969387 欢迎交流
'版本v1.0;
class awatemp
public aa
private fso,strtemp,filedata,getdatas,strhtmlname,htmlwrite,strlabel,strlvalues
'===========================================
'构造函数_初始化fso组件
'===========================================
private sub class_initialize()
set fso=server.createobject("scripting.filesystemobject")
end sub
'===========================================
'构析函数_销毁fso组件
'===========================================
private sub class_terminate()
set fso=nothing
end sub
'===========================================
'类属性
'===========================================
'版本信息
public property get version
version="crazy~蛙! 模板操作类!v1.0版本;"
end property
'获取模板地址以及名称
public property let temp(byval values)
strtemp=values
end property
'获取生成文件的文件名
public property let htmlname(byval values)
strhtmlname=values
end property
'获取标签
public property let label(byval values)
strlabel=values
end property
'获取将标签替换的值
public property let lvalues(byval values)
strlvalues=values
end property
'===========================================
'类方法
'===========================================
'检查模板设置以及是否存在
private function check()
if strtemp="" then
check="<span style='color:red;'>错误:未设置模板文件存储位置!</span>"
else
if fso.fileexists(strtemp)=false then
check="<span style='color:red;'>错误:指定模板不存在!</span>"
else
check=true
end if
end if
end function
'读取模板页
public sub readtemp()
if check()<>true then
response.write check()
response.end()
else
set getdatas=fso.opentextfile(server.mappath(strtemp))
filedata=getdatas.readall
getdatas.close
set getdata=nothing
end if
end sub
'替换内容
public function rep()
if strlabel="" then
response.write "<span style='color:red;'>错误:未设置欲替换的标签!</span>"
response.end()
end if
if strlvalues="" then
response.write "<span style='color:red;'>错误:未设置替换标签的数据!</span>"
response.end()
end if
filedata=replace(filedata,strlabel,strlvalues)
end function
'输出
public sub echo()
response.write filedata
end sub
'生成静态页面
public sub gethtml()
if strhtmlname="" then
response.write "<span style='color:red;'>错误:未设置生成html文件存储位置以及文件名称!</span>"
response.end()
end if
set htmlwrite=fso.createtextfile(server.mappath(strhtmlname),true)
'写入网页内容
htmlwrite.writeline filedata
htmlwrite.close
set htmlwrite=nothing
end sub
end class
%>

用法示例:

index.asp
复制代码 代码如下:

<!--#include file="awa_temp.class.asp"-->
<%
set awa=new awatemp
with awa
.temp="temp.tpl"
.readtemp
.label="{tl1}"
.lvalues="蛋疼"
.rep
.echo
'.htmlname="aa/"
'.gethtml
end with
set awa=nothing
%>

模版

temp.tpl:
复制代码 代码如下:

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>

<body>
<table width="200" border="1">
<tr>
<td>{tl1}</td>
<td>{tl2}</td>
<td>{tl3}</td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
</tr>
</table>
</body>
</html>

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网