• 3193阅读
  • 0回复

ASP模板技术 [复制链接]

上一主题 下一主题
离线Angel
 
只看楼主 倒序阅读 0楼 发表于: 2005-04-04

<!--         Template.class.asp         -->
<%
'=======================================================================
' file:     Template.class.asp ASP页面模板类
' author:     伍子
' date:     2005-04-01
' 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 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")
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
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_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 not m_varvals.Exists(name) then
  m_varvals.Add name,value
else
  m_varvals.Item(name)=value
end if
'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))
  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)
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
  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
%>


<!--           main.htm           -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head>

<body>
{HEAD}<br>
<p>{CONTENT}</p>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<!-- BEGIN BROW -->
<tr>
  <!-- BEGIN BCOL -->
  <td>{NUMBER}</td>
  <!-- END BCOL -->
</tr>
<!-- END BROW -->
</table>
<p>{FOOT}</p>
</body>
</html>


<!--           head.htm           -->
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
  <td align="center">欢迎您,{NAME}</td>
</tr>
</table>


<!--           foot.htm           -->
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
  <td align="center">版权所有:{COMPANY}</td>
</tr>
</table>


<!--           test.asp           -->
<!--#include file="library/Template.class.asp"-->
<%
dim tpl
set tpl = new Template
'tpl.set_root("tpl")
'tpl.set_unknowns("comment")
tpl.set_file "fh","template/main.htm"
tpl.set_file "headh","template/head.htm"
tpl.set_file "footh","template/foot.htm"
tpl.set_block "fh","BROW","row"
tpl.set_block "BROW","BCOL","col"
tpl.set_var "NAME","朋友"
tpl.set_var "CONTENT","ASP模板技术,支持模板嵌套,块操作"
tpl.set_var "COMPANY","youngor-studio"
for i=0 to 10
tpl.set_var "col",""
for j=0 to 10
  tpl.set_var "NUMBER",CStr(i)&CStr(j)
  tpl.parse "col","BCOL",true
next
tpl.parse "row","BROW",true
next
tpl.parse "HEAD","headh",false
tpl.parse "FOOT","footh",false
tpl.pparse "out","fh",false
set tpl=nothing
%>
A.n.g.e.l
快速回复
限100 字节
 
上一个 下一个