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 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 each 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 $1 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 not find tag " & name & "!") response.write(finish(m_valuelist.item(name))) end sub end class