欢迎光临
我们一直在努力

ASP通用模板类-ASP教程,ASP技巧

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

特性
可设定私有缓存或公共缓存,提高效率
可自由选择使用 stream 组件或 fso 组件
支持自定义文件编码
可保存文件

属性

name
文本,该模板名称,主要用于使用公共缓存时区分不同模板。

format
文本,文件编码类型,可设置值。

object
文本,使用组件,可设置值:

stream
fso

 

publiccache
布尔值,使用公共缓存,开启时模板文件将保存到application对象,其他引用此模板类的对象设置相同name值并同样打开公共缓存即可从缓存读取。(load方法)

privatecache
布尔值,使用私有缓存,开启时模板文件将保存到对象内部变量,同一引用此模板类的对象可读取。(load方法)

direction
文本,模板文件所在目录,前后无需斜杠或反斜杠,如:template/default

file
文本,模板文件名,前边无需斜杠或反斜杠,如:default.html

savedirection
文本,保存文件所在目录,前后无需斜杠或反斜杠,如:html/default

savefile
文本,保存文件名,前边无需斜杠或反斜杠,如:default.html

 

对象

code
文本,当前文本,使用setvar方法时对此对象进行替换,使用load方法时将模板重载到此对象

storage
文本,已保存文本,使用savefront或savelast方法时将code对象中文本保存到此对象的开头或结尾,可用于循环后得到所有代码

 

方法

clearcache
清除公共缓存和私有缓存(强制从文件重载模板)

clearpubliccache
清除公共缓存

clearprivatecache
清除私有缓存

clearcode
清除code对象

clearstorage
清除storage对象

savefront
将当前code对象中文本保存到storage对象开头

savelast
将当前code对象中文本保存到storage对象结尾

savecode
将当前code对象中文本保存到文件

savestorage
将当前storage对象中文本保存到文件

setvar
对当前code对象中文本进行替换
参数:需要被替换的文本,欲替换后的文本

load
将模板文件载入code对象,当开启并存在私有缓存时,从私有缓存载入,当开启并存在公共缓存时,从公共缓存载入,若无缓存则从文件载入

 

内部变量

ccstrpath
默认根目录

ccstrcookiename
默认application对象名前缀

 

代码

class ccclstemplate

  private ccstrcode,ccstrstorage
  private ccstrcachecode
  private ccblnpubliccache,ccblnprivatecache
  private ccstrname,ccstrcookiename
  private ccstrdirection,ccstrsavedirection,ccstrfile,ccstrsavefile,ccstrpath
  private ccobjstream,ccobjfso,ccstrformat,ccintobject,ccobjtext,ccintformat

  private sub class_initialize
    ccstrname = “default”    默认名称
    ccblnpubliccache = false
    ccblnprivatecache = false
    ccstrfile = “cache.html”
    ccstrsavefile = “save_cache.html”
    ccstrcookiename = “ccclass_template”  application对象名前缀
    ccstrformat = “utf-8”    utf-8|ascii|gb2312|big5
    ccintformat = -1
    ccintobject = 1        默认读取/保存模板组件 1:adodb.stream 2:fso
    ccstrpath = server.mappath(“./”)&”\”  默认根路径
  end sub

  public property let name(ccstrname_in)
    ccstrname = lcase(trim(ccstrname_in))
  end property

  public property let format(ccstrformat_in)
    ccstrformat = ccstrformat_in
    if instr(lcase(trim(ccstrformat_in)),”utf”) > 0 then
      ccintformat = -1
    else
      ccintformat = 0
    end if
  end property

  public property let object(ccstrobject_in)
    ccstrobject_in = lcase(trim(ccstrobject_in))
    if instr(ccstrobject_in,”fso”) > 0 then
      ccintobject = 2
    else
      ccintobject = 1
    end if
  end property

  public property let publiccache(ccblnpubliccache_in)
    if ccblnpubliccache_in = true then
      ccblnpubliccache = true
    else
      ccblnpubliccache = false
    end if
  end property

  public property let privatecache(ccblnprivatecache_in)
    if ccblnprivatecache_in = true then
      ccblnprivatecache = true
    else
      ccblnprivatecache = false
    end if
  end property

  public property let direction(ccstrdirection_in)
    ccstrdirection = ccstrdirection_in
  end property

  public property let file(ccstrfile_in)
    if ccstrfile_in <> “” then
      ccstrfile = ccstrfile_in
    end if
  end property

  public property let savedirection(ccstrsavedirection_in)
    ccstrsavedirection = ccstrsavedirection_in
  end property

  public property let savefile(ccstrsavefile_in)
    if ccstrsavefile_in <> “” then
      ccstrsavefile = ccstrsavefile_in
    end if
  end property

  public property get code
    code = ccstrcode
  end property

  public property get storage
    storage = ccstrstorage
  end property

  public sub clearcache
    call clearprivatecache
    call clearpubliccache
  end sub

  public sub clearprivatecache
    ccstrcachecode = “”
  end sub

  public sub clearpubliccache
    application(ccstrcookiename&ccstrname) = “”
  end sub

  public sub clearstorage
    ccstrstorage = “”
  end sub

  public sub clearcode
    ccstrcode = “”
  end sub

  public sub savefront
    ccstrstorage = ccstrcode & ccstrstorage
  end sub

  public sub savelast
    ccstrstorage = ccstrstorage & ccstrcode
  end sub

  public sub savecode
    call savetofile(1)
  end sub

  public sub savestorage
    call savetofile(2)
  end sub

  public sub setvar(ccstrtag_in,ccstrvalue_in)
    ccstrcode = replace(ccstrcode,ccstrtag_in,ccstrvalue_in)
  end sub

  private sub savetofile(ccintcode_in)
    dim ccstrsavecode
    if ccintcode_in = 1 then
      ccstrsavecode = ccstrcode
    else
      ccstrsavecode = ccstrstorage
    end if
    if ccintobject = 1 then
      set ccobjstream = server.createobject(“adodb.stream”)
      with ccobjstream
        .type = 2
        .mode = 3
        .open
        .charset = ccstrformat
        .position = ccobjstream.size
        .writetext ccstrsavecode
        .savetofile ccstrpath & ccstrsavedirection & “\” & ccstrsavefile,2
        .close
      end with
      set ccobjstream = nothing
    else
      set ccobjfso = createobject(“scripting.filesystemobject”)
      if ccobjfso.fileexists(ccstrpath & ccstrsavedirection & “\” & ccstrsavefile) = true then
        ccobjfso.deletefile(ccstrpath & ccstrsavedirection & “\” & ccstrsavefile)
      end if
      set ccobjtext = ccobjfso.opentextfile(ccstrpath & ccstrsavedirection & “\” & ccstrsavefile,2,true,ccintformat)
      ccobjtext.write ccstrsavecode
      set ccobjtext = nothing
      set ccobjfso = nothing
    end if
    ccstrsavecode = “”
  end sub

  public sub load
    ccstrcode = “”
    if ccblnprivatecache = true then
      if ccfncisempty(ccstrcachecode) = false then
        ccstrcode = ccstrcachecode
        exit sub
      end if
    end if
    if ccblnpubliccache = true then
      if ccfncisempty(application(ccstrcookiename&ccstrname)) = false then
        ccstrcode = application(ccstrcookiename&ccstrname)
        exit sub
      end if
    end if
    if ccintobject = 1 then
      set ccobjstream = server.createobject(“adodb.stream”)
      with ccobjstream
        .type = 2
        .mode = 3
        .open
        .charset = ccstrformat
        .position = ccobjstream.size
        .loadfromfile ccstrpath & ccstrdirection & “\” & ccstrfile
        ccstrcode = .readtext
        .close
      end with
      set ccobjstream = nothing
    else
      set ccobjfso = createobject(“scripting.filesystemobject”)
      if ccobjfso.fileexists(ccstrpath & ccstrdirection & “\” & ccstrfile) = true then
        set ccobjtext = ccobjfso.opentextfile(ccstrpath & ccstrdirection & “\” & ccstrfile,1,false,ccintformat)
        ccstrcode = ccobjtext.readall
        set ccobjtext = nothing
      end if
      set ccobjfso = nothing
    end if
    if ccblnprivatecache = true then
      ccstrcachecode = ccstrcode
    end if
    if ccblnpubliccache = true then
      application(ccstrcookiename&ccstrname) = ccstrcode
    end if
end sub

end class

function ccfncisempty(byref ccstrvalue_in)
  if isnull(ccstrvalue_in) or isempty(ccstrvalue_in) or ccstrvalue_in = “” then
    ccfncisempty = true
  else
    ccfncisempty = false
  end if
end function

 

实例

模板文件内容

 

<#test#>

asp程序代码

dim objtemplate
set objtemplate = new ccclstemplate
objtemplate.name = “test”
objtemplate.format = “utf-8”
开启缓存
objtemplate.publiccache = true
objtemplate.privatecache = true
设置模板目录和文件名
objtemplate.direction = “test”
objtemplate.file = “test.html”
设置保存文件目录和文件名
objtemplate.savedirection = “test”
objtemplate.savefile = “test3.html”
载入模板
call objtemplate.load
进行文本替换
call objtemplate.setvar(“<#test#>”,”hello world.”)
将文本保存至storage暂存
call objtemplate.savelast
重新载入模板,此时将从私有缓存重新装载,提高效率
call objtemplate.load
替换为其他值
call objtemplate.setvar(“<#test#>”,” by cloudream.”)
保存至storage结尾暂存
call objtemplate.savelast
保存code至文件
call objtemplate.savecode
response.write objtemplate.storage

set objtemplate = nothing

显示结果

hello world. by cloudream.

保存文件结果

by cloudream.

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » ASP通用模板类-ASP教程,ASP技巧
分享到: 更多 (0)