[风雨者]

山高路远,风雨兼程!

用户操作
[即时聊天] [发私信] [加为好友]
伍子ID:letsflytogether
82380次访问,排名1191好友8人,关注者94
一个非计算机专业毕业的,带着对计算机的热爱,如今从事在软件开发领域。我想,我已经证明了专业不应是我们就业的标尺。
除了软件开发,我的爱好还有篮球和美食。
letsflytogether的文章
原创 67 篇
翻译 0 篇
转载 58 篇
评论 117 篇
伍子的公告
    没有比人更高的山,
    没有比脚更长的路。
------------------------
    既然选择了远方,
    便只顾风雨兼程。
------------------------
    失败只有一种,
    就是半途而废。
------------------------
    天下难事必作于易,
    天下大事必作于细。
------------------------
    从用户的角度考虑,
    你就成功了一半。
------------------------
    谁也不能随随便便地成功,
    它来自彻底的自我管理和毅力。
------------------------
月 [下月] [上月]
最近评论
liusping:如果执行一个命令有返回值,这个返回值该如何处理?定义成Command 的一个字段吗?
sap99:www.sap99.com/,SAP99资料多多

SAP免费资料下载
http://www.sap99.com

有很多的学习资料,推荐一下,
cqg1220:机柜
badonly:原来模板技术是这样的!!!谢了~~~
apollosun:恭喜啊

女儿就是宝,享福了啊

我女儿现在虚岁4岁,太可爱了,我每天下班回家的感觉就象过节。
文章分类
收藏
相册
个人风采
精品收藏
文章贴图
个人网站
女儿的博客
我在ITPUB上的博客
我在世界经理人上的博客
技术网站
CSDN
MSDN
UML软件工程组织
中国UI设计网
蓝色理想
软件工程专家网
休闲娱乐
学习网站
ICXO
网页设计师
金桥译港世界通
友情链接
heiyeluren的Blog(RSS)
博客中国上的琴剑楼
小肥羊
汉博园——陈希成
韦子的空中楼阁
资源网站
中国盟动力
太平洋电脑网
存档
软件项目交易
订阅我的博客
XML聚合  FeedSky
订阅到鲜果
订阅到Google
订阅到抓虾
订阅到BlogLines
订阅到Yahoo
订阅到GouGou
订阅到飞鸽
订阅到Rojo
订阅到newsgator
订阅到netvibes

原创 aspTemplate收藏

新一篇: 动态下拉列表框 | 旧一篇: Javascript模板技术

修正了当模板文件为非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...)|编辑

新一篇: 动态下拉列表框 | 旧一篇: Javascript模板技术

评论

#skyblue 发表于2005-07-19 16:29:00  IP: 61.186.252.*
原来那份用着不错。
这份不会用。。用不来的说。。能不能把这份的范例弄完整了?
#楼主 发表于2005-07-28 08:22:00  IP: 61.186.252.*
这份也没有复杂多少,无非就多了一个函数oTpl.set_charset("UTF-8"),默认是GB2312
发表评论  


当前用户设置只有注册用户才能发表评论。如果你没有登录,请点击登录
Csdn Blog version 3.1a
Copyright © 伍子