最新更新 sitemap 网站制作设计本站搜索
网页设计
国外网站 韩国网站 个人主页 手提袋设计 CSS 网页特效 平面设计 网站设计 Flash CMS技巧 服装网站 php教程 photoshop 画册 服务器选用 数据库 Office
虚拟主机 域名注册 云主机 网页设计 客服QQ:8208442
当前位置:首页 > 编程开发 > asp教程

ASP通用模板类ASP技巧

日期:11-04    来源:网页设计秀    作者:cnwebshow.com

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

属性9Pc中国设计秀

9Pc中国设计秀
Name9Pc中国设计秀
文本,该模板名称,主要用于使用公共缓存时区分不同模板。9Pc中国设计秀

9Pc中国设计秀
Format9Pc中国设计秀
文本,文件编码类型,可设置值。9Pc中国设计秀

9Pc中国设计秀
Object9Pc中国设计秀
文本,使用组件,可设置值:9Pc中国设计秀

9Pc中国设计秀
Stream9Pc中国设计秀
FSO9Pc中国设计秀

 9Pc中国设计秀

PublicCache9Pc中国设计秀
布尔值,使用公共缓存,开启时模板文件将保存到application对象,其他引用此模板类的对象设置相同Name值并同样打开公共缓存即可从缓存读取。(Load方法)9Pc中国设计秀

9Pc中国设计秀
PRivateCache9Pc中国设计秀
布尔值,使用私有缓存,开启时模板文件将保存到对象内部变量,同一引用此模板类的对象可读取。(Load方法)9Pc中国设计秀

9Pc中国设计秀
Direction9Pc中国设计秀
文本,模板文件所在目录,前后无需斜杠或反斜杠,如:template/default9Pc中国设计秀

9Pc中国设计秀
File9Pc中国设计秀
文本,模板文件名,前边无需斜杠或反斜杠,如:default.html9Pc中国设计秀

9Pc中国设计秀
SaveDirection9Pc中国设计秀
文本,保存文件所在目录,前后无需斜杠或反斜杠,如:html/default9Pc中国设计秀

9Pc中国设计秀
SaveFile9Pc中国设计秀
文本,保存文件名,前边无需斜杠或反斜杠,如:default.html9Pc中国设计秀

 9Pc中国设计秀

对象9Pc中国设计秀

9Pc中国设计秀
Code9Pc中国设计秀
文本,当前文本,使用SetVar方法时对此对象进行替换,使用Load方法时将模板重载到此对象9Pc中国设计秀

9Pc中国设计秀
Storage9Pc中国设计秀
文本,已保存文本,使用SaveFront或SaveLast方法时将Code对象中文本保存到此对象的开头或结尾,可用于循环后得到所有代码9Pc中国设计秀

 9Pc中国设计秀

方法9Pc中国设计秀

9Pc中国设计秀
ClearCache9Pc中国设计秀
清除公共缓存和私有缓存(强制从文件重载模板)9Pc中国设计秀

9Pc中国设计秀
ClearPublicCache9Pc中国设计秀
清除公共缓存9Pc中国设计秀

9Pc中国设计秀
ClearPrivateCache9Pc中国设计秀
清除私有缓存9Pc中国设计秀

9Pc中国设计秀
ClearCode9Pc中国设计秀
清除Code对象9Pc中国设计秀

9Pc中国设计秀
ClearStorage9Pc中国设计秀
清除Storage对象9Pc中国设计秀

9Pc中国设计秀
SaveFront9Pc中国设计秀
将当前Code对象中文本保存到Storage对象开头9Pc中国设计秀

9Pc中国设计秀
SaveLast9Pc中国设计秀
将当前Code对象中文本保存到Storage对象结尾9Pc中国设计秀

9Pc中国设计秀
SaveCode9Pc中国设计秀
将当前Code对象中文本保存到文件9Pc中国设计秀

9Pc中国设计秀
SaveStorage9Pc中国设计秀
将当前Storage对象中文本保存到文件9Pc中国设计秀

9Pc中国设计秀
SetVar9Pc中国设计秀
对当前Code对象中文本进行替换9Pc中国设计秀
参数:需要被替换的文本,欲替换后的文本9Pc中国设计秀

9Pc中国设计秀
Load9Pc中国设计秀
将模板文件载入Code对象,当开启并存在私有缓存时,从私有缓存载入,当开启并存在公共缓存时,从公共缓存载入,若无缓存则从文件载入9Pc中国设计秀

 9Pc中国设计秀

内部变量9Pc中国设计秀

9Pc中国设计秀
ccStrPath9Pc中国设计秀
默认根目录9Pc中国设计秀

9Pc中国设计秀
ccStrCookieName9Pc中国设计秀
默认Application对象名前缀9Pc中国设计秀

 9Pc中国设计秀

代码9Pc中国设计秀

9Pc中国设计秀
Class ccClsTemplate9Pc中国设计秀

  Private ccStrCode,ccStrStorage9Pc中国设计秀
  Private ccStrCacheCode9Pc中国设计秀
  Private ccBlnPublicCache,ccBlnPrivateCache9Pc中国设计秀
  Private ccStrName,ccStrCookieName9Pc中国设计秀
  Private ccStrDirection,ccStrSaveDirection,ccStrFile,ccStrSaveFile,ccStrPath9Pc中国设计秀
  Private ccObjStream,ccObjFSO,ccStrFormat,ccIntObject,ccObjText,ccIntFormat9Pc中国设计秀

  Private Sub Class_Initialize9Pc中国设计秀
    ccStrName = "default"    '默认名称9Pc中国设计秀
    ccBlnPublicCache = False9Pc中国设计秀
    ccBlnPrivateCache = False9Pc中国设计秀
    ccStrFile = "cache.html"9Pc中国设计秀
    ccStrSaveFile = "save_cache.html"9Pc中国设计秀
    ccStrCookieName = "ccClass_Template"  'Application对象名前缀9Pc中国设计秀
    ccStrFormat = "UTF-8"    'UTF-8|ASCII|GB2312|BIG59Pc中国设计秀
    ccIntFormat = -19Pc中国设计秀
    ccIntObject = 1        '默认读取/保存模板组件 1:ADODB.Stream 2:FSO9Pc中国设计秀
    ccStrPath = Server.MapPath("./")&""  '默认根路径9Pc中国设计秀
  End Sub9Pc中国设计秀

  Public Property Let Name(ccStrName_in)9Pc中国设计秀
    ccStrName = LCase(Trim(ccStrName_in))9Pc中国设计秀
  End Property9Pc中国设计秀

  Public Property Let Format(ccStrFormat_in)9Pc中国设计秀
    ccStrFormat = ccStrFormat_in9Pc中国设计秀
    If InStr(LCase(Trim(ccStrFormat_in)),"utf") > 0 Then9Pc中国设计秀
      ccIntFormat = -19Pc中国设计秀
    Else9Pc中国设计秀
      ccIntFormat = 09Pc中国设计秀
    End If9Pc中国设计秀
  End Property9Pc中国设计秀

  Public Property Let Object(ccStrObject_in)9Pc中国设计秀
    ccStrObject_in = LCase(Trim(ccStrObject_in))9Pc中国设计秀
    If InStr(ccStrObject_in,"fso") > 0 Then9Pc中国设计秀
      ccIntObject = 29Pc中国设计秀
    Else9Pc中国设计秀
      ccIntObject = 19Pc中国设计秀
    End If9Pc中国设计秀
  End Property9Pc中国设计秀

  Public Property Let PublicCache(ccBlnPublicCache_in)9Pc中国设计秀
    If ccBlnPublicCache_in = True Then9Pc中国设计秀
      ccBlnPublicCache = True9Pc中国设计秀
    Else9Pc中国设计秀
      ccBlnPublicCache = False9Pc中国设计秀
    End If9Pc中国设计秀
  End Property9Pc中国设计秀

  Public Property Let PrivateCache(ccBlnPrivateCache_in)9Pc中国设计秀
    If ccBlnPrivateCache_in = True Then9Pc中国设计秀
      ccBlnPrivateCache = True9Pc中国设计秀
    Else9Pc中国设计秀
      ccBlnPrivateCache = False9Pc中国设计秀
    End If9Pc中国设计秀
  End Property9Pc中国设计秀

  Public Property Let Direction(ccStrDirection_in)9Pc中国设计秀
    ccStrDirection = ccStrDirection_in9Pc中国设计秀
  End Property9Pc中国设计秀

  Public Property Let File(ccStrFile_in)9Pc中国设计秀
    If ccStrFile_in <> "" Then9Pc中国设计秀
      ccStrFile = ccStrFile_in9Pc中国设计秀
    End If9Pc中国设计秀
  End Property9Pc中国设计秀

  Public Property Let SaveDirection(ccStrSaveDirection_in)9Pc中国设计秀
    ccStrSaveDirection = ccStrSaveDirection_in9Pc中国设计秀
  End Property9Pc中国设计秀

  Public Property Let SaveFile(ccStrSaveFile_in)9Pc中国设计秀
    If ccStrSaveFile_in <> "" Then9Pc中国设计秀
      ccStrSaveFile = ccStrSaveFile_in9Pc中国设计秀
    End If9Pc中国设计秀
  End Property9Pc中国设计秀

  Public Property Get Code9Pc中国设计秀
    Code = ccStrCode9Pc中国设计秀
  End Property9Pc中国设计秀

  Public Property Get Storage9Pc中国设计秀
    Storage = ccStrStorage9Pc中国设计秀
  End Property9Pc中国设计秀

  Public Sub ClearCache9Pc中国设计秀
    Call ClearPrivateCache9Pc中国设计秀
    Call ClearPublicCache9Pc中国设计秀
  End Sub9Pc中国设计秀

  Public Sub ClearPrivateCache9Pc中国设计秀
    ccStrCacheCode = ""9Pc中国设计秀
  End Sub9Pc中国设计秀

  Public Sub ClearPublicCache9Pc中国设计秀
    Application(ccStrCookieName&ccStrName) = ""9Pc中国设计秀
  End Sub9Pc中国设计秀

  Public Sub ClearStorage9Pc中国设计秀
    ccStrStorage = ""9Pc中国设计秀
  End Sub9Pc中国设计秀

  Public Sub ClearCode9Pc中国设计秀
    ccStrCode = ""9Pc中国设计秀
  End Sub9Pc中国设计秀

  Public Sub SaveFront9Pc中国设计秀
    ccStrStorage = ccStrCode & ccStrStorage9Pc中国设计秀
  End Sub9Pc中国设计秀

  Public Sub SaveLast9Pc中国设计秀
    ccStrStorage = ccStrStorage & ccStrCode9Pc中国设计秀
  End Sub9Pc中国设计秀

  Public Sub SaveCode9Pc中国设计秀
    Call SaveToFile(1)9Pc中国设计秀
  End Sub9Pc中国设计秀

  Public Sub SaveStorage9Pc中国设计秀
    Call SaveToFile(2)9Pc中国设计秀
  End Sub9Pc中国设计秀

  Public Sub SetVar(ccStrTag_in,ccStrValue_in)9Pc中国设计秀
    ccStrCode = RePlace(ccStrCode,ccStrTag_in,ccStrValue_in)9Pc中国设计秀
  End Sub9Pc中国设计秀

  Private Sub SaveToFile(ccIntCode_in)9Pc中国设计秀
    Dim ccStrSaveCode9Pc中国设计秀
    If ccIntCode_in = 1 Then9Pc中国设计秀
      ccStrSaveCode = ccStrCode9Pc中国设计秀
    Else9Pc中国设计秀
      ccStrSaveCode = ccStrStorage9Pc中国设计秀
    End If9Pc中国设计秀
    If ccIntObject = 1 Then9Pc中国设计秀
      Set ccObjStream = Server.CreateObject("ADODB.Stream")9Pc中国设计秀
      With ccObjStream9Pc中国设计秀
        .Type = 29Pc中国设计秀
        .Mode = 39Pc中国设计秀
        .Open9Pc中国设计秀
        .Charset = ccStrFormat9Pc中国设计秀
        .Position = ccObjStream.Size9Pc中国设计秀
        .WriteText ccStrSaveCode9Pc中国设计秀
        .SaveToFile ccStrPath & ccStrSaveDirection & "" & ccStrSaveFile,29Pc中国设计秀
        .Close9Pc中国设计秀
      End With9Pc中国设计秀
      Set ccObjStream = Nothing9Pc中国设计秀
    Else9Pc中国设计秀
      Set ccObjFSO = CreateObject("Scripting.FileSystemObject")9Pc中国设计秀
      If ccObjFSO.FileExists(ccStrPath & ccStrSaveDirection & "" & ccStrSaveFile) = True Then9Pc中国设计秀
        ccObjFSO.DeleteFile(ccStrPath & ccStrSaveDirection & "" & ccStrSaveFile)9Pc中国设计秀
      End If9Pc中国设计秀
      Set ccObjText = ccObjFSO.OpenTextFile(ccStrPath & ccStrSaveDirection & "" & ccStrSaveFile,2,True,ccIntFormat)9Pc中国设计秀
      ccObjText.Write ccStrSaveCode9Pc中国设计秀
      Set ccObjText = Nothing9Pc中国设计秀
      Set ccObjFSO = Nothing9Pc中国设计秀
    End If9Pc中国设计秀
    ccStrSaveCode = ""9Pc中国设计秀
  End Sub9Pc中国设计秀

  Public Sub Load9Pc中国设计秀
    ccStrCode = ""9Pc中国设计秀
    If ccBlnPrivateCache = True Then9Pc中国设计秀
      If ccFncIsEmpty(ccStrCacheCode) = False Then9Pc中国设计秀
        ccStrCode = ccStrCacheCode9Pc中国设计秀
        Exit Sub9Pc中国设计秀
      End If9Pc中国设计秀
    End If9Pc中国设计秀
    If ccBlnPublicCache = True Then9Pc中国设计秀
      If ccFncIsEmpty(Application(ccStrCookieName&ccStrName)) = False Then9Pc中国设计秀
        ccStrCode = Application(ccStrCookieName&ccStrName)9Pc中国设计秀
        Exit Sub9Pc中国设计秀
      End If9Pc中国设计秀
    End If9Pc中国设计秀
    If ccIntObject = 1 Then9Pc中国设计秀
      Set ccObjStream = Server.CreateObject("ADODB.Stream")9Pc中国设计秀
      With ccObjStream9Pc中国设计秀
        .Type = 29Pc中国设计秀
        .Mode = 39Pc中国设计秀
        .Open9Pc中国设计秀
        .Charset = ccStrFormat9Pc中国设计秀
        .Position = ccObjStream.Size9Pc中国设计秀
        .LoadFromFile ccStrPath & ccStrDirection & "" & ccStrFile9Pc中国设计秀
        ccStrCode = .ReadText9Pc中国设计秀
        .Close9Pc中国设计秀
      End With9Pc中国设计秀
      Set ccObjStream = Nothing9Pc中国设计秀
    Else9Pc中国设计秀
      Set ccObjFSO = CreateObject("Scripting.FileSystemObject")9Pc中国设计秀
      If ccObjFSO.FileExists(ccStrPath & ccStrDirection & "" & ccStrFile) = True Then9Pc中国设计秀
        Set ccObjText = ccObjFSO.OpenTextFile(ccStrPath & ccStrDirection & "" & ccStrFile,1,False,ccIntFormat)9Pc中国设计秀
        ccStrCode = ccObjText.ReadAll9Pc中国设计秀
        Set ccObjText = Nothing9Pc中国设计秀
      End If9Pc中国设计秀
      Set ccObjFSO = Nothing9Pc中国设计秀
    End If9Pc中国设计秀
    If ccBlnPrivateCache = True Then9Pc中国设计秀
      ccStrCacheCode = ccStrCode9Pc中国设计秀
    End If9Pc中国设计秀
    If ccBlnPublicCache = True Then9Pc中国设计秀
      Application(ccStrCookieName&ccStrName) = ccStrCode9Pc中国设计秀
    End If9Pc中国设计秀
End Sub9Pc中国设计秀

End Class9Pc中国设计秀

Function ccFncIsEmpty(ByRef ccStrValue_in)9Pc中国设计秀
  If IsNull(ccStrValue_in) Or IsEmpty(ccStrValue_in) Or ccStrValue_in = "" Then9Pc中国设计秀
    ccFncIsEmpty = True9Pc中国设计秀
  Else9Pc中国设计秀
    ccFncIsEmpty = False9Pc中国设计秀
  End If9Pc中国设计秀
End Function9Pc中国设计秀

 9Pc中国设计秀

9Pc中国设计秀
实例9Pc中国设计秀

9Pc中国设计秀
模板文件内容9Pc中国设计秀

 9Pc中国设计秀

<#test#>9Pc中国设计秀

asp程序代码9Pc中国设计秀

9Pc中国设计秀
Dim objTemplate9Pc中国设计秀
Set objTemplate = New ccClsTemplate9Pc中国设计秀
objTemplate.Name = "Test"9Pc中国设计秀
objTemplate.Format = "UTF-8"9Pc中国设计秀
'开启缓存9Pc中国设计秀
objTemplate.PublicCache = True9Pc中国设计秀
objTemplate.PrivateCache = True9Pc中国设计秀
'设置模板目录和文件名9Pc中国设计秀
objTemplate.Direction = "test"9Pc中国设计秀
objTemplate.File = "test.html"9Pc中国设计秀
'设置保存文件目录和文件名9Pc中国设计秀
objTemplate.SaveDirection = "test"9Pc中国设计秀
objTemplate.SaveFile = "test3.html"9Pc中国设计秀
'载入模板9Pc中国设计秀
Call objTemplate.Load9Pc中国设计秀
'进行文本替换9Pc中国设计秀
Call objTemplate.SetVar("<#test#>","Hello world.")9Pc中国设计秀
'将文本保存至Storage暂存9Pc中国设计秀
Call objTemplate.SaveLast9Pc中国设计秀
'重新载入模板,此时将从私有缓存重新装载,提高效率9Pc中国设计秀
Call objTemplate.Load9Pc中国设计秀
'替换为其他值9Pc中国设计秀
Call objTemplate.SetVar("<#test#>"," By Cloudream.")9Pc中国设计秀
'保存至Storage结尾暂存9Pc中国设计秀
Call objTemplate.SaveLast9Pc中国设计秀
'保存Code至文件9Pc中国设计秀
Call objTemplate.SaveCode9Pc中国设计秀
Response.Write objTemplate.Storage9Pc中国设计秀

Set objTemplate = Nothing9Pc中国设计秀

9Pc中国设计秀
显示结果9Pc中国设计秀

9Pc中国设计秀
Hello world. By Cloudream.9Pc中国设计秀

保存文件结果9Pc中国设计秀

9Pc中国设计秀
By Cloudream.9Pc中国设计秀

本文引用地址:/bc/article_46514.html
网站地图 | 关于我们 | 联系我们 | 网站建设 | 广告服务 | 版权声明 | 免责声明