ASP Template class description
author: shaoyun Form www.devjs.com
Time: 17:05 2008-12-10
++Function introduction
--Supports single-layer loop tags and can be used multiple times in a page class.
--Supports the introduction of template files. When loading, the templates will be merged.
--You can specify the template file path. The path is a relative path and the default is the current file path.
--For the final output of blank lines, delete
the ++ tag definition
{$tag$} ordinary tags
{$include:filename$} template file tag
<loop name="tagname">...</loop> loop tag, the name attribute is the tag name
++ tag description
in loop tag
:Use regular expressions to match and filter tags. There can be multiple spaces before the name attribute in the loop tag, and there can be other attributes before and after, name Attributes can be quoted or not. Single quotes and double quotes are recognized. The setting only matches the first
++ function description
.The LoadTPL function reads the template file. When reading, check the nested template file tag in the template file. First, replace the content of the nested template file tag, merge the template files, and store it in the variable
Assign function to analyze the template tag. For ordinary tags, add it to the data object. If it is a loop tag, store it in the loop data object. If it is a loop tag object, replace it. , then the data accumulated in the cycle is added to the data object
Flush function template class. It is a very important function used to process cycle labels. For a single cycle, it performs internal replacement of the cycle block and accumulates and saves the cycle data. Each single cycle After completion, the Bulid function must be called
to add the unsaved and saved loop data to the data object, and then output all the data in the data object according to the template definition. The replacement of ordinary labels is completed in this step. In
particular, the assign function has a convenient The assignment method is to call the default attribute to assign the value. The effect is the same, for example:
program code
tp.assign("title","news")
can use this more concise assignment method
program code
tp("title")="News"
tp is an instantiated template object.
The entire template code is as follows (template.asp):
program code
<%
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 Class" : m_Version="1.0" : m_Copyright="DevJS.com"
m_TplPath="./" : m_SetTplPath=false
Set tagData = Server.CreateObject("Scripting.Dictionary")
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 is the default property of the template class to determine whether the template contains this tag.
Public Default Property Get Tag(tagname)
Tag = InStr(m_content,"{$" & tagname & "$")>0
End Property
Rem calls the defined assignment function. This property is used to simplify the assignment operation.
Public Property Let Tag(tagname,replaceString)
Call Assign(tagname,replaceString)
End Property
Public Property Get TplPath
TplPath = m_TplPath
End Property
Rem sets the path of the template file
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 oFSO : Set oFSO = Server.CreateObject("Scripting.FileSystemObject")
FileExist=oFSO.FileExists(filespec)
Set oFSO=Nothing
End Function
Rem gets the loop block
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 removes HTML tags
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 Remove blank lines
Private Function RemoveSpace(tagString)
Dim regex
Set regex=New RegExp
regEx.Pattern = "ns*r"
regEx.Global = True
regEx.IgnoreCase = True
RemoveSpace = regEx.Replace(tagString,"")
Set regex=nothing
End Function
Rem reads template files, processes nested templates at the same time, and merges templates
Public Function LoadTpl(tplfile)
tplfile=Server.MapPath(tplfile)
If Not FileExist(tplfile) Then
Response.Write "Load template file failed!"
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 assignment replacement function
Public Function Assign(tagname,replaceString)
If tagname="" Then Exit Function
Rem if it is a loop label
If InStr(tagname,"/")>0 and InStr(tagname,"/")<Len(tagname) Then
Rem gets the loop label name
m_curLooptag=Left(tagname,InStrRev(tagname,"/")-1)
If m_Looptag="" Then
Rem If the loop label is detected for the first time, set the initial value of the variables required for the loop.
m_looptag=m_curLooptag : m_loop_content=""
m_looptmp=GetTmpStr(m_content,"loop",m_Looptag)
Else
If m_LoopTag<>m_curLooptag Then
Rem If the loop label changes, the initial loop variable
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 common label
tagData.Add tagname,replaceString
End If
End Function
Rem performs intra-block replacement
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 saves the data in the block to a variable
m_loop_content=m_loop_content & chgtmp
loopdata.RemoveAll
End if
End Function
Rem build, complete the final replacement of the template
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
%>
Parent template template code (default.tpl):
program code
{$include:head.tpl$}
<h1 align=center>{$doc_title$}</h1>
<h3>{$news_title$}</h3>
<ul>
<loop name="news">
<Li style="color:#F00">News title: {$news/title$}--Author: {$news/author$}</Li>
</loop>
</ul>
<h3>{$lastest_news$}</h3>
<ul>
<!-- The bing and count in the loop here are only for testing and are not necessary. Please delete them when actually using them -->
<loop bind="id" name=arts count="15">
<Li>Article title: {$arts/title$}--Author: {$arts/author$}</Li>
</loop>
</ul>
{$include:foot.tpl$}
Nested subtemplate (head.tpl):
program code
<title>{$doc_title$}</title>
Nested child template (foot.tpl):
program code
<p align=center>Copyright By DevJS.Com</p>
Calling code (default.asp):
program code
<!--#include file="function/template.asp"-->
<%
Rem template class usage example
Set tp = new Template
tp.tplpath="tpl"
tp.LoadTpl(tp.tplpath & "default.tpl")
tp.assign "doc_title","Example of template mechanism"
tp.assign "news_title","domestic news"
for i=0 to 2
call tp.assign("arts/title","The financial crisis has led to a large number of unemployed people")
call tp.assign("arts/author","NetEase")
tp.flush
next
tp.assign "lastest_news","Latest Article"
Rem uses another assignment method here
for i=0 to 2
tp("news/title")="Good news from the government will help boost the stock market"
tp("news/author")="SOHU"
tp.flush
next
tp.bulid
Set tp = nothing
%>
This article comes from shaoyun's blog http://www.devjs.com/ , original address: http://www.devjs.com/post/asp-template-class.html