ASP 根据模板生成HTML静态文件类

 

ASP 根据模板生成HTML静态文件类

 

 

 

<%

'使用范例:

'    dim tpl

'    set tpl = New template

'    tpl.setTplPath = "../template/footer.tpl"

'    tpl.setTags = array("<!--{标签名称}-->")

'    tpl.setNotes = array("替换内容")

'    tpl.setStoreF = "../html/"            '使用自动生成文件名时请在未尾加"/"

'    tpl.exeTpl

'    Set tpl = Nothing

'----------------------------------------------------------

Class template

    Private objFso

    Private strFileCode            ' 读取模板文件的内容

    Private intRnd                ' 生成随机数,组合成新文件名

    Private tplpath                ' 模板名称

    Private StorePath            ' 生成文件存放目录

    Private StoreFname            ' 生成文件名称

    Private arrTags                ' 标签数组

    Private arrNotes            ' 内容

  

    ' 在使用 Set 创建对象时自动执行

    Private Sub Class_Initialize()

        set objFso = CreateObject("scripting.filesystemobject")

    End Sub

 

    ' 模板文件位置+名称   只写

    Public Property Let setTplPath(ByVal tName)

        tplpath = Server.MapPath(Replace(tName,"//","/"))

    End Property

    ' 存储文件位置+名称   只写

    Public Property Let setStoreF(ByVal tName)

        tName = Replace(tName,"//","/")

        ' 取文件名

        dim arrY

        arrY = split(tName,"/")

        StoreFname = Ubound(arrY)

        StoreFname = arrY(StoreFname)

        ' 取目录名

        StorePath = Replace(tName,StoreFname,"")

        StorePath = Server.MapPath(StorePath)

    End Property

    ' 标签   只写

    Public Property Let setTags(ByVal strString)

        arrTags = strString

    End Property

    ' 内容   只写

    Public Property Let setNotes(ByVal strString)

        arrNotes = strString

    End Property

 

    ' 生成在Min 与 Max之间取随机数

    Private Sub rnd_Integer(Min,Max)

        Randomize

        intRnd = CInt((Max-Min+1)*Rnd()+Min)

    End Sub

    ' 生成文件名:时间+随机数

    Private Sub makeFilename

        dim fname

        fname = now()

        fname = replace(fname,"-","")

        fname = replace(fname," ","")

        fname = replace(fname,":","")

        fname = replace(fname,"PM","")

        fname = replace(fname,"AM","")

        fname = replace(fname,"上午","")

        fname = replace(fname,"下午","")

        call rnd_Integer(0,9999)

        StoreFname = fname&intRnd&".html"

        fname = null

    End Sub

    ' 读取模板文件内容

    Private Sub LoadTemplate

        If objFso.FileExists(tplpath) Then

            Set FileObj = objFso.GetFile(tplpath)

            Set FileStreamObj = FileObj.OpenAsTextStream(1)

            If Not FileStreamObj.AtEndOfStream Then

                strFileCode = FileStreamObj.ReadAll

            Else

                Message 1002

            End If

        Else

            Message 1001

        End If

        Set FileObj = Nothing:Set FileStreamObj = Nothing

    End Sub

    ' 更新页面内容

    Private Sub updateNotes

        if IsArray(arrNotes) and IsArray(arrTags) then

            if Ubound(arrNotes) = Ubound(arrTags) then

                LoadTemplate

                For intI = Lbound(arrNotes) to Ubound(arrNotes)

                    strFileCode = Replace(strFileCode,arrTags(intI),arrNotes(intI))

                Next

            else

                Message 1004

            end if

        else

            Message 1004

        end if

    End Sub

    ' 创建文件夹

    Private Sub makeFold(ByVal strFoladName)

        on error resume next

        '判断文件夹是否存在,(about files:objFso.FileExists)

        If false = objFso.FolderExists(strFoladName) Then

            objFso.CreateFolder(strFoladName)

            if err>0 then

                err.clear

                Message 1003

            end if

        End If

    End Sub

    ' 创建文件

    Private Sub makeFile

        dim arrX,intI,strW

        strW = ""

        arrX = split(StorePath,"/")

        ' 判断目录是否存在,决定是否创建目录

        For intI = Lbound(arrX) to Ubound(arrX)

            makeFold strW&arrX(intI)

            strW = strW&arrX(intI)

        Next

        ' 未设置文件名则使用日期加随机数作为文件名

        if StoreFname = "" then

            makeFilename

        end if

        ' 更新内容

        updateNotes

        ' 生成文件 object.CreateTextFile(filename[, overwrite[, unicode]])

        Set objFout = objFso.CreateTextFile(StorePath&"/"&StoreFname,true)

        ' 输入内容

        objFout.WriteLine strFileCode

        objFout.close

        set objFso = Nothing

        ' 提示操作成功

        Message 1000

    End Sub

    ' 执行

    public Sub exeTpl

        makeFile

    End Sub

    ' 返回

    Private Sub Message(ByVal s)

        Select Case s

        Case 1000

            Response.Write "操作成功!"&StoreFname

        Case 1001

            Response.Write "模板不存在,请先绑定!"

        Case 1002

            Response.Write "模板内容为空!"

        Case 1003

            Response.Write "文件目录创建失败!"

        Case Else

        End Select

    End Sub

    '在使用 Set 释放对象时自动执行

    Private Sub Class_Terminate

        Set objFso = Nothing

    End Sub 

End Class

%>

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值