国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

ASP支持嵌套模板和循環(huán)標(biāo)簽的模板類

2019-11-17 04:13:40
字體:
供稿:網(wǎng)友

asp Template 類說明

作者:shaoyun Form www.devjs.com
時(shí)間:17:05 2008-12-10
++功能簡介

--支持單層循環(huán)標(biāo)簽,并可在一個(gè)頁面類多次使用.
--支持引入模板文件,在裝載的時(shí)候,將進(jìn)行模板的合并
--可指定模板文件路徑,路徑為相對路徑,默認(rèn)為當(dāng)前文件路徑
--對于空白行最終輸出的時(shí)候,進(jìn)行刪除

++標(biāo)簽定義

{$tag$} 普通標(biāo)簽
{$include:filename$} 模板文件標(biāo)簽
<loop name="tagname">...</loop> 循環(huán)標(biāo)簽,name屬性為標(biāo)簽名稱
{$tag/subtag$} 循環(huán)標(biāo)簽中的子標(biāo)簽

++標(biāo)簽說明:

采用正則表達(dá)式進(jìn)行標(biāo)簽的匹配和過濾,loop標(biāo)簽中的name屬性之前可以有多個(gè)空格,之前之后可以存在其他屬性,name屬性可以帶引號也可以不帶,識別單引號和雙引號,設(shè)定只匹配第一個(gè)

++函數(shù)說明

LoadTPL函數(shù) 讀取模板文件,讀取的時(shí)候,檢查模板文件里的嵌套模板文件標(biāo)簽,首先替換嵌套的模板文件標(biāo)簽的內(nèi)容,合并模板文件,存入變量

Assign函數(shù) 分析模板標(biāo)簽,對于普通標(biāo)簽將其加入數(shù)據(jù)對象,如果為循環(huán)標(biāo)簽,則存入循環(huán)數(shù)據(jù)對象,如果循環(huán)標(biāo)簽對象更換,則將循環(huán)累加的數(shù)據(jù)加入數(shù)據(jù)對象

Flush函數(shù) 模板類很重要的一個(gè)函數(shù),用于處理循環(huán)標(biāo)簽,對于單次的循環(huán),執(zhí)行循環(huán)塊內(nèi)部替換,對循環(huán)數(shù)據(jù)進(jìn)行累加保存,每個(gè)單次循環(huán)完后必須調(diào)用

Bulid函數(shù) 將沒有來的及保存的循環(huán)數(shù)據(jù)加入到數(shù)據(jù)對象,然后按照模板定義輸出數(shù)據(jù)對象中的所有數(shù)據(jù),普通標(biāo)簽的替換在這一步完成

特別說明一下,assign函數(shù)有一個(gè)便捷的賦值方法,就是調(diào)用默認(rèn)屬性來賦值,效果是一致的,例如:

 程序代碼
tp.assign("title","新聞")

可以采取這樣更簡潔的賦值方式

 程序代碼
tp("title")="新聞"

tp是實(shí)例化的模板對象

整個(gè)模板了代碼如下(template.asp):


 程序代碼
<%
Class Template

PRivate m_content,m_looptmp,tagData,loopdata,m_loop_content,m_Looptag,m_TplPath,m_SetTplPath
Private m_ClassName,m_Version,m_Copyright

Private Sub Class_Initialize()
  m_content="" : m_looptmp="" : m_loop_content="" : m_looptag=""
  m_ClassName="Shaoyun ASP Template類" : m_Version="1.0" : m_Copyright="DevJS.com"
  m_TplPath="./" : m_SetTplPath=false
  Set tagData = Server.CreateObject("Scr  Set loopData = Server.CreateObject("Scripting.Dictionary")
End Sub

Private Sub Class_Terminate()
  m_content="" : m_looptmp="" : m_loop_content="" : m_looptag=""
  m_TplPath="./" : m_SetTplPath=false
  Set tagData = Nothing : Set loopData = Nothing
End Sub

Public Property Get ClassName
  ClassName = m_ClassName
End Property

Public Property Get Version
  Version = m_Version
End Property

Public Property Get Copyright
  Copyright = m_Copyright
End Property

Rem 模板類的默認(rèn)屬性,判斷模板中是否含有這個(gè)標(biāo)簽
Public Default Property Get Tag(tagname)
  Tag = InStr(m_content,"{$" & tagname & "$")>0
End Property

Rem 調(diào)用定義好的賦值函數(shù),這個(gè)屬性用來簡化賦值操作
Public Property Let Tag(tagname,replaceString)
  Call Assign(tagname,replaceString)
End Property

Public Property Get TplPath
  TplPath = m_TplPath
End Property

Rem 設(shè)定模板文件的路徑
Public Property Let TplPath(sTplPath)
  If sTplPath<>"" Then m_TplPath = sTplPath
  If Right(m_TplPath,1)<>"/" Then m_TplPath = m_TplPath & "/"
End Property

Private Function LoadFromFile(sFilePath,sCharset)
  LoadFromFile=false
  Dim oStream
  Set oStream=Server.CreateObject("ADODB.Stream")
  oStream.Type=2
  oStream.Mode=3
  oStream.Open
  oStream.Charset=sCharset
  oStream.Position=oStream.Size
  oStream.LoadFromFile sFilePath
  LoadFromFile=oStream.ReadText
  oStream.Close
  Set oStream=Nothing
End Function

Private Function FileExist(filespec)
  On Error Resume Next
  FileExist=False
  Dim o
fso : Set oFSO = Server.CreateObject("Scripting.FileSystemObject")
  FileExist=oFSO.FileExists(filespec)
  Set oFSO=Nothing
End Function

Rem 獲取循環(huán)塊
Private Function GetTmpStr(tplstr,tagname,attname)
  Dim regEx,Matches,Match
  Set regEx = New RegExp
  regEx.Pattern = "<" & tagname & ".*?/s+name=[/""|/']?" & attname & "[/""|/']?.*?>([/s/S.]*?)<//" & tagname & ">"
  regEx.Global = False
  regEx.IgnoreCase = True
  Set Matches = regEx.Execute(tplstr)
  For Each Match in Matches
  GetTmpStr=Match.Value
  Next
  Set regEx = Nothing
End Function

Rem 移除HTML標(biāo)記
Private Function RemoveTag(tagString,tagname)
  Dim regex
  Set regex=New RegExp
  regEx.Pattern = "<[//]?" & tagname & ".*?>"
  regEx.Global = True
  regEx.IgnoreCase = True
  RemoveTag = regEx.Replace(tagString,"")
  Set regex=nothing
End Function

Rem 移除空白行
Private Function RemoveSpace(tagString)
  Dim regex
  Set regex=New RegExp
  regEx.Pattern = "/n/s*/r"
  regEx.Global = True
  regEx.IgnoreCase = True
  RemoveSpace = regEx.Replace(tagString,"")
  Set regex=nothing
End Function

Rem 讀取模板文件,同時(shí)處理嵌套模板,進(jìn)行模板的合并
Public Function LoadTpl(tplfile)
  tplfile=Server.MapPath(tplfile)
  If Not FileExist(tplfile) Then
    Response.Write "Load template file faild!"
    Response.End
    Exit Function
  End If
  m_content=LoadFromFile(tplfile,"GB2312")
  Dim regEx,Matches,Match,fname,sContent
  Set regEx = New RegExp
  regEx.Pattern = "/{/$include/:(.*?)/$/}"
  regEx.Global = True
  regEx.IgnoreCase = True
  Set Matches = regEx.Execute(m_content)
  For Each Match in Matches
    fname=Match.SubMatches(0)
    fname=Server.MapPath(m_TplPath & fname)
    If FileExist(fname) Then
      sContent=LoadFromFile(fname,"GB2312")
      m_content=replace(m_content,Match.value,sContent)
    End If
  Next
  Set regEx = Nothing
End Function

Rem 賦值替換函數(shù)
Public Function Assign(tagname,replaceString)
  If tagname="" Then Exit Function
  Rem 如果是循環(huán)標(biāo)簽
  If InStr(tagname,"/")>0 and InStr(tagname,"/")<Len(tagname) Then
    Rem 獲取循環(huán)標(biāo)簽名稱
    m_curLooptag=Left(tagname,InStrRev(tagname,"/")-1)
    If m_Looptag="" Then
      Rem 如果是第一次檢測到循環(huán)標(biāo)簽,設(shè)置循環(huán)所需變量初始值
      m_looptag=m_curLooptag : m_loop_content=""
      m_looptmp=GetTmpStr(m_content,"loop",m_Looptag)
    Else
      If m_LoopTag<>m_curLooptag Then
        Rem 如果循環(huán)標(biāo)簽改變,初始循環(huán)變量
        m_content=replace(m_content,m_looptmp,m_loop_content)
        m_looptag=m_curLooptag : m_loop_content=""
        m_looptmp=GetTmpStr(m_content,"loop",m_Looptag)
      End If
    End If
    If Not(loopData.Exists(tagname)) Then loopData.Add tagname,replaceString
  Else
    Rem 普通標(biāo)簽
    tagData.Add tagname,replaceString
  End If
End Function

Rem 執(zhí)行塊內(nèi)替換
Public Function Flush()
  If loopdata.count>0 then
    Dim i
    chgtmp=RemoveTag(m_looptmp,"loop")
    arrtag=loopData.keys
    arrval=loopData.items
    For i=0 To loopData.count-1
      chgtmp=replace(chgtmp,"{$" & arrtag(i) & "$}",arrval(i))
    Next
    Rem 將塊內(nèi)數(shù)據(jù)保存到變量中
    m_loop_content=m_loop_content & chgtmp
    loopdata.RemoveAll
  End if
End Function

Rem 構(gòu)建,完成模板的最后替換
Public Function Bulid()
  m_content=replace(m_content,m_looptmp,m_loop_content)
  arrtag=tagData.keys
  arrval=tagData.items
  For i=0 To tagData.count-1
    m_content=replace(m_content,"{$" & arrtag(i) & "$}",arrval(i))
  Next
  m_Content=RemoveSpace(m_Content)
  Response.Write m_Content
End Function

End Class
%>


父模板模板代碼(default.tpl):


 程序代碼
{$include:head.tpl$}
<h1 align=center>{$doc_title$}</h1>
<h3>{$news_title$}</h3>
<ul>
<loop name="news">
  <Li style="color:#F00">新聞標(biāo)題:{$news/title$}--作者:{$news/author$}</Li>
</loop>
</ul>
<h3>{$lastest_news$}</h3>
<ul>
<!-- 這里loop中的bing和count只用作測試,不是必須的,實(shí)際使用的時(shí)候請刪除 -->
<loop bind="id"  name=arts count="15">
  <Li>文章標(biāo)題:{$arts/title$}--作者:{$arts/author$}</Li>
</loop>
</ul>
{$include:foot.tpl$}


嵌套的子模板(head.tpl):


 程序代碼
<title>{$doc_title$}</title>


嵌套的子模板(foot.tpl):


 程序代碼
<p align=center>Copyright By DevJS.Com</p>


調(diào)用代碼(default.asp):


 程序代碼
<!--#include file="function/template.asp"-->
<%
Rem 模板類的使用方法事例

Set tp = new Template
tp.tplpath="tpl"
tp.LoadTpl(tp.tplpath & "default.tpl")
tp.assign "doc_title","模板機(jī)制的例子"
tp.assign "news_title","國內(nèi)新聞"
for i=0 to 2
  call tp.assign("arts/title","金融危機(jī)導(dǎo)致大批失業(yè)人員")
  call tp.assign("arts/author","網(wǎng)易")
  tp.flush
next
tp.assign "lastest_news","最新文章"
Rem 這里改用另一種賦值方式
for i=0 to 2
  tp("news/title")="政府利好消息將有助拉高股市"
  tp("news/author")="SOHU"
  tp.flush
next
tp.bulid
Set tp = nothing
%>


本文來源于shaoyun的blog http://www.devjs.com/ , 原文地址:http://www.devjs.com/post/asp-template-class.html


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 白银市| 元氏县| 平昌县| 息烽县| 通城县| 象州县| 云和县| 吉木萨尔县| 乐平市| 民和| 库伦旗| 扶风县| 鹿泉市| 克山县| 荣成市| 布拖县| 铜梁县| 淅川县| 乌拉特后旗| 巫山县| 边坝县| 罗江县| 乡宁县| 轮台县| 吉安县| 绥滨县| 长垣县| 黄大仙区| 安庆市| 麦盖提县| 大宁县| 尤溪县| 永城市| 徐汇区| 铜山县| 太仓市| 盐亭县| 大洼县| 罗田县| 庄浪县| 双峰县|