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

ASP支持嵌套模板和循环标签的模板类技巧

日期:05-04    来源:中国设计秀    作者:cnwebshow.com

asp Template 类说明DVu中国设计秀

--支持单层循环标签,并可在一个页面类多次使用.DVu中国设计秀
--支持引入模板文件,在装载的时候,将进行模板的合并DVu中国设计秀
--可指定模板文件路径,路径为相对路径,默认为当前文件路径DVu中国设计秀
--对于空白行最终输出的时候,进行删除DVu中国设计秀

++标签定义DVu中国设计秀

{$tag$} 普通标签DVu中国设计秀
{$include:filename$} 模板文件标签DVu中国设计秀
<loop name="tagname">...</loop> 循环标签,name属性为标签名称DVu中国设计秀
{$tag/subtag$} 循环标签中的子标签DVu中国设计秀

++标签说明:DVu中国设计秀

采用正则表达式进行标签的匹配和过滤,loop标签中的name属性之前可以有多个空格,之前之后可以存在其他属性,name属性可以带引号也可以不带,识别单引号和双引号,设定只匹配第一个DVu中国设计秀

++函数说明DVu中国设计秀

LoadTPL函数 读取模板文件,读取的时候,检查模板文件里的嵌套模板文件标签,首先替换嵌套的模板文件标签的内容,合并模板文件,存入变量DVu中国设计秀

Assign函数 分析模板标签,对于普通标签将其加入数据对象,如果为循环标签,则存入循环数据对象,如果循环标签对象更换,则将循环累加的数据加入数据对象DVu中国设计秀

Flush函数 模板类很重要的一个函数,用于处理循环标签,对于单次的循环,执行循环块内部替换,对循环数据进行累加保存,每个单次循环完后必须调用DVu中国设计秀

Bulid函数 将没有来的及保存的循环数据加入到数据对象,然后按照模板定义输出数据对象中的所有数据,普通标签的替换在这一步完成DVu中国设计秀

特别说明一下,assign函数有一个便捷的赋值方法,就是调用默认属性来赋值,效果是一致的,例如:DVu中国设计秀

 程序代码DVu中国设计秀
tp.assign("title","新闻")DVu中国设计秀

可以采取这样更简洁的赋值方式DVu中国设计秀

 程序代码DVu中国设计秀
tp("title")="新闻"DVu中国设计秀

tp是实例化的模板对象DVu中国设计秀

整个模板了代码如下(template.asp):DVu中国设计秀

DVu中国设计秀
 程序代码DVu中国设计秀
<%DVu中国设计秀
Class TemplateDVu中国设计秀

PRivate m_content,m_looptmp,tagData,loopdata,m_loop_content,m_Looptag,m_TplPath,m_SetTplPathDVu中国设计秀
Private m_ClassName,m_Version,m_CopyrightDVu中国设计秀

Private Sub Class_Initialize()DVu中国设计秀
  m_content="" : m_looptmp="" : m_loop_content="" : m_looptag=""DVu中国设计秀
  m_ClassName="Shaoyun ASP Template类" : m_Version="1.0" : m_Copyright="DevJS.com"DVu中国设计秀
  m_TplPath="./" : m_SetTplPath=falseDVu中国设计秀
  Set tagData = Server.CreateObject("Scripting.Dictionary")DVu中国设计秀
  Set loopData = Server.CreateObject("Scripting.Dictionary")DVu中国设计秀
End SubDVu中国设计秀

Private Sub Class_Terminate()DVu中国设计秀
  m_content="" : m_looptmp="" : m_loop_content="" : m_looptag=""DVu中国设计秀
  m_TplPath="./" : m_SetTplPath=falseDVu中国设计秀
  Set tagData = Nothing : Set loopData = NothingDVu中国设计秀
End SubDVu中国设计秀

Public Property Get ClassNameDVu中国设计秀
  ClassName = m_ClassNameDVu中国设计秀
End PropertyDVu中国设计秀

Public Property Get VersionDVu中国设计秀
  Version = m_VersionDVu中国设计秀
End PropertyDVu中国设计秀

Public Property Get CopyrightDVu中国设计秀
  Copyright = m_CopyrightDVu中国设计秀
End PropertyDVu中国设计秀

Rem 模板类的默认属性,判断模板中是否含有这个标签DVu中国设计秀
Public Default Property Get Tag(tagname)DVu中国设计秀
  Tag = InStr(m_content,"{$" & tagname & "$")>0DVu中国设计秀
End PropertyDVu中国设计秀

Rem 调用定义好的赋值函数,这个属性用来简化赋值操作DVu中国设计秀
Public Property Let Tag(tagname,replaceString)DVu中国设计秀
  Call Assign(tagname,replaceString)DVu中国设计秀
End PropertyDVu中国设计秀

Public Property Get TplPathDVu中国设计秀
  TplPath = m_TplPathDVu中国设计秀
End PropertyDVu中国设计秀

Rem 设定模板文件的路径DVu中国设计秀
Public Property Let TplPath(sTplPath)DVu中国设计秀
  If sTplPath<>"" Then m_TplPath = sTplPathDVu中国设计秀
  If Right(m_TplPath,1)<>"/" Then m_TplPath = m_TplPath & "/"DVu中国设计秀
End PropertyDVu中国设计秀

Private Function LoadFromFile(sFilePath,sCharset)DVu中国设计秀
  LoadFromFile=falseDVu中国设计秀
  Dim oStreamDVu中国设计秀
  Set oStream=Server.CreateObject("ADODB.Stream")DVu中国设计秀
  oStream.Type=2DVu中国设计秀
  oStream.Mode=3DVu中国设计秀
  oStream.OpenDVu中国设计秀
  oStream.Charset=sCharsetDVu中国设计秀
  oStream.Position=oStream.SizeDVu中国设计秀
  oStream.LoadFromFile sFilePathDVu中国设计秀
  LoadFromFile=oStream.ReadTextDVu中国设计秀
  oStream.CloseDVu中国设计秀
  Set oStream=NothingDVu中国设计秀
End FunctionDVu中国设计秀

Private Function FileExist(filespec)DVu中国设计秀
  On Error Resume NextDVu中国设计秀
  FileExist=FalseDVu中国设计秀
  Dim ofso : Set oFSO = Server.CreateObject("Scripting.FileSystemObject")DVu中国设计秀
  FileExist=oFSO.FileExists(filespec)DVu中国设计秀
  Set oFSO=NothingDVu中国设计秀
End FunctionDVu中国设计秀

Rem 获取循环块DVu中国设计秀
Private Function GetTmpStr(tplstr,tagname,attname)DVu中国设计秀
  Dim regEx,Matches,MatchDVu中国设计秀
  Set regEx = New RegExpDVu中国设计秀
  regEx.Pattern = "<" & tagname & ".*?s+name=[""|']?" & attname & "[""|']?.*?>([sS.]*?)</" & tagname & ">"DVu中国设计秀
  regEx.Global = FalseDVu中国设计秀
  regEx.IgnoreCase = TrueDVu中国设计秀
  Set Matches = regEx.Execute(tplstr)DVu中国设计秀
  For Each Match in MatchesDVu中国设计秀
  GetTmpStr=Match.ValueDVu中国设计秀
  NextDVu中国设计秀
  Set regEx = Nothing DVu中国设计秀
End FunctionDVu中国设计秀

Rem 移除HTML标记DVu中国设计秀
Private Function RemoveTag(tagString,tagname)DVu中国设计秀
  Dim regexDVu中国设计秀
  Set regex=New RegExpDVu中国设计秀
  regEx.Pattern = "<[/]?" & tagname & ".*?>"DVu中国设计秀
  regEx.Global = TrueDVu中国设计秀
  regEx.IgnoreCase = TrueDVu中国设计秀
  RemoveTag = regEx.Replace(tagString,"")DVu中国设计秀
  Set regex=nothing DVu中国设计秀
End FunctionDVu中国设计秀

Rem 移除空白行DVu中国设计秀
Private Function RemoveSpace(tagString)DVu中国设计秀
  Dim regexDVu中国设计秀
  Set regex=New RegExpDVu中国设计秀
  regEx.Pattern = "ns*r"DVu中国设计秀
  regEx.Global = TrueDVu中国设计秀
  regEx.IgnoreCase = TrueDVu中国设计秀
  RemoveSpace = regEx.Replace(tagString,"")DVu中国设计秀
  Set regex=nothing DVu中国设计秀
End FunctionDVu中国设计秀

Rem 读取模板文件,同时处理嵌套模板,进行模板的合并DVu中国设计秀
Public Function LoadTpl(tplfile)DVu中国设计秀
  tplfile=Server.MapPath(tplfile)DVu中国设计秀
  If Not FileExist(tplfile) ThenDVu中国设计秀
    Response.Write "Load template file faild!"DVu中国设计秀
    Response.EndDVu中国设计秀
    Exit FunctionDVu中国设计秀
  End IfDVu中国设计秀
  m_content=LoadFromFile(tplfile,"GB2312")DVu中国设计秀
  Dim regEx,Matches,Match,fname,sContentDVu中国设计秀
  Set regEx = New RegExpDVu中国设计秀
  regEx.Pattern = "{$include:(.*?)$}"DVu中国设计秀
  regEx.Global = TrueDVu中国设计秀
  regEx.IgnoreCase = TrueDVu中国设计秀
  Set Matches = regEx.Execute(m_content)DVu中国设计秀
  For Each Match in MatchesDVu中国设计秀
    fname=Match.SubMatches(0)DVu中国设计秀
    fname=Server.MapPath(m_TplPath & fname)DVu中国设计秀
    If FileExist(fname) ThenDVu中国设计秀
      sContent=LoadFromFile(fname,"GB2312")DVu中国设计秀
      m_content=replace(m_content,Match.value,sContent)DVu中国设计秀
    End IfDVu中国设计秀
  NextDVu中国设计秀
  Set regEx = NothingDVu中国设计秀
End FunctionDVu中国设计秀

Rem 赋值替换函数DVu中国设计秀
Public Function Assign(tagname,replaceString)DVu中国设计秀
  If tagname="" Then Exit FunctionDVu中国设计秀
  Rem 如果是循环标签DVu中国设计秀
  If InStr(tagname,"/")>0 and InStr(tagname,"/")<Len(tagname) ThenDVu中国设计秀
    Rem 获取循环标签名称DVu中国设计秀
    m_curLooptag=Left(tagname,InStrRev(tagname,"/")-1)DVu中国设计秀
    If m_Looptag="" ThenDVu中国设计秀
      Rem 如果是第一次检测到循环标签,设置循环所需变量初始值DVu中国设计秀
      m_looptag=m_curLooptag : m_loop_content=""DVu中国设计秀
      m_looptmp=GetTmpStr(m_content,"loop",m_Looptag)DVu中国设计秀
    ElseDVu中国设计秀
      If m_LoopTag<>m_curLooptag ThenDVu中国设计秀
        Rem 如果循环标签改变,初始循环变量DVu中国设计秀
        m_content=replace(m_content,m_looptmp,m_loop_content)DVu中国设计秀
        m_looptag=m_curLooptag : m_loop_content=""DVu中国设计秀
        m_looptmp=GetTmpStr(m_content,"loop",m_Looptag)DVu中国设计秀
      End IfDVu中国设计秀
    End IfDVu中国设计秀
    If Not(loopData.Exists(tagname)) Then loopData.Add tagname,replaceStringDVu中国设计秀
  ElseDVu中国设计秀
    Rem 普通标签DVu中国设计秀
    tagData.Add tagname,replaceStringDVu中国设计秀
  End IfDVu中国设计秀
End FunctionDVu中国设计秀

Rem 执行块内替换DVu中国设计秀
Public Function Flush()DVu中国设计秀
  If loopdata.count>0 thenDVu中国设计秀
    Dim iDVu中国设计秀
    chgtmp=RemoveTag(m_looptmp,"loop")DVu中国设计秀
    arrtag=loopData.keysDVu中国设计秀
    arrval=loopData.itemsDVu中国设计秀
    For i=0 To loopData.count-1DVu中国设计秀
      chgtmp=replace(chgtmp,"{$" & arrtag(i) & "$}",arrval(i))DVu中国设计秀
    NextDVu中国设计秀
    Rem 将块内数据保存到变量中DVu中国设计秀
    m_loop_content=m_loop_content & chgtmpDVu中国设计秀
    loopdata.RemoveAllDVu中国设计秀
  End ifDVu中国设计秀
End FunctionDVu中国设计秀

Rem 构建,完成模板的最后替换DVu中国设计秀
Public Function Bulid()DVu中国设计秀
  m_content=replace(m_content,m_looptmp,m_loop_content)DVu中国设计秀
  arrtag=tagData.keysDVu中国设计秀
  arrval=tagData.itemsDVu中国设计秀
  For i=0 To tagData.count-1DVu中国设计秀
    m_content=replace(m_content,"{$" & arrtag(i) & "$}",arrval(i))DVu中国设计秀
  NextDVu中国设计秀
  m_Content=RemoveSpace(m_Content)DVu中国设计秀
  Response.Write m_ContentDVu中国设计秀
End FunctionDVu中国设计秀

End ClassDVu中国设计秀
%>DVu中国设计秀

DVu中国设计秀
父模板模板代码(default.tpl):DVu中国设计秀

DVu中国设计秀
 程序代码DVu中国设计秀
{$include:head.tpl$}DVu中国设计秀
<h1 align=center>{$doc_title$}</h1>DVu中国设计秀
<h3>{$news_title$}</h3>DVu中国设计秀
<ul>DVu中国设计秀
<loop name="news">DVu中国设计秀
  <Li style="color:#F00">新闻标题:{$news/title$}--作者:{$news/author$}</Li>DVu中国设计秀
</loop>DVu中国设计秀
</ul>DVu中国设计秀
<h3>{$lastest_news$}</h3>DVu中国设计秀
<ul>DVu中国设计秀
<!-- 这里loop中的bing和count只用作测试,不是必须的,实际使用的时候请删除 -->DVu中国设计秀
<loop bind="id"  name=arts count="15">DVu中国设计秀
  <Li>文章标题:{$arts/title$}--作者:{$arts/author$}</Li>DVu中国设计秀
</loop>DVu中国设计秀
</ul>DVu中国设计秀
{$include:foot.tpl$}DVu中国设计秀

DVu中国设计秀
嵌套的子模板(head.tpl):DVu中国设计秀

DVu中国设计秀
 程序代码DVu中国设计秀
<title>{$doc_title$}</title>DVu中国设计秀

DVu中国设计秀
嵌套的子模板(foot.tpl):DVu中国设计秀

DVu中国设计秀
 程序代码DVu中国设计秀
<p align=center>Copyright By DevJS.Com</p>DVu中国设计秀

DVu中国设计秀
调用代码(default.asp):DVu中国设计秀

DVu中国设计秀
 程序代码DVu中国设计秀
<!--#include file="function/template.asp"-->DVu中国设计秀
<%DVu中国设计秀
Rem 模板类的使用方法事例DVu中国设计秀

Set tp = new TemplateDVu中国设计秀
tp.tplpath="tpl"DVu中国设计秀
tp.LoadTpl(tp.tplpath & "default.tpl")DVu中国设计秀
tp.assign "doc_title","模板机制的例子"DVu中国设计秀
tp.assign "news_title","国内新闻"DVu中国设计秀
for i=0 to 2DVu中国设计秀
  call tp.assign("arts/title","金融危机导致大批失业人员")DVu中国设计秀
  call tp.assign("arts/author","网易")DVu中国设计秀
  tp.flushDVu中国设计秀
nextDVu中国设计秀
tp.assign "lastest_news","最新文章"DVu中国设计秀
Rem 这里改用另一种赋值方式DVu中国设计秀
for i=0 to 2DVu中国设计秀
  tp("news/title")="政府利好消息将有助拉高股市"DVu中国设计秀
  tp("news/author")="SOHU"DVu中国设计秀
  tp.flushDVu中国设计秀
nextDVu中国设计秀
tp.bulidDVu中国设计秀
Set tp = nothingDVu中国设计秀
%>DVu中国设计秀

DVu中国设计秀
本文来源于shaoyun的blog http://www.devjs.com/ , 原文地址:http://www.devjs.com/post/asp-template-class.htmlDVu中国设计秀

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