老外的代碼分離辦法

來源:互聯網
上載者:User
代碼分離 index.asp
-----------------------------------------------------------------------
<!--#include file="templateclass.asp"-->
<%
' This is the code used to load and display the template.
' See how clean it is!!! :)
sub go()
dim oTemplate

set oTemplate = new template

with oTemplate
.usetemplate("message.tpl")
.tag("date") = Date()
.tag("self") = "<div style='background:#dddddd'>" & .gettemplate("message.tpl",true) & "</div>"
.tag("aspcode") = .gettemplate("index.asp",false)
.tag("howtouse") = .gettemplate("howtouse.tpl",false)
.display()
end with

set oTemplate = nothing
end sub
go()
%>

templateclass.asp
------------------------------------------------------------------------
<%
' This is the template object. It allows the creation, reading
' and editing of templates on the server.
' CREATED BY: Christopher Brown-Floyd
' DATE: November 3, 1999
class template

' This variable stores the template
private mytemplate 'as string

' This method gets a template from the server and returns
' the whole file as a string.
public function gettemplate(pathfilename,encodetohtml) 'as string
dim oFSO 'as object
dim oTemplate 'as object
dim temptemplate 'as string

' Open type for the template(read-only)
const forreading = 1,boolcreatefile = false

if IsNull(encodetohtml) or encodetohtml = "" or encodetohtml = false then
encodetohtml = false
else
encodetohtml = true
end if


on error resume next
' Create filesystemobject
set oFSO = server.createobject("scripting.filesystemobject")

' Create template object
set oTemplate = oFSO.opentextfile(server.mappath(pathfilename),forreading,boolcreatefile)
if err <> 0 then
err.clear
exit function
end if
' Get the whole file as a string
temptemplate = oTemplate.readall

' Encode template to HTML?
if encodetohtml then
gettemplate = tohtml(temptemplate)
else
gettemplate = temptemplate
end if

' Close the template
oTemplate.close

' Free the server resources
set oTemplate = nothing

set oFSO = nothing
end function


' This procedure gets and stores a template
public sub usetemplate(pathfilename)
thistemplate = gettemplate(pathfilename,false)
end sub


' This property replaces tags with the user's template
public property let tag(tagname,userstring)
dim ld, rd 'as string
dim temptag 'as string
dim tagstart, tagend 'as integer

ld = "<!--"
rd = "-->"
tagstart = 1

do while tagstart > 0 and tagstart < len(thistemplate)
tagstart = instr(tagstart,thistemplate,ld)
if tagstart > 0 then
tagend = instr(tagstart,thistemplate,rd)
if tagend > (tagstart + 3) then
temptag = mid(thistemplate,tagstart + 4,tagend-(tagstart+4))
if (trim(temptag) = tagname) or (temptag = tagname) then
if IsNull(userstring) then
thistemplate = replace(thistemplate,ld & temptag & rd,"")
else
thistemplate = replace(thistemplate,ld & temptag & rd,userstring)
end if
exit do
else
tagstart = tagstart + 4
end if
end if
end if
loop
end property


public sub removeTags()
dim ld, rd 'as string
dim temptag 'as string
dim tagstart, tagend 'as integer

ld = "<!--"
rd = "-->"
tagstart = 1

do while tagstart > 0 and tagstart < len(thistemplate)
tagstart = instr(tagstart,thistemplate,ld)
if tagstart > 0 then
tagend = instr(tagstart,thistemplate,rd)
if tagend > (tagstart + 3) then
temptag = mid(thistemplate,tagstart + 4,tagend-(tagstart+4))
thistemplate = replace(thistemplate,ld & temptag & rd,"")
tagstart = tagend
end if
end if
loop
end sub


' This property allows the user to assign the current template
public property let thistemplate(template_as_string)
if vartype(template_as_string) = vbstring _
or vartype(template_as_string) = vbnull then
mytemplate = template_as_string
end if
end property



相關文章

Beyond APAC's No.1 Cloud

19.6% IaaS Market Share in Asia Pacific - Gartner IT Service report, 2018

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。