|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
优点:简单易学、开发速度快、有很多年“历史”,能找到非常多别人做好的程序来用、配合activeX功能强大,很多php做不到的asp+activeX能做到,例如银行安全控件代码分别
index.asp
-----------------------------------------------------------------------
<!--#includefile="templateclass.asp"-->
<%
Thisisthecodeusedtoloadanddisplaythetemplate.
Seehowcleanitis!!!:)
subgo()
dimoTemplate
setoTemplate=newtemplate
withoTemplate
.usetemplate("message.tpl")
.tag("date")=Date()
.tag("self")="<divstyle=background:#dddddd>"&.gettemplate("message.tpl",true)&"</div>"
.tag("aspcode")=.gettemplate("index.asp",false)
.tag("howtouse")=.gettemplate("howtouse.tpl",false)
.display()
endwith
setoTemplate=nothing
endsub
go()
%>
templateclass.asp
------------------------------------------------------------------------
<%
Thisisthetemplateobject.Itallowsthecreation,reading
andeditingoftemplatesontheserver.
CREATEDBY:ChristopherBrown-Floyd
DATE:November3,1999
classtemplate
Thisvariablestoresthetemplate
privatemytemplateasstring
Thismethodgetsatemplatefromtheserverandreturns
thewholefileasastring.
publicfunctiongettemplate(pathfilename,encodetohtml)asstring
dimoFSOasobject
dimoTemplateasobject
dimtemptemplateasstring
Opentypeforthetemplate(read-only)
constforreading=1,boolcreatefile=false
ifIsNull(encodetohtml)orencodetohtml=""orencodetohtml=falsethen
encodetohtml=false
else
encodetohtml=true
endif
onerrorresumenext
Createfilesystemobject
setoFSO=server.createobject("scripting.filesystemobject")
Createtemplateobject
setoTemplate=oFSO.opentextfile(server.mappath(pathfilename),forreading,boolcreatefile)
iferr0then
err.clear
exitfunction
endif
Getthewholefileasastring
temptemplate=oTemplate.readall
EncodetemplatetoHTML?
ifencodetohtmlthen
gettemplate=tohtml(temptemplate)
else
gettemplate=temptemplate
endif
Closethetemplate
oTemplate.close
Freetheserverresources
setoTemplate=nothing
setoFSO=nothing
endfunction
Thisproceduregetsandstoresatemplate
publicsubusetemplate(pathfilename)
thistemplate=gettemplate(pathfilename,false)
endsub
Thispropertyreplacestagswiththeuserstemplate
publicpropertylettag(tagname,userstring)
dimld,rdasstring
dimtemptagasstring
dimtagstart,tagendasinteger
ld="<!--"
rd="-->"
tagstart=1
dowhiletagstart>0andtagstart<len(thistemplate)
tagstart=instr(tagstart,thistemplate,ld)
iftagstart>0then
tagend=instr(tagstart,thistemplate,rd)
iftagend>(tagstart+3)then
temptag=mid(thistemplate,tagstart+4,tagend-(tagstart+4))
if(trim(temptag)=tagname)or(temptag=tagname)then
ifIsNull(userstring)then
thistemplate=replace(thistemplate,ld&temptag&rd,"")
else
thistemplate=replace(thistemplate,ld&temptag&rd,userstring)
endif
exitdo
else
tagstart=tagstart+4
endif
endif
endif
loop
endproperty
publicsubremoveTags()
dimld,rdasstring
dimtemptagasstring
dimtagstart,tagendasinteger
ld="<!--"
rd="-->"
tagstart=1
dowhiletagstart>0andtagstart<len(thistemplate)
tagstart=instr(tagstart,thistemplate,ld)
iftagstart>0then
tagend=instr(tagstart,thistemplate,rd)
iftagend>(tagstart+3)then
temptag=mid(thistemplate,tagstart+4,tagend-(tagstart+4))
thistemplate=replace(thistemplate,ld&temptag&rd,"")
tagstart=tagend
endif
endif
loop
endsub
Thispropertyallowstheusertoassignthecurrenttemplate
publicpropertyletthistemplate(template_as_string)
ifvartype(template_as_string)=vbstring_
orvartype(template_as_string)=vbnullthen
mytemplate=template_as_string
endif
endproperty
Thispropertyreturnsthecurrenttemplate
publicpropertygetthistemplate()asstring
thistemplate=mytemplate
endproperty
Thissubroutinedisplaysthecurrenttemplate
publicsubdisplay()
response.writethistemplate
endsub
ThissubroutineencodesthecurrenttemplatetoHTML
publicsubhtmlencode()
tohtml(thistemplate)
endsub
Thisproceduresavesthecurrenttemplatetotheserver
publicsubsaveas(pathfilename)
dimoFSOasobject
dimoTemplate,oBackupasobject
dimstrTruePathasstring
Opentypeforthetemplate(read-only)
constforreading=1,forwriting=2,boolcreatefile=true
onerrorresumenext
Createfilesystemobject
setoFSO=server.createobject("scripting.filesystemobject")
Createtemplateobject
strTruePath=server.mappath(pathfilename)
ifoFSO.fileexists(strTruePath)then
oFSO.copyfilestrTruePath,strTruePath&".bak",true
endif
setoTemplate=oFSO.opentextfile(strTruePath,forwriting,boolcreatefile)
iferr0then
err.clear
exitsub
endif
Writethewholetemplatetotheserver
oTemplate.writethistemplate
Closethetemplate
oTemplate.close
Freetheserverresources
setoTemplate=nothing
setoFSO=nothing
endsub
Thisfunctionencodestexttohtml
privatefunctiontohtml(temptemplate)
temptemplate=replace(temptemplate,"<","<")
temptemplate=replace(temptemplate,"","")
temptemplate=replace(temptemplate,vbcrlf,"<br/>")
tohtml=temptemplate
endfunction
Thisprocedureclearsthevariablethatthecurrenttemplate
isstoredinwhentheobjectiscreated.
privatesubclass_initialize()
mytemplate=""
endsub
Thisprocedureclearsthevariablethatthecurrenttemplate
isstoredinwhentheobjectisterminated.
privatesubclass_terminate()
setmytemplate=nothing
endsub
endclass
%>
howtouse.tpl
------------------------------------------------------------------------
<h3>Objectsproperties:</h3>
<divstyle="background:#dddddd"><p><b>thistemplate</b>=<i>String</i>
Loadsadynamicallybuilttemplateintothetemplateobject.Usethismethod
whenthereisntafiletoreadfrom.</p></div>
<h3>Objectsprocedures:</h3>
<divstyle="background:#dddddd"><p><b>tag(<i>tagnameasstring</i>)</b>=<i>String</i>
Replacesalloccurrencesofatagwithinthecurrenttemplatewiththespecifiedstring.</p>
<p><i>variable</i>=<b>gettemplate(<i>pathasstring</i>,<i>[encodetohtml]asboolean</i>)</b>
Returnsthetemplatefromthespecifiedpath;however,itisntloadedintotheobject.</p></div>
<h3>Objectsmethods:</h3>
<divstyle="background:#dddddd"><p><b>usetemplate(<i>pathasstring</i>)</b>
Loadsthetemplatefromthespecifiedpathintotheobject.</p>
<p><b>htmlencode()</b>
EncodesthecurrenttemplateloadedintoHTML.</p>
<p><b>display()</b>
Writesthecurrenttemplatetotheusersbrowser.</p>
<p><b>removetags()</b>
Removesalltagsthathaventbeenreplaced.</p>
<p><b>saveas(<i>pathashtml</i>)</b>
Savesthecurrenttemplatetothespecifiedpath.Usethismethodwhenyou
wanttosavethechangesmadetothetemplate.
NOTE:Ifafileofthesamenameexistsatthespecifiedpath,".bak"willbe
addedtotheendofitsname;thus"myfile.tpl"wouldbecome"myfile.tpl.bak".</p></div>
</div>
message.tpl
------------------------------------------------------------------------
<html>
<body>
<pre>
Thisisanexampleofhowtousemytemplateclass.
Dynamicdataforyourtemplatecancomefromanywhere.
Itcancomefromabuilt-inASPfunction:<!--date-->
Itcancomefromafile:
<!--self-->
YoucanevenuseitasanonlinesourceeditingtoolforyourASPcode:
<form><textareacols="80"rows="30"><!--aspcode--></textarea></form>
<!--howtouse-->
NOTE:TemplatescanthaveanyASPcodeinthem.ItllonlyprocessHTML.
Thisisgoodpractice,becauseitproducescleanercodeandHTML.
</pre>
</body>
</html>
但愿能对你有所匡助!
ASP最大的缺点在于网络的安全性和可靠性,企业将经营数据放在开放的平台上,最大的担忧就是如何保证这些数据不被其他人破坏。 |
|