MVC 模式在网站架构中十分常见。它允许我们建立一个三层结构的应用程式,从代码中分离出有用的层,帮助设计师和开发者协同工作以及提高我们维护和扩展既有程式的能力。
PHP 中有一个很著名的类库 phpLib,其中有 Template 模板类。能够很方便地实现代码分离在 ASP 中是否也可以这样做呢?当然可以,这就是 aspTemplate 的初衷。它完全实现了 phpLib Template 的全部功能,你可以象使用 phpLib Template 一样使用它,连习惯也基本不用改。:)
<%
'#######################################################################
'## NAME: aspTemplate
'## BY: BigHan
'## DATE: Nov. 28, 2003
'## SITE: http://aspTemplate.yeah.net/
'## EMAIL: aspTemplate@21cn.com
'##
'## (C) Copyright 2003-2004 bighan
'#######################################################################
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
'## 4 = debug internals (outputs all function calls with parameters).
'##
'## @var int
'## @access Private
'## @see property: Debug
'####
Private m_intDebug
'####
'## The base directory from which template files are loaded.
'##
'## @var string
'## @access private
'## @see property: Root, Dir; method: SetRoot, set_root
'####
Private m_strRoot
'####
'## Determines how to output variable tags with no assigned value in templates.
'##
'## @var string
'## @access private
'## @see property Unknown; 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
'####
'## get class name attribute.
'##
'## usage: oTemplate.Name
'## access public
'##
Public Property Get Name()
'############################################################
Name = m_strName
End Property
'####
'## get class version attribute.
'##
'## usage: oTemplate.Version
'## access public
'##
Public Property Get Version()
'############################################################
Version = m_strVersion
End Property
'####
'## get/set m_intDebug attribute.
'##
'## usage: oTemplate.Debug = 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.Unknown = A_unknowns
'##
'## @param A_unknowns new value for unknowns
'## @see unknowns, SetUnknowns, set_unknowns
'## @access public
'##
Public Property Let Unknown(ByVal A_unknowns)
'############################################################
If Debug = 4 Then Response.Write "<p><b>Unknown:</b> unknown = " & A_unknowns & "</p>" & VbCrLf
A_unknowns = LCase(A_unknowns)
Select Case A_unknowns
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 Unknown()
Unknown = 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 = A_root
'##
'## @param A_root string containing new template directory
'## @see m_strRoot, SetRoot, set_root
'## @access public
'##
Public Property Let Root(ByVal A_root)
'############################################################
If Debug = 4 Then Response.Write "<p><b>Root:</b> root = " & A_root & "</p>" & VbCrLf
Dim MM_FSO
If Len(A_root) > 0 Then
Set MM_FSO = CreateObject("Scripting.FileSystemObject")
If MM_FSO.FolderExists(Server.MapPath(A_root)) Then
If Right(A_root, 1) <> "/" Then