- Код: Выделить всё
Class ParserEngine
Public tag_open ' Open tag of template replace target
Public tag_close ' Close tag of template replace target
Public temp_folder ' Folder with templates
Public temp_file ' Template file
Public parse_debug ' debug info on / off
Private temp_text ' Template source
Private replace_vars ' Strings and ReplaceStrings of template
Private parse_start ' debug info - parser start time
Private parse_end ' debug info - parser end time
' Parse.Init - execute
Public function Init()
' Check properties
if NOT exists(Me.OpenTag) then
tag_open = "%"
end if
if NOT exists(Me.CloseTag) then
tag_close = tag_open
end if
if NOT exists(Me.TempFolder) then
temp_folder = Server.MapPath("\") & "/templates/"
end if
if NOT exists(Me.TempFile) then
temp_file = "default.htm"
end if
if NOT exists(Me.Debug) then
debug = false
end if
' Lets' go
parse_start = Now()
Me.Get_Template()
End function
' Parser.OpenTag = "%" (default)
Public Property Get OpenTag()
OpenTag = tag_open
End Property
Public Property Let OpenTag(oData)
tag_open = oData
End Property
' Parser.CloseTag = "%" (default)
Public Property Get CloseTag()
CloseTag = tag_close
End Property
Public Property Let CloseTag(oData)
tag_close = oData
End Property
' Parser.TempFolder = "C:\Inetpub\wwwroot\templates\"
Public Property Get TempFolder()
TempFolder = temp_folder
End Property
Public Property Let TempFolder(oData)
temp_folder = oData
End Property
' Parser.TempFile = "default.htm"
Public Property Get TempFile()
TempFile = temp_file
End Property
Public Property Let TempFile(oData)
temp_file = oData
End Property
' Parser.Debug = true OR false
Public Property Get Debug()
Debug = parse_debug
End Property
Public Property Let Debug(oData)
parse_debug = oData
End Property
' debug info parse_time ReadOnly
Public Property Get ParseTime()
ParseTime = DateDiff("s",parse_start,parse_end) ' in seconds
End Property
' Parser.PreReplace "PARSERABOUT", "This is my Parser class. Originally written by BuG (bbbug@newmail.ru)"
' Parser.PreReplace "CURRENTTIME", FormatDateTime(Now(),3)
' Parser.PreReplace "...", "..."
' Parser.PreReplace strTOreplace, strFORreplace
Public function PreReplace(replace_var, replace_value)
On Error Resume Next
if NOT isObject(replace_vars) then
Set replace_vars = Server.CreateObject("Scripting.Dictionary")
end if
replace_vars.Add replace_var, replace_value
if Err then
Response.Write("Ошибка парсера! Невозможно добавить переменную для замены!<br>")
Response.Write("Err: " & Err.Description & "<br>")
end if
End function
' Check existense of template file in FileSystemObject
Public function Get_template()
Dim oFS, temp_path
temp_path = temp_folder & temp_file
Set oFS = Server.CreateObject("Scripting.FileSystemObject")
if oFS.FileExists(temp_path) then
Set temp_file = oFS.OpenTextFile(temp_path, ForReading)
Parse_Template(temp_file.ReadAll)
else
Response.Write("<b style=""color:red;"">Ошибка парсера! Файл не существует ("&temp_path&")</b><br>")
end if
Set temp_file = Nothing
Set oFS = Nothing
End function
' Main function to parse template (simple)
Private Function Parse_Template(temp_text)
On Error Resume Next
' Replace strTOreplace with strFORreplace
For each i in replace_vars.Keys
temp_text = Replace(temp_text, tag_open & UCase(i) & tag_close, replace_vars(i))
Next
' Document is ready, write it out.
Response.Write(temp_text)
' Check for errors of parsing
if Err then
Response.Write( chr(13) & "<!-- parse error! -->" & chr(13))
Response.Write("<!-- Err: 0x" & Hex(Err.Number) & " -->" & chr(13))
Response.Write("<!-- Description: " & Err.Description & " -->" & chr(13))
Response.Write("<!-- Source: " & Err.Source & " -->" & chr(13))
Response.Write("<!-- Help: " & Err.HelpFile & " - " & Err.HelpContext & " -->" & chr(13))
else
' debug info
if parse_debug then
Response.Write( chr(13) & "<!-- parse ok! -->" & chr(13))
Response.Write("<!-- generation time: " & DateDiff("s",parse_start,Now()) & " s. -->" & chr(13))
end if
end if
parse_end = Now()
Me.Release ' Освободим парсер после вывода результатов
End Function
' Remove all cashed info
Public Function Release()
replace_vars.RemoveAllKeys
temp_text = ""
End function
End Class ' Parser class
Это класс-парсер HTML-шаблонов. Если использовать для замены каких то строк, функции или процедуры, возвращающие значения строкового типа, то можно и динамический контент цеплять. Можно класс использовать внутри нового класса, т.е. вот так
- Код: Выделить всё
Class HTML
Function Head(part)
' Check style
if NOT exists(Session.Contents("style")) then
Session("style") = "default.css"
end if
' Check part
if NOT exists(part) then
part = "blank"
end if
' Check logo
Dim logo
if NOT exists(Session("logo")) AND NOT exists(Session("p_id")) then
logo = "blank.gif"
else
logo = Session("logo")
end if
' Let's start Parser of templates
Parse.TempFile = part & "_header.htm"
Parse.PreReplace "TITLE", title
Parse.PreReplace "STYLE", Session("style")
select case part
case "closed", "blue"
Parse.PreReplace "LOGO", "<img src=""/blue/" & logo & """>"
Parse.PreReplace "ALIAS", Session("alias")
Parse.PreReplace "USERNAME", Session("username")
Parse.PreReplace "MENU", Draw.menu() ' function returns menu for specified user
Parse.PreReplace "NEWS", Draw.LastNews()
Parse.PreReplace "MAINCONTROLS", Draw.MainControls()
case "public"
Parse.PreReplace "MENU", Draw.PublicMenu()
Parse.PreReplace "TODAY", FormatDateTime(Date(),2)
Parse.PreReplace "COUNTER", count_show()
end select
Parse.Init()
End Function 'end of head function
Function Foot(part)
'if NOT exists()
Parse.TempFile = part & "_footer.htm"
Parse.PreReplace "SCRIPTTIME", DateDiff("s",starttime,Now())
select case part
case "closed"
case "blue"
Parse.PreReplace "YEAR", Year(Now())
case "public"
case "blank"
end select
Parse.Init()
End Function 'end of draw.foot
End Class