这次加入了数据库模板功能 🙂
<!– metadata type="typelib" uuid="00000200-0000-0010-8000-00aa006d2ea4" name="ado type library" –>
<%
#######################################################################
## name: asptemplate
## by: bighan
## date: nov. 28, 2003
## site: http://asptemplate.yeah.net/
## email: asptemplate@21cn.com
##
## (c) copyright 2003-2004 bighan
#######################################################################
#######################################################################
## database table: see db/asptemplate.mdb
#######################################################################
class asptemplate
####
## name of this class
## var string
## @access private
## @see property: name
####
private m_strname
####
## version of this class
## var string
## @access private
## @see property: version
####
private m_strversion
####
## determines how much debugging output template will produce.
## this is a bitwise mask of available debug levels:
## 0 = no debugging
## 1 = debug variable assignments
## 2 = debug calls to get variable
## 3 = debug sql
## 4 = debug internals (outputs all function calls with parameters).
##
## @var int
## @access private
## @see property: debug
####
private m_intdebug
####
## template files data type
##
## "db" = database
## "file" = file
##
## @var string
## @access private
## @see property: mode
####
private m_strmode
####
## the base directory from which template files are loaded.
##
## @var string
## @access private
## @see property: root, dir; method: setroot, set_root
####
private m_root
####
## determines how to output variable tags with no assigned value in templates.
##
## @var string
## @access private
## @see property unknowns; method: setunknowns, set_unknowns
####
private m_strunknowns
####
## determines how template handles error conditions.
## "yes" = the error is reported, then execution is halted
## "report" = the error is reported, then execution continues by returning "false"
## "no" = errors are silently ignored, and execution resumes reporting "false"
##
## @var string
## @access private
## @see property ishalt; method: halt
####
private m_strhalterror
####
## the last error message is retained in this variable.
##
## @var string
## @access private
## @see property lasterror
##
private m_strlasterror
####
## opening delimiter (usually "{")
##
## @var string
## @access private
## @see property begintag
####
private m_strbegintag
####
## closing delimiter (usually "}")
##
## @var string
## @access private
## @see private endtag
####
private m_strendtag
####
## a hash of strings forming a translation table which translates variable names
## into names of files containing the variable content.
## m_ofile.item(varname) = "filename";
##
## @var object
## @access private
## @see method: setfile, setfiles, set_file
####
private m_ofile
####
## regular expression object
##
## @var object
## @access private
####
private m_oregexp
####
## a hash of strings forming a translation table which translates variable names
## into regular expressions for themselves.
## m_ovarkeys.item(varname) = "{varname}"
##
## @var object
## @access private
## @see method: setvar, setvars, setappendvar, setappendvars, set_var
####
private m_ovarkeys
####
## a hash of strings forming a translation table which translates variable names
## into values for their respective varkeys.
## m_ovarvals.item(varname) = "value"
##
## @var object
## @access private
## @see method: setvar, setvars, setappendvar, setappendvars, set_var
####
private m_ovarvals
####
## connection object, if this mode = "db" the connection object need.
##
## @var object
## @access private
## @see property: activeconnection, method: opentemplatedatabase, closetemplatedatabase
####
private m_oconn
####
## is native connection object.
##
## @var object
## @access private
## @see property: activeconnection, method: opentemplatedatabase, closetemplatedatabase
####
private m_blnnativeconnection
####
## is open connection object.
##
## @var object
## @access private
## @see property: activeconnection, method: opentemplatedatabase, closetemplatedatabase
####
private m_blnconnectionstate
####
## template database set table name.
##
## @var string
## @access private
## @see property: cattable
####
private m_strcattable
####
## template database data table name.
##
## @var string
## @access private
## @see property: datatable
####
private m_strdatatable
####
## get class name attribute.
##
## usage: otemplate.name
## access public
##
public property get name()
############################################################
name = m_strname
end property
####
## get class version property.
##
## usage: otemplate.version
## access public
##
public property get version()
############################################################
version = m_strversion
end property
####
## get/set m_strmode property.
##
## usage: otemplate.mode = string a_strtype
## access public
##
public property let mode(byval a_strtype)
############################################################
if debug = 4 then response.write "<p><b>mode:</b> a_strtype = " & a_strtype & "</p>" & vbcrlf
a_strtype = lcase(a_strtype)
select case a_strtype
case "file"
m_strmode = "file"
case "db"
m_strmode = "db"
end select
end property
public property get mode()
mode = m_strmode
end property
####
## set m_oconn property.
##
## usage: otemplate.activeconnection = object a_oconn
## access public
##
public property let activeconnection(byref a_oconn)
############################################################
if debug = 3 then response.write "<p><b>activeconnection:</b> use activeconnection</p>" & vbcrlf
if isobject(a_oconn) then
if a_oconn.state <> adstateclosed then
set m_oconn = a_oconn
m_blnconnectionstate = true
m_blnnativeconnection = false
end if
end if
end property
####
## set/get m_strcattable property.
##
## usage: otemplate.cattable = string a_strcattable
## access public
##
public property let cattable(byval a_strcattable)
############################################################
if debug = 3 then response.write "<p><b>cattable:</b> a_strcattable = " & a_strcattable & "</p>" & vbcrlf
m_strcattable = a_strcattable
end property
public property get cattable()
cattable = m_strcattable
end property
####
## set/get m_strdatatable property.
##
## usage: otemplate.datatable = string a_strdatatable
## access public
##
public property let datatable(byval a_strdatatable)
############################################################
if debug = 3 then response.write "<p><b>datatable:</b> a_strdatatable = " & a_strdatatable & "</p>" & vbcrlf
m_strdatatable = a_strdatatable
end property
public property get datatable()
datatable = m_strdatatable
end property
####
## get/set m_intdebug attribute.
##
## usage: otemplate.debug = int a_intdebug
## access public
##
public property let debug(byval a_intdebug)
############################################################
m_intdebug = cint(a_intdebug)
end property
public property get debug()
debug = m_intdebug
end property
####
## sets the policy for dealing with unresolved variable names.
##
## unknowns defines what to do with undefined template variables
## "remove" = remove undefined variables
## "comment" = replace undefined variables with comments
## "keep" = keep undefined variables
##
## note: "comment" can cause unexpected results when the variable tag is embedded
## inside an html tag, for example a tag which is expected to be replaced with a url.
##
## usage: otemplate.unknowns = string a_strunknowns
##
## @param a_strunknowns new value for unknowns
## @see unknowns, setunknowns, set_unknowns
## @access public
##
public property let unknowns(byval a_strunknowns)
############################################################
if debug = 4 then response.write "<p><b>unknowns:</b> unknowns = " & a_strunknowns & "</p>" & vbcrlf
a_strunknowns = lcase(a_strunknowns)
select case a_strunknowns
case "keep"
m_strunknowns = "keep"
case "remove"
m_strunknowns = "remove"
case "comment"
m_strunknowns = "comment"
case else
m_strunknowns = "remove"
end select
end property
public property get unknowns()
unknowns = m_strunknowns
end property
####
## checks that root is a valid directory and if so sets this directory as the
## base directory from which templates are loaded by storing the value in
## root. relative filenames are prepended with the path in root.
##
## usage: otemplate.root = string a_strdir
##
## @param a_root string containing new template directory
## @see m_root, setroot, set_root
## @access public
##
public property let root(byval a_strdir)
############################################################
dim mm_fso, sql, rs, mm_tempdir, num
if debug = 4 then response.write "<p><b>root:</b> root = " & a_strdir & "</p>" & vbcrlf
if len(a_strdir) > 0 then
if mode = "file" then
set mm_fso = createobject("scripting.filesystemobject")
if mm_fso.folderexists(server.mappath(a_strdir)) then
if right(a_strdir, 1) <> "/" then
m_root = a_strdir & "/"
else
m_root = a_strdir
end if
else
call halt("the folder " & a_strdir & " does not exist.")
end if
elseif mode = "db" then
if right(a_strdir, 1) = "/" then a_strdir = left(a_strdir, len(a_strdir) -1)
if left(a_strdir, 1) = "/" then a_strdir = right(a_strdir, len(a_strdir) -1)
mm_tempdir = split(a_strdir, "/")
num = ubound(mm_tempdir)
if num > 0 then a_strdir = mm_tempdir(num)
sql = "select tplcat_id from " & cattable & " where tplcat_name=" & a_strdir &""
set rs = server.createobject("adodb.recordset")
rs.open sql, m_oconn, adopenforwardonly, adlockreadonly, adcmdtext
if not rs.eof then
m_root = rs("tplcat_id")
else
call halt("not find template category " & a_strdir & " from database.")
end if
set rs = nothing
if debug = 3 then response.write "<p><b>root:</b> sql = " & sql & "</p>" & vbcrlf
end if
else
call halt("the folder root does not empty.")
end if
end property
public property get root()
root = m_root
end property
####
##
## alias of root
##
public property let dir(byval a_strdir)
############################################################
root = a_strdir
end property
public property get dir()
dir = root
end property
####
## set/get class m_strhalterror attribute.
##
## "yes" = the error is reported, then execution is halted.
## "no" = errors are silently ignored.
## "report" = the error is reported, then execution continues.
##
## usage: otemplate.ishalt = string a_strhalt
##
## @param a_strhalt new value for m_strhalterror
## @see halt
## @access public
##
public property let ishalt(byval a_strhalt)
############################################################
a_strhalt = lcase(a_strhalt)
select case a_strhalt
case "yes"
m_strhalterror = "yes"
case "no"
m_strhalterror = "no"
case "report"
m_strhalterror = "report"
end select
end property
public property get ishalt()
ishalt = m_strhalterror
end property
####
## set/get class m_strbegintag attribute.
##
## note: dont conflict of html tag
##
## usage: otemplate.begintag = string a_strtag
##
## @param a_strtag new value for m_strbegintag
## @access public
##
public property let begintag(byval a_strtag)
############################################################
if debug = 4 then response.write "<p><b>begintag:</b> begintag = " & a_strtag & "</p>" & vbcrlf
m_strbegintag = a_strtag
end property
public property get begintag()
begintag = m_strbegintag
end property
####
## set/get class m_strendtag attribute.
##
## note: dont conflict of html tag
##
## usage: otemplate.endtag = string a_strtag
##
## @param a_strtag new value for m_strendtag
## @access public
##
public property let endtag(byval a_strtag)
############################################################
if debug = 4 then response.write "<p><b>endtag:</b> endtag = " & a_strtag & "</p>" & vbcrlf
m_strendtag = a_strtag
end property
public property get endtag()
endtag = m_strendtag
end property
####
## get class last error messages.
##
## usage: otemplate.lasterror
##
## @access public
##
public property get lasterror()
############################################################
lasterror = m_strlasterror
end property
####
## open template database connection object. if this mode="db", need first open.
##
## usage: otemplate.opentemplatedatabase string a_strconnstring
##
## @access public
##
public sub opentemplatedatabase(byval a_strconnstring)
############################################################
on error resume next
if debug = 3 then response.write "<p><b>opentemplatedatabase:</b> a_strconnstring = " & a_strconnstring & "</p>" & vbcrlf
if isnull(m_oconn) or not isobject(m_oconn) then
set m_oconn = server.createobject("adodb.connection")
m_oconn.connectionstring = a_strconnstring
m_oconn.open
if err then
err.clear
set m_oconn = nothing
call halt("connection: open connection by string " & a_strconnstring & " error.")
else
m_blnconnectionstate = true
m_blnnativeconnection = true
end if
end if
end sub
####
## close template database connection object.
##
## usage: otemplate.closetemplatedatabase
##
## @access public
##
public sub closetemplatedatabase()
############################################################
if isobject(m_oconn) then
if debug = 3 then response.write "<p><b>closetemplatedatabase:</b> close database … …</p>" & vbcrlf
if m_blnnativeconnection = true then
m_oconn.close
set m_oconn = nothing
else
set m_oconn = nothing
end if
end if
m_blnconnectionstate = false
end sub
####
##
## @see root
##
public sub setroot(byval a_strdir)
############################################################
root = a_strdir
end sub
## @same phplib::template->set_root
public sub set_root(byval a_strdir)
root = a_strdir
end sub
####
##
## @see unknown
##
public sub setunknowns(byval a_strunknowns)
############################################################
unknowns = a_strunknowns
end sub
## @same phplib::template->set_root
public sub set_unknowns(byval a_strunknowns)
unknowns = a_strunknowns
end sub
####
## defines a filename for the initial value of a variable.
##
## it may be passed either a varname and a file name as two strings or
## a hash of strings with the key being the varname and the value
## being the file name.
##
## the new mappings are stored in the object m_ofile.
## the files are not loaded yet, but only when needed.
##
##
## usage: otemplate.setfile a_varname, a_filename
## or
## usage: otemplate.setfile array(a_varname1, a_filename1 _
## ,a_varname2, a_filename2 _
## ,…. …. , ,,,. ,,,, ) _
## , ""
## @see setfiles
## @param a_varname either a string containing a varname or a hash of varname/file name pairs.
## @param a_filename if varname is a string this is the filename otherwise filename is not required
## @access public
##
public sub setfile(byval a_varname, byval a_filename)
############################################################
dim mm_strfiles, num
if not isarray(a_varname) then
if debug = 4 then response.write "<p><b>setfile:</b> (with scalar) varname = "& a_varname &", filename = "& a_filename &"</p>" & vbcrlf
if a_filename = "" then
call halt("setfile: for varname " & a_filename & " filename is empty.")
exit sub
end if
mm_strfiles = filename(a_filename)
m_ofile.add a_varname, mm_strfiles
else
call setfiles(a_varname)
end if
end sub
####
## defines a multi-filename for the initial value of a variable.
##
## usage: otemplate.setfiles array(a_varname1, a_filename1 _
## ,a_varname2, a_filename2 _
## ,…. …. , ,,,. ,,,, )
## @param array a_varname
## @access public
## @see setfile
##
public sub setfiles(byval a_varname)
############################################################
dim i, num
if isarray(a_varname) then
num = ubound(a_varname)
if ((num +1) mod 2) <> 0 then
call halt("setfiles: for varname arrays element not gemination.")
exit sub
else
for i = 0 to num step 2
call setfile(a_varname(i), a_varname(i+1))
next
end if
else
call setfile(a_varname, "")
end if
end sub
## @same phplib::template->set_file
public sub set_file(byval a_varname, byval a_filename)
call setfile(a_varname, a_filename)
end sub
####
## a variable $parent may contain a variable block defined by:
## <!– begin a_varname –> content <!– end a_varname –>. this function removes
## that block from $parent and replaces it with a variable reference named $name.
## the block is inserted into the varkeys and varvals hashes. if a_name is
## omitted, it is assumed to be the same as a_varname.
##
## blocks may be nested but care must be taken to extract the blocks in order
## from the innermost block to the outermost block.
##
## usage: otemplate.setblock string a_parent, string a_parent, string a_name
##
## @param a_parent a string containing the name of the parent variable
## @param a_varname a string containing the name of the block to be extracted
## @param a_name the name of the variable in which to store the block
## @access public
##
public sub setblock(byval a_parent, byval a_varname, byval a_name)
############################################################
dim mm_string, mm_matchstring
if debug = 4 then response.write "<p><b>setblock:</b> parent = " & a_parent & ", varname = " & a_varname & ", name = " & a_name & "</p>" & vbcrlf
if not loadfile(a_parent) then
call halt("setblock: unable to load " & a_parent & ".")
exit sub
end if
if a_name = "" then a_name = a_varname
mm_string = getvar(a_parent)
m_oregexp.ignorecase = true
m_oregexp.global = true
m_oregexp.pattern = "<!–\s+begin\s+(" & a_varname & ")\s+–>([\s\s.]*)<!–\s+end\s+\1\s+–>"
set matches = m_oregexp.execute(mm_string)
for each match in matches
mm_matchstring = match.submatches(1)
mm_string = m_oregexp.replace(mm_string, begintag & a_name & endtag)
call setvar(a_varname,mm_matchstring)
call setvar(a_parent,mm_string)
next
end sub
## @same phplib::template->set_block
public sub set_block(byval a_parent, byval a_varname, byval a_name)
call setblock(a_parent, a_varname, a_name)
end sub
####
## this functions sets the value of a variable.
##
## it may be called with either a varname and a value as two strings or an
## an associative array with the key being the varname and the value being
## the new variable value.
##
## the function inserts the new value of the variable into the $varkeys and
## $varvals hashes. it is not necessary for a variable to exist in these hashes
## before calling this function.
##
## usage: otemplate.setvar string a_varname, string a_value
## or
## usage: otemplate.setvar array( a_varname1, a_value1 _
## ,a_varname2, a_value2 _
## , … , … ) _
## , ""
##
## @param a_varname either a string containing a varname or a hash of varname/value pairs.
## @param a_value if a_varname is a string this contains the new value for the variable otherwise this parameter is ignored
## @access public
##
public sub setvar(byval a_varname, byval a_value)
############################################################
dim mm_varname
if not isarray(a_varname) then
if a_varname <> "" then
if debug = 1 then response.write "<b>setvar:</b> (with scalar) <b>" & a_varname & "</b> = " & server.htmlencode(a_value) & "<br>" & vbcrlf
mm_varname = varname(a_varname)
if m_ovarkeys.exists(a_varname) then
m_ovarkeys.remove a_varname
m_ovarkeys.add a_varname, mm_varname
else
m_ovarkeys.add a_varname, mm_varname
end if
if m_ovarvals.exists(a_varname) then
m_ovarvals.remove a_varname
m_ovarvals.add a_varname, a_value
else
m_ovarvals.add a_varname, a_value
end if
end if
else
call setvars(a_varname)
end if
end sub
####
## usage: otemplate.setvar array( a_varname1, a_value1 _
## ,a_varname2, a_value2 _
## , … , … )
## @param a_varname a hash of varname/value pairs.
## @access public
## @see setvar
##
public sub setvars(byval a_varname)
############################################################
dim i, num
if isarray(a_varname) then
num = ubound(a_varname)
if ((num +1) mod 2) <> 0 then
call halt("setvars: for varname arrays element not gemination.")
exit sub
else
for i = 0 to num step 2
call setvar(a_varname(i), a_varname(i+1))
next
end if
else
call setvar(a_varname, "")
end if
end sub
####
## usage: otemplate.setappendvar string a_varname, string a_value
## or
## usage: otemplate.setappendvar array( a_varname1, a_value1 _
## ,a_varname2, a_value2 _
## , … , … ) _
## , ""
## @param a_varname either a string containing a varname or a hash of varname/value pairs.
## @param a_value if a_varname is a string this contains the new value for the variable otherwise this parameter is ignored
## @access public
## @see setvar
##
public sub setappendvar(byval a_varname, byval a_value)
############################################################
dim mm_varname, mm_string
if not isarray(a_varname) then
if a_varname <> "" then
if debug = 1 then response.write "<b>setappendvar:</b> (with scalar) <b>" & a_varname & "</b> = " & server.htmlencode(a_value) & "<br>" & vbcrlf
mm_varname = varname(a_varname)
if m_ovarkeys.exists(a_varname) then
m_ovarkeys.remove a_varname
m_ovarkeys.add a_varname, mm_varname
else
m_ovarkeys.add a_varname, mm_varname
end if
if m_ovarvals.exists(a_varname) then
mm_string = m_ovarvals.item(a_varname) & a_value
m_ovarvals.remove a_varname
m_ovarvals.add a_varname, mm_string
else
m_ovarvals.add a_varname, a_value
end if
end if
else
call setappendvars(a_varname)
end if
end sub
####
## usage: otemplate.setappendvars array( a_varname1, a_value1 _
## ,a_varname2, a_value2 _
## , … , … )
## @param a_varname a hash of varname/value pairs.
## @access public
## @see setvar
##
public sub setappendvars(byval a_varname)
############################################################
dim i, num
if isarray(a_varname) then
num = ubound(a_varname)
if ((num +1) mod 2) <> 0 then
call halt("setvars: for varname arrays element not gemination.")
exit sub
else
for i = 0 to num step 2
call setappendvar(a_varname(i), a_varname(i+1))
next
end if
else
call setappendvar(a_varname, "")
end if
end sub
####
##
## @same phplib::template->set_var
##
public sub set_var(byval a_varname, byval a_value, byval a_append)
############################################################
if cbool(a_append) = true then
if not isarray(a_varname) then
call setappendvar(a_varname, a_value)
else
call setappendvars(a_varname, a_value)
end if
else
if not isarray(a_varname) then
call setvar(a_varname, a_value)
else
call setvars(a_varname, a_value)
end if
end if
end sub
####
## this function fills in all the variables contained within the variable named
## a_varname. the resulting value is returned as the function result and the
## original value of the variable varname is not changed. the resulting string
## is not "finished", that is, the unresolved variable name policy has not been
## applied yet.
##
## returns: the value of the variable $varname with all variables substituted.
##
## usage: substring(string a_varname)
##
## @param a_varname the name of the variable within which variables are to be substituted
## @access public
## @return string
##
public function substring(byval a_varname)
############################################################
dim mm_string
if debug = 4 then response.write "<p><b>substring:</b> varname = " & a_varname & "</p>" & vbcrlf
if not loadfile(a_varname) then
call halt("substring: unable to load " & a_varname & ".")
end if
mm_string = getvar(a_varname)
m_oregexp.ignorecase = true
m_oregexp.global = true
m_oregexp.pattern = "(" & begintag & ")([^ \t\r\n" & endtag &"]+)" & endtag
set matches = m_oregexp.execute(mm_string)
for each match in matches
if m_ovarvals.exists(match.submatches(1)) then
m_oregexp.pattern = match.value
mm_string = m_oregexp.replace(mm_string, m_ovarvals.item(match.submatches(1)))
end if
next
substring = mm_string
end function
####
##
## @same phplib::template->subst
##
public function subst(byval a_varname)
subst = substring(a_varname)
end function
####
## this is shorthand for print substring(a_varname). see substring for further
## details.
##
## usage: otemplate.writesubstring string a_varname
##
## @param a_varname the name of the variable within which variables are to be substituted
## @access public
## @see substring
##
public sub writesubstring(byval a_varname)
############################################################
if debug = 4 then response.write "<p><b>writesubstring:</b> varname = " & a_varname & "</p>" & vbcrlf
response.write substring(a_varname)
end sub
####
##
## @same phplib::template->psubst
##
public sub psubst(byval a_varname)
call writesubstring(a_varname)
end sub
####
## the function substitutes the values of all defined variables in the variable
## named a_varname and stores or appends the result in the variable named a_target.
##
## it may be called with either a target and a varname as two strings or a
## target as a string and an array of variable names in varname.
##
## the function inserts the new value of the variable into the ovarveys and
## $varvals hashes. it is not necessary for a variable to exist in these hashes
## before calling this function.
##
## an optional third parameter allows the value for each varname to be appended
## to the existing target variable instead of replacing it. the default is to
## replace.
##
## if a_target and a_varname are both strings, the substituted value of the
## variable a_varname is inserted into or appended to a_target.
##
## returns: the last value assigned to a_target.
##
## usage: otemplate.parse string a_target, string a_varname, boolean a_append
## usage: string = otemplate.parse( string a_target, string a_varname, boolean a_append )
## or
## usage: otemplate.parse string a_target, array(a_varname1, a_varname2, …) , boolean a_append
## usage: string = otemplate.parse( string a_target, array(a_varname1, a_varname2, …), boolean a_append)
##
## @param a_target a string containing the name of the variable into which substituted $varnames are to be stored
## @param a_varname if a string, the name the name of the variable to substitute or if an array a list of variables to be substituted
## @param a_append if true, the substituted variables are appended to $target otherwise the existing value of $target is replaced
## @access public
## @return string
## @see substring
##
## @same phplib::template->pparse
##
public function parse(byval a_target, byval a_varname, byval a_append)
############################################################
dim mm_string, i, num
if not isarray(a_varname) then
if debug = 4 then response.write "<p><b>parse:</b> (with scalar) target = " & a_target & ", varname = " & a_varname & ", append = " & a_append & "</p>" & vbcrlf
mm_string = substring(a_varname)
if a_append = true then
mm_string = getvar(a_target) & mm_string
call setvar(a_target, mm_string)
else
call setvar(a_target, mm_string)
end if
else
num = ubound(a_varname)
for i = 0 to num
if debug = 4 then response.write "<p><b>parse:</b> (with array) target = " & a_target & ", varname = " & a_varname(i) & ", append = " & a_append & "</p>" & vbcrlf
mm_string = substring(a_varname(i))
if a_append = true then
mm_string = getvar(a_target) & mm_string
call setvar(a_target, mm_string)
else
call setvar(a_target, mm_string)
end if
next
end if
if debug = 4 then response.write "<p><b>parse:</b> completed</p>" & vbcrlf
parse = mm_string
end function
####
## this is shorthand for print parse(…) and is functionally identical.
## see parse for further details.
##
## returns: always returns void.
##
## usage: otemplate.write string a_target, string a_varname
##
## @param a_target a string containing the name of the variable into which substituted $varnames are to be stored
## @param a_varname if a string, the name the name of the variable to substitute or if an array a list of variables to be substituted
## @access public
## @return void
## @see parse
##
public sub write(byval a_target, byval a_varname)
############################################################
dim mm_string
if debug = 4 then response.write "<p><b>write:</b> passing parameters to parse…</p>" & vbcrlf
mm_string = parse(a_target, a_varname, false)
mm_string = finish(mm_string)
response.write mm_string
end sub
####
##
## @see write
##
public sub appendwrite(byval a_target, byval a_varname)
############################################################
dim mm_string
if debug = 4 then response.write "<p><b>write:</b> passing parameters to parse…</p>" & vbcrlf
mm_string = parse(a_target, a_varname, true)
mm_string = finish(mm_string)
response.write mm_string
end sub
####
##
## @same phplib::template->pparse
##
public sub pparse(byval a_target, byval a_varname, byval a_append)
############################################################
if cbool(a_append) = true then
call appendwrite(a_target, a_varname)
else
call write(a_target, a_varname)
end if
end sub
####
## this function returns an associative object of all defined variables with the
## name as the key and the value of the variable as the value.
##
## this is mostly useful for debugging. also note that $this->debug can be used
## to echo all variable assignments as they occur and to trace execution.
##
## returns: a hash of all defined variable values keyed by their names.
##
## usage: otemplate.get_vars()
##
## @access public
## @return object
##
public function getvars()
############################################################
if debug = 4 then response.write "<p><b>getvars:</b> constructing dictionary of vars…</p>" & vbcrlf
set getvars = m_ovarvals
end function
####
##
## @same phplib::template->get_vars
##
public function get_vars()
set get_vars = getvars()
end function
####
## this function returns the value of the variable named by a_varname.
## if a_varname references a file and that file has not been loaded yet, the
## variable will be reported as empty.
##
## when called with an array of variable names this function will return a a
## hash of variable values keyed by their names.
##
## returns: a string or an array containing the value of $varname.
##
## usage: getvar(string a_varname)
## or
## usage: getvar(array a_varname)
##
## @param a_varname if a string, the name the name of the variable to get the value of, or if an array a list of variables to return the value of
## @access public
## @return string or object
##
public function getvar(byval a_varname)
############################################################
dim mm_string, mm_ovars, i, num
if not isarray(a_varname) then
mm_string = ""
if a_varname <> "" then
if m_ovarvals.exists(a_varname) then
mm_string = m_ovarvals.item(a_varname)
end if
end if
if debug = 2 then response.write "<b>getvar:</b> (with scalar) <b>" & a_varname & "</b> = " & server.htmlencode(mm_string) & "<br>" & vbcrlf
getvar = mm_string
else
set mm_ovars = createobject("scripting.dictionary")
num = ubound(a_varname)
for i=0 to num
if m_ovarvals.exists(a_varname(i)) then
mm_string = m_ovarvals.item(a_varname(i))
mm_ovars.add a_varname(i), mm_string
end if
if debug = 2 then response.write "<b>getvar:</b> (with array) <b>" & a_varname(i) & "</b> = " & server.htmlencode(mm_string) & "<br>" & vbcrlf
next
set getvar = mm_ovars
end if
end function
####
##
## @same phplib::template->get_var
##
public function get_var(byval a_varname)
if not isarray(a_varname) then
get_var = getvar(a_varname)
else
set get_var = getvar(a_varname)
end if
end function
####
## this functions clears the value of a variable.
##
## it may be called with either a varname as a string or an array with the
## values being the varnames to be cleared.
##
## the function sets the value of the variable in the ovarkeys and ovarvals
## hashes to "". it is not necessary for a variable to exist in these hashes
## before calling this function.
##
##
## usage: otemplate.clearvar string a_varname
## or
## usage: otemplate.clearvar array (a_varname1, a_varname2, …)
##
## @param $varname either a string containing a varname or an array of varnames.
## @access public
## @return void
##
public sub clearvar(byval a_varname)
############################################################
dim i, num
if not isarray(a_varname) then
if a_varname <> "" then
if debug = 1 then response.write "<b>clear_var:</b> (with scalar) <b>" & a_varname & "</b><br>" & vbcrlf
call setvar(a_varname, "")
end if
else
num = ubound(a_varname)
for i=0 to num
if debug = 1 then response.write "<b>clear_var:</b> (with array) <b>" & a_varname(i) & "</b><br>" & vbcrlf
call setvar(a_varname(i), "")
next
end if
end sub
####
##
## @same phplib::template->clear_var
##
public sub clear_var(byval a_varname)
call clearvar(a_varname)
end sub
####
## this functions unsets a variable completely.
##
## it may be called with either a varname as a string or an array with the
## values being the varnames to be cleared.
##
## the function removes the variable from the ovarkeys and ovarvals hashes.
## it is not necessary for a variable to exist in these hashes before calling
## this function.
##
##
## usage: otemplate.unsetvar string a_varname
## or
## usage: otemplate.unsetvar array(a_varname1, a_varname2, …)
##
## @param a_varname either a string containing a varname or an array of varnames.
## @access public
##
public sub unsetvar(byval a_varname)
############################################################
dim i, num
if not isarray(a_varname) then
if a_varname <> "" then
if debug = 1 then response.write "<b>unsetvar:</b> (with scalar) <b>" & a_varname & "</b><br>" & vbcrlf
if m_ovarkeys.exists(a_varname) then
m_ovarkeys.remove a_varname
end if
if m_ovarvals.exists(a_varname) then
m_ovarvals.remove a_varname
end if
end if
else
num = ubound(a_varname)
for i=0 to num
if a_varname(i) <> "" then
if debug = 1 then response.write "<b>unsetvar:</b> (with array) <b>" & a_varname & "</b><br>" & vbcrlf
if m_ovarkeys.exists(a_varname(i)) then
m_ovarkeys.remove a_varname(i)
end if
if m_ovarvals.exists(a_varname(i)) then
m_ovarvals.remove a_varname(i)
end if
end if
next
end if
end sub
####
##
## @same phplib::template->unset_var
##
public sub unset_var(byval a_varname)
call unsetvar(a_varname)
end sub
####
## this function returns a hash of unresolved variable names in a_varname, keyed
## by their names.
##
## returns: a hash of varname/varname pairs or false on error.
##
## usage: getundefined(string a_varname)
##
## @param a_varname a string containing the name the name of the variable to scan for unresolved variables
## @access public
## @return array
##
public function getundefined(byval a_varname)
############################################################
dim mm_string, mm_result
if debug = 4 then response.write "<p><b>getundefined:</b> varname = " & a_varname & "</p>" & vbcrlf
if not loadfile(a_varname) then
call halt("get_undefined: unable to load " & a_varname & ".")
getundefined = false
exit function
end if
mm_string = getvar(a_varname)
set mm_result = createobject("scripting.dictionary")
m_oregexp.ignorecase = true
m_oregexp.global = true
m_oregexp.pattern = "(" & begintag & ")([^ \t\r\n" & endtag &"]+)" & endtag
set matches = m_oregexp.execute(mm_string)
i = 0
for each match in matches
if not m_ovarvals.exists(match.submatches(1)) then
if debug = 4 then response.write "<p><b>get_undefined:</b> undefined: " & submatches(1) & "</p>" & vbcrlf
mm_result.add match.submatches(1), match.submatches(1)
mm_result(i) = match.submatches(1)
i = i + 1
end if
next
if mm_result.count > 0 then
set getundefined = mm_result
if isarray(mm_result) then
getundefined = mm_result
else
getundefined = false
end if
end function
####
##
## @same phplib::template->get_undefined
##
public function get_undefined(byval a_varname)
############################################################
get_undefined = getundefined
end function
####
## this function returns the finished version of $str. that is, the policy
## regarding unresolved variable names will be applied to $str.
##
## returns: a finished string derived from a_string and unknowns.
##
## usage: finish(string a_string)
##
## @param a_string a string to which to apply the unresolved variable policy
## @access public
## @return string
## @see unknowns, setunknowns, set_unknowns
##
public function finish(byval a_string)
############################################################
dim mm_string
select case unknowns
case "keep"
mm_string = a_string
case "remove"
m_oregexp.ignorecase = true
m_oregexp.global = true
m_oregexp.pattern = "(" & begintag & ")([^ \t\r\n" & endtag &"]+)" & endtag
mm_string = m_oregexp.replace(a_string, "")
case "comment"
m_oregexp.ignorecase = true
m_oregexp.global = true
m_oregexp.pattern = "(" & begintag & ")([^ \t\r\n" & endtag &"]+)" & endtag
set matches = m_oregexp.execute(a_string)
for each match in matches
mm_string = m_oregexp.replace(a_string, "<!– template variable " & match.submatches(1) &" undefined –>")
next
end select
finish = mm_string
end function
####
## this function returns the finished version of the value of the variable named
## by $varname. that is, the policy regarding unresolved variable names will be
## applied to the variable a_varname and the result returned.
##
## returns: a finished string derived from the variable a_varname.
##
## usage: otemplate.getvariable(string a_varname)
##
## @param a_varname a string containing the name of the variable to finish
## @access public
## @return string
## @see setunknowns
## @see finish
##
public function getvariable(byval a_varname)
############################################################
getvariable = finish(getvar(a_varname))
end function
public function get(byval a_varname)
冲突不支持
end function
####
## this function prints the finished version of the value of the variable named
## by $varname. that is, the policy regarding unresolved variable names will be
## applied to the variable a_varname then it will be printed.
##
## usage: otemplate.writevariable string a_varname
##
## @param a_varname a string containing the name of the variable to finish and print
## @access public
## @see setunknowns
## @see finish
##
public sub writevariable(byval a_varname)
############################################################
response.write finish(getval(a_varname))
end sub
####
##
## @see writevariable
## @same phplib::template->p
##
public sub p(byval a_varname)
call writevariable(a_varname)
end sub
####
## when called with a relative pathname, this function will return the pathname
## with root prepended. absolute pathnames are returned unchanged.
##
## returns: a string containing an absolute pathname.
##
## usage: filename(string a_filename)
##
## @param a_filename a string containing a filename
## @access private
## @return string
## @see root, setroot
##
## @same phplib::template->filename
##
private function filename(byval a_filename)
############################################################
dim mm_fso, mm_filename, mm_tempfilename, rs, sql
if debug = 4 then response.write "<p><b>filename:</b> filename = " & a_filename & "</p>" & vbcrlf
if mode = "file" then
set mm_fso = createobject("scripting.filesystemobject")
if left(a_filename, 1) = "/" then
a_filename = right(a_filename, len(a_filename) – 1)
end if
a_filename = root & a_filename
a_filename = server.mappath(a_filename)
if not mm_fso.fileexists(a_filename) then
call halt("filename: file " & a_filename & " does not exist.")
else
mm_filename = a_filename
end if
elseif mode = "db" then
a_filename = split(a_filename, ".")
mm_tempfilename = a_filename(0)
sql = "select tpldata_id from " & datatable & " where tplcat_id =" & root &" and tpldata_name=" & mm_tempfilename &""
set rs = server.createobject("adodb.recordset")
rs.open sql, m_oconn, adopenforwardonly, adlockreadonly, adcmdtext
if rs.eof then
call halt("filename: file " & mm_tempfilename & " does not exist.")
else
mm_filename = rs("tpldata_id")
end if
set rs = nothing
if debug = 3 then response.write "<p><b>filename:</b> sql = " & sql & "</p>" & vbcrlf
end if
filename = mm_filename
end function
####
## if a variables value is undefined and the variable has a filename stored in
## ofile.item(a_varname) then the backing file will be loaded and the files
## contents will be assigned as the variables value.
##
## note that the behaviour of this function changed slightly after the 7.2d
## release. where previously a variable was reloaded from file if the value
## was empty, now this is not done. this allows a variable to be loaded then
## set to "", and also prevents attempts to load empty variables. files are
## now only loaded if ovarvals.item(a_varname) is unset.
##
## returns: true on success, false on error.
##
## usage: loadfile(string a_varname)
##
## @param a_varname a string containing the name of a variable to load
## @access private
## @return boolean
## @see setfile, setfiles
##
## @same phplib::template->loadfile
##
private function loadfile(byval a_varname)
############################################################
dim mm_fso, mm_ofile, mm_filename, mm_filesting, mm_bool
if debug = 4 then response.write "<p><b>loadfile:</b> varname = " & a_varname & "</p>" & vbcrlf
mm_bool = true
if not m_ofile.exists(a_varname) then
loadfile = mm_bool
if debug = 4 then response.write "<p><b>loadfile:</b> varname " & a_varname & " does not reference a file</p>" & vbcrlf
exit function
end if
if m_ovarvals.exists(a_varname) then
loadfile = mm_bool
if debug = 4 then response.write "<p><b>loadfile:</b> varname " & a_varname & " is already loaded</p>" & vbcrlf
exit function
end if
mm_filename = m_ofile.item(a_varname)
if mode = "file" then
set mm_fso = createobject("scripting.filesystemobject")
set mm_ofile = mm_fso.opentextfile(mm_filename)
mm_filesting = mm_ofile.readall
mm_filesting = trim(mm_filesting)
if mm_filesting = "" then
mm_bool = false
call halt("loadfile: while loading " & a_varname & ", " & mm_filename & " does not exist or is empty.")
else
if debug = 4 then response.write "<b>loadfile:</b> loaded " & mm_filename & " into " & a_varname & "<br>" & vbcrlf
call setvar(a_varname, mm_filesting)
end if
mm_ofile.close
set mm_ofile = nothing
set fso = nothing
elseif mode = "db" then
sql = "select tpldata_text from " & datatable & " where tpldata_id =" & mm_filename
set rs = server.createobject("adodb.recordset")
rs.open sql, m_oconn, adopenforwardonly, adlockreadonly, adcmdtext
if rs.eof then
mm_bool = false
call halt("filename: file " & mm_tempfilename & " does not exist.")
else
mm_filesting = rs("tpldata_text")
call setvar(a_varname, mm_filesting)
end if
set rs = nothing
if debug = 3 then response.write "<p><b>loadfile:</b> sql = " & sql & "</p>" & vbcrlf
end if
loadfile = mm_bool
end function
####
## this function will construct a regexp for a given variable name with any
## special chars quoted.
##
## returns: a string containing an escaped variable name.
##
## usage: varname(string a_varname)
##
## @param a_varname a string containing a variable name
## @access private
## @return string
## @same phplib::template->varname
##
private function varname(byval a_varname)
############################################################
varname = begintag & a_varname & endtag
end function
####
## this function is called whenever an error occurs and will handle the error
## according to the policy defined in ishalt. additionally the
## error message will be saved in m_strlasterror.
##
## returns: always returns false.
##
## usage: halt(string a_message)
##
## @param $msg a string containing an error message
## @access private
## @return void
## @see ishalt
##
private sub halt(byval a_message)
############################################################
m_strlasterror = a_message
if ishalt <> "no" then call haltmsg(a_message)
if ishalt = "yes" then
response.write "<b>halted.</b>"
response.end
end if
end sub
####
## this function prints an error message.
## it can be overridden by your subclass of template. it will be called with an
## error message to display.
##
## usage: haltmsg(string a_message)
##
## @param a_message a string containing the error message to display
## @access public
## @return void
## @see halt
##
public sub haltmsg(byval a_message)
############################################################
response.write "<b>template error:</b>" & a_message & "<br>"
end sub
####
## class constructor, set class default attributes, you can change it
## @see property let debug
## @see property let mode
## @see property let cattable
## @see property let datatable
## @see property let unknown
## @see property let ishalt
## @see property let begintag
## @see property let endtag
####
private sub class_initialize
debug = 0
mode = "file"
cattable = "tplcat"
datatable = "tpldata"
unknowns = "remove"
ishalt = "yes"
m_strlasterror = ""
begintag = "{"
endtag = "}"
m_root = "templates/"
set m_ofile = createobject("scripting.dictionary")
set m_ovarkeys = createobject("scripting.dictionary")
set m_ovarvals = createobject("scripting.dictionary")
set m_oregexp = new regexp
m_blnconnectionstate = false
m_strname = "asptemplate"
m_strversion = "2.0.0"
if debug = 4 then response.write "<p><b>template:</b> root = " & m_root & ", unknowns = " & unknowns & "</p>" & vbcrlf
end sub
####
## class destructor, free memory.
####
private sub class_terminate
set m_ofile = nothing
set m_ovarkeys = nothing
set m_ovarvals = nothing
set m_oregexp = nothing
call closetemplatedatabase()
end sub
end class
%>