ASP template Class Code

Source: Internet
Author: User
Class Template

Private M_filename, M_root, m_unknowns, M_lasterror, M_haltonerr
Private M_valuelist, M_blocklist
Private M_regexp
Private Sub Class_Initialize
Set m_valuelist = CreateObject ("Scripting.Dictionary")
Set m_blocklist = CreateObject ("Scripting.Dictionary")
Set m_regexp = New RegExp
M_regexp.ignorecase = True
M_regexp.global = True
M_filename = ""
M_root = "."
m_unknowns = "Remove"
M_lasterror = ""
M_haltonerr = True
End Sub

Private Sub Class_Terminate
Set M_regexp = Nothing
Set m_blockmatches = Nothing
Set m_valuematches = Nothing
End Sub

Public Property Get ClassName ()
ClassName = "Template"
End Property

Public Property Get Version ()
Version = "1.0"
End Property
Private Function loadfile (ByVal chartype)
Dim Filename, FSO, Hndfile
Filename = M_root
If right (filename, 1) <> "/" and Right (filename, 1) <> "\" Then filename = filename & "/"
filename = Server.MapPath (filename & m_filename)
Set streamobject = Server.CreateObject ("ADODB.stream")
Streamobject.type = 1
Streamobject.mode = 3
Streamobject.open
streamobject.position = 0
Streamobject.loadfromfile Filename
streamobject.position = 0
Streamobject.type = 2
Streamobject.charset = Chartype
LoadFile = Streamobject.readtext ()
If loadfile = "" Then showerror ("0x11<br>could not Load the File" & M_filename & "!")
End Function

Private Sub ShowError (ByVal msg)
M_lasterror = Msg
Response.Write "<span style=" "font-size:12px;color:red" ">error ID:" & msg & "</span>"
If M_haltonerr Then Response.End
End Sub

Public Sub set_root (ByVal Value)
M_root = Value
End Sub
Public Function Get_root ()
Get_root = M_root
End Function
Public Property Let Root (ByVal Value)
Set_root (Value)
End Property
Public Property Get Root ()
Root = M_root
End Property

Public Sub set_file (ByVal handle,byval filename,byval chartype)
M_filename = FileName
M_blocklist.add Handle, LoadFile (Chartype)
End Sub
Public Function Get_file ()
Get_file = M_filename
End Function
Public Sub set_unknowns (ByVal unknowns)
M_unknowns = unknowns
End Sub
Public Function get_unknowns ()
get_unknowns = m_unknowns
End Function
Public Property Let unknowns (ByVal unknown)
M_unknowns = Unknown
End Property
Public Property Get Unknowns ()
unknowns = m_unknowns
End Property

Public Sub Set_block (ByVal Parent, ByVal Blocktag, ByVal Name)
Dim matches
M_regexp.pattern = "<!--\s+begin" & Blocktag & \s+--> ([\s\s.] *) <!--\s+end "& Blocktag &" \s+--> "
If not m_blocklist.exists (parent) Then ShowError ("0x12<br>undefined block Tag" & Parent & "!")
Set matches = M_regexp.execute (M_blocklist.item (Parent))
For the Match in matches
M_blocklist.add Blocktag, match.submatches (0)
M_blocklist.item (parent) = Replace (M_blocklist.item (parent), Match.value, "{" & Name & "}")
Next
Set matches = Nothing
End Sub

Public Sub Set_var (ByVal Name, ByVal Value, ByVal AppEnd)
Dim Val
If IsNull (Value) Then val = "" Else val = Value
If m_valuelist.exists (Name) Then
If AppEnd Then m_valuelist.item (name) = M_valuelist.item (name) & Val _
Else M_valuelist.item (Name) = Val
Else
M_valuelist.add Name, Value
End If
End Sub

Public Sub Unset_var (ByVal Name)
If m_valuelist.exists (name) Then M_valuelist.remove (name)
End Sub

Private Function instancevalue (ByVal blocktag)
Dim Keys, I
Instancevalue = M_blocklist.item (Blocktag)
Keys = M_valuelist.keys
For I=0 to M_valuelist.count-1
Instancevalue = Replace (Instancevalue, "{" & Keys (i) & "}", M_valuelist.item (keys (i))
Next
End Function

Public Sub Parse (ByVal Name, ByVal Blocktag, ByVal AppEnd)
If not m_blocklist.exists (blocktag) Then ShowError ("0x12<br>undefined block Tag" & Parent & "!")
If m_valuelist.exists (Name) Then
If AppEnd Then m_valuelist.item (name) = M_valuelist.item (name) & Instancevalue (Blocktag) _
Else M_valuelist.item (Name) = Instancevalue (Blocktag)
Else
M_valuelist.add Name, Instancevalue (Blocktag)
End If
End Sub

Private Function Finish (ByVal content)
Select Case M_unknowns
Case "Keep" finish = Content
Case "Remove"
M_regexp.pattern = "\{[^ \t\r\n}]+\}"
finish = m_regexp.replace (content, "")
Case "comment"
M_regexp.pattern = "\{([^ \t\r\n}]+) \}"
finish = m_regexp.replace (content, "<!--Template Variable $ Undefined-->")
Case Else finish = content
End Select
End Function

Public Sub output (ByVal Name)
If not m_valuelist.exists (name) Then showerror ("0x13<br>could Don't find Tag" & Name & "!")
Response.Write (M_valuelist.item (Name))
End Sub
End Class


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.