模板技术


要生成静态页,必须采用模板技术,因为要写入TXT/Html文件,必须先将所有的最终Html代码,提前放入一个变量,然后才能写文件。

而要将所有的代码放在一个变量,必须用标签技术,即在模板中,用预定义标签来代表各个功能,读模板的过程中,根据各个标签来调用各个变量,随时将调用结果加到代码里,这样才能实现。

<%
filename = Server.MapPath("index.html")
HtmlContent = Readfile(filename)
'这是提供给前台程序的Html代码,可以从数据库中读取模板,或从Txt/Html文件读取到这些代码,这是例程,采用直接赋值给变量的方法。
HtmlContent = HtmlContent & "<Table border=1 width=""100%""><tr><td>{$ReadJobList(1,2)}</td><td>{$ReadCompanyList(1,2)}</td></tr></table>"

%>



<%

'clsMain.asp
 '================================================
 '函数名:CutMatchContent
 '作  用:截取相匹配的内容
 '参  数:Str   ----原字符串
 '        PatStr   ----符合条件字符
 '================================================
 Public Function CutMatchContent(ByVal str, ByVal start, ByVal last, ByVal Condition)
       
  Dim Match,s,re
  Dim FilterStr,MatchStr
  Dim strContent,ArrayFilter
  Dim i, n,bRepeat
  
  If Len(start) = 0 Or Len(last) = 0 Then Exit Function
  
  On Error Resume Next
  
  MatchStr = "(" & CorrectPattern(start) & ")(.+?)(" & CorrectPattern(last) & ")"

  Set re = New RegExp
  re.IgnoreCase = True
  re.Global = True
  re.Pattern = MatchStr
  Set s = re.Execute(str)
  n = 0
  For Each Match In s
   If n = 0 Then
    n = n + 1
    ReDim ArrayFilter(n)
    ArrayFilter(n) = Match
   Else
    bRepeat = False
    For i = 0 To UBound(ArrayFilter)
     If UCase(Match) = UCase(ArrayFilter(i)) Then
      bRepeat = True
      Exit For
     End If
    Next
    If bRepeat = False Then
     n = n + 1
     ReDim Preserve ArrayFilter(n)
     ArrayFilter(n) = Match
    End If
   End If
  Next
  
  Set s = Nothing
  Set re = Nothing
  
  If CBool(Condition) Then
   strContent = Join(ArrayFilter, "|||")
  Else
   strContent = Join(ArrayFilter, "|||")
   strContent = Replace(strContent, start, "")
   strContent = Replace(strContent, last, "")
  End If
  
  CutMatchContent = Replace(strContent, "|||", vbNullString, 1, 1)
 End Function
 
 Function CutFixContent(ByVal str, ByVal start, ByVal last, ByVal n)
  Dim strTemp
  On Error Resume Next
  If InStr(str, start) > 0 Then
   Select Case n
   Case 0  '左右都截取(都取前面)(去处关键字)
    strTemp = Right(str, Len(str) - InStr(str, start) - Len(start) + 1)
    strTemp = Left(strTemp, InStr(strTemp, last) - 1)
   Case Else  '左右都截取(都取前面)(保留关键字)
    strTemp = Right(str, Len(str) - InStr(str, start) + 1)
    strTemp = Left(strTemp, InStr(strTemp, last) + Len(last) - 1)
   End Select
  Else
   strTemp = ""
  End If
  CutFixContent = strTemp
 End Function
 Private Function CorrectPattern(ByVal str)
  str = Replace(str, "\", "\\")
  str = Replace(str, "~", "\~")
  str = Replace(str, "!", "\!")
  str = Replace(str, "@", "\@")
  str = Replace(str, "#", "\#")
  str = Replace(str, "%", "\%")
  str = Replace(str, "^", "\^")
  str = Replace(str, "&", "\&")
  str = Replace(str, "*", "\*")
  str = Replace(str, "(", "\(")
  str = Replace(str, ")", "\)")
  str = Replace(str, "-", "\-")
  str = Replace(str, "+", "\+")
  str = Replace(str, "[", "\[")
  str = Replace(str, "]", "\]")
  str = Replace(str, "<", "\<")
  str = Replace(str, ">", "\>")
  str = Replace(str, ".", "\.")
  str = Replace(str, "/", "\/")
  str = Replace(str, "?", "\?")
  str = Replace(str, "=", "\=")
  str = Replace(str, "|", "\|")
  str = Replace(str, "$", "\$")
  CorrectPattern = str
 End Function


 '=============================================================
 '过程名:CreatedTextFile
 '作  用:创建文本文件
 '参  数:filename  ----文件名
 '        body  ----主要内容
 '=============================================================
 Public Function CreatedTextFile(ByVal FileName, ByVal body)
  On Error Resume Next
  If InStr(FileName, ":") = 0 Then FileName = Server.MapPath(FileName)
  Dim fso,f
  Set fso = Server.CreateObject(FSO_ScriptName)
  Set f = fso.CreateTextFile(FileName)
  f.WriteLine body
  f.Close
  Set f = Nothing
  Set fso = Nothing
  If Err.Number <> 0 Then Err.Clear
 End Function
 '================================================
 '函数名:Readfile
 '作  用:读取文件内容
 '参  数:fromPath   ----来源文件路径
 '================================================
 Public Function Readfile(ByVal fromPath)
  'On Error Resume Next
  Dim strTemp,fso,f
  If InStr(fromPath, ":") = 0 Then fromPath = Server.MapPath(fromPath)
  Set fso = Server.CreateObject("Scripting.FileSystemObject")
  If fso.FileExists(fromPath) Then
   Set f = fso.OpenTextFile(fromPath, 1, True)
   strTemp = f.ReadAll
   f.Close
   Set f = Nothing
  End If
  Set fso = Nothing
  Readfile = strTemp
  If Err.Number <> 0 Then Err.Clear
 End Function
%>


<%

'Cls_Public.asp

 '================================================
 '函数名:LoadCompanyList
 '================================================
 Public Function LoadCompanyList(a,b) 

  '根据接收到的参数,从数据库中查询到记录,然后将所有结果连成一个字符串,放到strContent中传递
  strContent = "<a href=show.asp?id=6 target=_blank>百誉科技!</a>"
  LoadCompanyList = strContent

 End Function

 '================================================
 '函数名:ReadCompanyList
 '作  用:读取公司列表
 '参  数:str ----原字符串
 '================================================
 Public Function ReadCompanyList(ByVal str)
  Dim strTemp, i
  Dim sTempContent, nTempContent
  Dim arrTempContent, arrTempContents, ArrayList
  'On Error Resume Next
  strTemp = str
  If InStr(strTemp, "{$ReadCompanyList(") > 0 Then

   sTempContent = CutMatchContent(strTemp, "{$ReadCompanyList(", ")}", 1)
   nTempContent = CutMatchContent(strTemp, "{$ReadCompanyList(", ")}", 0)
   arrTempContents = Split(sTempContent, "|||")
   arrTempContent = Split(nTempContent, "|||")
   For i = 0 To UBound(arrTempContents)
    ArrayList = Split(arrTempContent(i), ",")
    strTemp = Replace(strTemp, arrTempContents(i), LoadCompanyList(1,2))
   Next
  End If
  ReadCompanyList = strTemp

 End Function

 

 

 

 

 

 '================================================
 '函数名:LoadJobList
 '================================================
 Public Function LoadJobList(a,b) 

  '根据接收到的参数,从数据库中查询到记录,然后将所有结果连成一个字符串,放到strContent中传递
  strContent = "<a href=show.asp?id=6 target=_blank>财务人员!</a>"
  LoadJobList = strContent

 End Function

 '================================================
 '函数名:ReadJobList
 '作  用:读取职位列表
 '参  数:str ----原字符串
 '================================================
 Public Function ReadJobList(ByVal str)
  Dim strTemp, i
  Dim sTempContent, nTempContent
  Dim arrTempContent, arrTempContents, ArrayList
  'On Error Resume Next
  strTemp = str
  If InStr(strTemp, "{$ReadJobList(") > 0 Then

   sTempContent = CutMatchContent(strTemp, "{$ReadJobList(", ")}", 1)
   nTempContent = CutMatchContent(strTemp, "{$ReadJobList(", ")}", 0)
   arrTempContents = Split(sTempContent, "|||")
   arrTempContent = Split(nTempContent, "|||")
   For i = 0 To UBound(arrTempContents)
    ArrayList = Split(arrTempContent(i), ",")
    strTemp = Replace(strTemp, arrTempContents(i), LoadJobList(1,2))
   Next
  End If
  ReadJobList = strTemp

 End Function

 Public Function ShowIndex(isHtml)  


  HtmlContent = ReadJobList(HtmlContent)    '职位列表
  HtmlContent = ReadCompanyList(HtmlContent)   '公司列表

  If isHtml=1 Then
   'ShowIndex = HtmlContent
   Response.Write HtmlContent
  Else
   Response.Write HtmlContent
  End If

 End Function

%>

<%
'Templete.asp
filename = Server.MapPath("index.html")
HtmlContent = Readfile(filename)
'这是提供给前台程序的Html代码,可以从数据库中读取模板,或从Txt/Html文件读取到这些代码,这是例程,采用直接赋值给变量的方法。
HtmlContent = HtmlContent & "<Table border=1 width=""100%""><tr><td>{$ReadJobList(1,2)}</td><td>{$ReadCompanyList(1,2)}</td></tr></table>"

%>

<!--#include file="inc/Cls_Public.asp"-->
<!--#include file="inc/clsMain.asp"-->
<!--#include file="inc/Templete.asp"-->

<%
Call ShowIndex(1)
%>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值