修正了当模板文件为非gb2312时出现乱码的问题。
改用adodb.stream读取模板文件,增加了函数set_charset(ByVal charset),支持多种编码。
使用时除了要使用该函数指定编码以外,还得为程序文件指定编码,如下:
<%@ CODEPAGE=65001%>
<!--#include file="Template.class.asp" -->
<%
dim oTpl
set oTpl=new Template
oTpl.set_file "fh","/tpl/template.htm"
oTpl.set_charset("UTF-8")
oTpl.set_var "VAR","您好!"
oTpl.pparse "out","fh",false
set oTpl=nothing
%>
<%
'=======================================================================
' file: Template.class.asp ASP页面模板类
' author: 伍子
' date: 2005-04-01
' last modate: 2005-06-06
' website: http://www.54youngor.com/
' email: letsflytogether.com
' reference: phplib,kktTemplate
'=======================================================================
class Template
private m_classname
'/* if set, echo assignments */
private m_debug
'/* $file[handle] = "filename"; */
private m_file
'/* relative filenames are relative to this pathname */
private m_root
'/* $varkeys[key] = "key"; $varvals[key] = "value"; */
private m_varkeys
private m_varvals
'/* "remove" => remove undefined variables
'* "comment" => replace undefined variables with comments
'* "keep" => keep undefined variables
'*/
private m_unknowns
'/* "yes" => halt, "report" => report error, continue, "no" => ignore error quietly */
private m_halt_on_error
'/* last error message is retained here */
private m_last_error
private m_regexp
private m_fso
private m_stream
private m_charset
private sub class_initialize
m_classname="Template"
m_debug=true
set m_file=Server.CreateObject("Scripting.Dictionary")
m_root=Server.MapPath("/")
set m_varkeys=Server.CreateObject("Scripting.Dictionary")
set m_varvals=Server.CreateObject("Scripting.Dictionary")
m_unknowns="remove"
m_halt_on_error="yes"
m_last_error=""
set m_regexp=new RegExp
m_regexp.IgnoreCase = True
m_regexp.Global = True
set m_fso = Server.CreateObject("Scripting.FileSystemObject")
set m_stream = Server.CreateObject("ADODB.Stream")
m_charset="GB2312"
end sub
private sub class_terminate
set m_file=nothing
set m_varkeys=nothing
set m_varvals=nothing
set m_regexp=nothing
set m_fso=nothing
set m_stream=nothing
end sub
'/* public:set_root(pathname root)
' * root:new template directory.
' */
public sub set_root(ByVal root)
if not is_dir(m_root&root&"/") then
halt("set_root:root is not a directory.")
end if
m_root=m_root&root&"/"
end sub
'/* public: set_unknowns(enum $unknowns)
' * unknowns: "remove", "comment", "keep"
' *
' */
public sub set_unknowns(ByVal unknowns)
m_unknowns=unknowns
end sub
'/* public: set_charset(string $charset)
' *
' */
public sub set_charset(ByVal charset)
m_charset=charset
end sub
'/* public: set_file(array $filelist)
' * filelist: array of handle, filename pairs.
' *
' * public: set_file(string $handle, string $filename)
' * handle: handle for a filename,
' * filename: name of template file
' */
public sub set_file(ByVal handle,ByVal file)
if not m_file.Exists(file) then
m_file.Add handle,filename(file)
end if
end sub
'/* public: set_var(array $values)
' * values: array of variable name, value pairs.
' *
' * public: set_var(string $varname, string $value)
' * varname: name of a variable that is to be defined
' * value: value of that variable
' */
public sub set_var(ByVal name,ByVal value)
if not m_varkeys.Exists(name) then
m_varkeys.Add name,varname(name)
end if
if IsNULL(value) then
value=""
end if
if not m_varvals.Exists(name) then
m_varvals.Add name,value
else
m_varvals.Item(name)=value
end if
'response.Write(typename(value))
'response.Write(name & "====" & value &"<br>-----------------------------------------------------<br>")
end sub
'/* public: set_block(string $parent, string $handle, string $name = "")
' * extract the template $handle from $parent,
' * place variable {$name} instead.
' */
public sub set_block(ByVal parent,ByVal handle,ByVal name)
if not loadfile(parent) then
halt("subst: unable to load ."&parent)
end if
if name="" then
name=handle
end if
str=get_var(parent)
m_regexp.Pattern="<!--\s+BEGIN " & handle & "\s+-->([\s\S.]*)<!--\s+END " & handle & "\s+-->"
set matches=m_regexp.Execute(str)
str=m_regexp.Replace(str,"{" & name & "}")
for each match in matches
set_var handle,match.SubMatches(0)
next
set_var parent,str
end sub
'/* public: get_var(string varname)
' * varname: name of variable.
' *
' * public: get_var(array varname)
' * varname: array of variable names
' */
public function get_var(ByVal name)
get_var=m_varvals.Item(name)
end function
'/* public: subst(string $handle)
' * handle: handle of template where variables are to be substituted.
' */
public function subst(ByVal handle)
if not loadfile(handle) then
halt("subst: unable to load " & handle)
end if
str=get_var(handle)
keys=m_varkeys.Keys
for i_i=0 to m_varkeys.Count-1
m_regexp.Pattern=m_varkeys.Item(keys(i_i))
'response.Write(m_varkeys.Item(keys(i_i)))
str=m_regexp.Replace(str,m_varvals.Item(keys(i_i)))
next
subst=str
end function
'/* public: parse(string $target, string $handle, boolean append)
' * public: parse(string $target, array $handle, boolean append)
' * target: handle of variable to generate
' * handle: handle of template to substitute
' * append: append to target handle
' */
public sub parse(ByVal target,ByVal handle,ByVal append)
str=subst(handle)
if append=true then
set_var target,get_var(target) & str
else
set_var target,str
end if
end sub
'/* public: finish(string $str)
' * str: string to finish.
' */
public function finish(ByVal str)
select case m_unknowns
case "keep"
finish=str
case "remove"
m_regexp.pattern ="{[^ \t\r\n}]+}"
finish=m_regexp.Replace(str, "")
case "comment"
m_regexp.pattern = "{[^ \t\r\n}]+}"
finish = m_regexp.Replace(str, "<!-- Template Variable undefined -->")
case else finish = str
end select
end function
'/* public: p(string $varname)
' * varname: name of variable to print.
' */
public sub p(ByVal name)
response.Write(finish(get_var(name)))
end sub
'/* public: p(string $varname)
' * varname: name of variable to print.
' */
public sub pparse(ByVal target,ByVal handle,ByVal append)
parse target,handle,append
p target
end sub
' private *************************************************************
private function is_dir(ByVal path)
is_dir = m_fso.FolderExists(path)
end function
private function file_exists(ByVal file)
file_exists = m_fso.FileExists(file)
end function
'/* private: loadfile(string $handle)
' * handle: load file defined by handle, if it is not loaded yet.
' */
private function loadfile(ByVal handle)
dim str
if not (m_varkeys.Exists(handle) and m_varvals.Item(handle)<>"") then
if m_file.Item(handle)="" then
halt("loadfile:" & handle & " is not a valid handle.")
end if
name=m_file.Item(handle)
if not file_exists(name) then
halt("loadfile:while loading " & handle & ", " & name & " does not exist.")
end if
'set fh = m_fso.OpenTextFile(name)
'str = fh.ReadAll
'if str="" then
' halt("loadfile:while loading " & handle & ", " & name & " is empty.")
'end if
'set fh = nothing
with m_stream
.Type = 2
.Mode = 3
.Open
.LoadFromFile name
if Err.Number<>0 then
halt("loadfile:while loading " & handle & ", " & name & " does not read.")
Err.Clear
response.end
end if
.Charset = m_charset
.Position = 2
str = .ReadText
.Close
if str="" then
halt("loadfile:while loading " & handle & ", " & name & " is empty.")
end if
end with
set_var handle,str
end if
loadfile=true
end function
'/* private: filename($filename)
' * filename: name to be completed.
' */
private function filename(ByVal file)
if not file_exists(m_root&file) then
halt("filename:file "&file&" does not exist.")
end if
filename=m_root&file
end function
'/* private: varname($varname)
' * varname: name of a replacement variable to be protected.
' */
private function varname(ByVal name)
varname="{"&name&"}"
end function
'/* public: halt(string $msg)
' * msg: error message to show.
' */
private sub halt(ByVal msg)
m_last_error=msg
if m_halt_on_error<>"no" then
haltmsg(msg)
end if
if m_halt_on_error="yes" then
response.Write("<b>Halted.</b>")
response.End()
end if
end sub
'/* public, override: haltmsg($msg)
' * msg: error message to show.
' */
private sub haltmsg(ByVal msg)
response.Write("<b>Template Error:</b>"&msg&"<br>")
response.End()
end sub
end class
%>
发表于 @ 2005年06月06日 10:33:00|评论(loading...)|编辑