gusucode.com > 深度学习(asp)网址导航 v4.0.1 > 深度学习(asp)网址导航 v4.0.1\code\include\Deep.Class.Template.asp
<% '┌──────────────────── 深度空间 深度学习 ──┐ '│深度学习(asp)模版类 (ASP Template Engine) '│Version : 0.3.1 '│作者:吕海鹏 www.deepstudy.cn '│建立时间:2009-6-23 最后修改时间:2010-2-15 '└──────────────────── www.deepstudy.cn ──┘ class Deep_Template private VERSION public left_delimiter '左结束符变量 public right_delimiter '右结束符变量 public templates_dir '模板目录变量 public template '模版内容变量 private oRegEx '正则 private left_delimiter_regex '左结束符变量正则 private right_delimiter_regex '右结束符变量正则 ' --------------------------- ' 构造函数 初始化 Private Sub Class_Initialize VERSION = "深度学习(asp)模版类 ver0.3.1" left_delimiter ="{$" right_delimiter="}" templates_dir = "templates/" template="" Set oRegEx = New RegExp ' 建立正则表达式。 'oRegEx.Pattern = "\{\$"+labelName+"\(.*?\)\}" ' 设置得到{$}里参数正则 oRegEx.IgnoreCase = True ' 设置是否区分字符大小写。 oRegEx.Global = True ' 设置全局可用性。 left_delimiter_regex=charToRegexp(left_delimiter) right_delimiter_regex=charToRegexp(right_delimiter) End Sub ' --------------------------- ' 析构函数 Private Sub Class_Terminate Set oRegEx = Nothing End Sub ' --------------------------- '返回 读取模版文件内容 public function readTemplateFile(inFileName) dim oFSO,oFile if len(inFileName) > 0 then set oFSO = server.CreateObject("Scripting.FileSystemObject") 'response.write server.mappath(p_templates_dir & inFileName) if oFSO.FileExists(server.mappath(templates_dir & inFileName)) then set oFile = oFSO.OpenTextFile(server.mappath(templates_dir & inFileName), 1) '1 只读 2只写 template = oFile.ReadAll '返回模版内容 readTemplateFile=template oFile.Close : set oFile = nothing else response.write "<b>DeepTemplate错误: 文件[" & inFileName & "] 没有!</b><br />" end if set oFSO = nothing else response.write "<b>DeepTemplate错误: readTemplateFile错误的文件名.</b><br />" end if end function ' --------------------------- '模版文件内容 替换 public sub replaceText(oldText,newText) if (isnull(oldText) )then oldText="" end if if ( isnull(newText))then newText="" end if template=replace( template ,oldText,newText) end sub ' --------------------------- '模版文件 标签 替换 public sub replaceTag(oldText,newText) if (isnull(oldText) )then oldText="" end if if ( isnull(newText))then newText="" end if template=replace( template ,left_delimiter&oldText&right_delimiter,newText) end sub '┌──────────────────── 深度空间 深度学习 ──┐ '│过程名:replaceTagParameter '│作 用:模版文件内容 替换 含参数标签 '│参 数: labelName 标签名 '│ functionName 函数名 '│说 明: 标签参数与对应函数参数必须一致 '│日 期:2010-2-15 2010-2-15 '└──────────────────── www.deepstudy.cn ──┘ public sub replaceTagParameter(labelName,ByRef functionName) dim tempValue if ( isnull(labelName))then labelName="" end If for each tempValue in GetLabelParameter(labelName) replaceTag labelName&"("+tempValue+")" , getref(functionName)( split(tempValue,",") ) Next end Sub ' --------------------------- ' 显示模版文件内容 public sub display() response.Write( template ) end sub ' --------------------------- ' 得到 标签集合 返回值:取得模板变量的值的数组 private Function GetLabelParameter(labelName) Dim oMatches ' 建立变量。 dim parameterArray() , i dim oZus,temp oRegEx.Pattern = left_delimiter_regex & labelName+"\(.*?\)" &right_delimiter_regex ' 设置得到{$}里参数正则 Set oMatches = oRegEx.Execute(template) ' 执行搜索。 ' 得到()里参数 oRegEx.pattern="\(.*?\)" '设置 得到()里参数正则 redim preserve parameterArray(oMatches.count-1) 'response.Write( ubound(MyArray)) 'response.Write( Matches.count ) for i=0 to oMatches.count-1 ' 遍历匹配集合 set oZus=oRegEx.execute(oMatches(i).Value) temp=replace(oZus(0),"(","") temp=replace(temp,")","") parameterArray(i)=temp 'response.Write(parameterArray(i)) Next SET oZus = Nothing SET oMatches = Nothing GetLabelParameter =parameterArray End Function ' ********************** ' 正则表达式 字符转义 \{\$ \} private function charToRegexp(sText) dim i,temp,chars:chars="" oRegEx.Pattern = "[\W]" ' 设置得到{$}里参数正则 for i=1 to len(sText) temp= mid(sText,i,1) if(oRegEx.test(temp)) then chars=chars& "\"&temp else chars=chars & temp end if next charToRegexp = chars end function '只读 Public property Get TEMPLATE_VERSION() TEMPLATE_VERSION= VERSION End Property end class %>