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 users 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
this property returns the current template
public property get thistemplate() as string
thistemplate = mytemplate
end property
this subroutine displays the current template
public sub display()
response.write thistemplate
end sub
this subroutine encodes the current template to html
public sub htmlencode()
tohtml(thistemplate)
end sub
this procedure saves the current template to the server
public sub saveas(pathfilename)
dim ofso as object
dim otemplate,obackup as object
dim strtruepath as string
open type for the template(read-only)
const forreading = 1,forwriting = 2,boolcreatefile = true
on error resume next
create filesystemobject
set ofso = server.createobject("scripting.filesystemobject")
create template object
strtruepath = server.mappath(pathfilename)
if ofso.fileexists(strtruepath) then
ofso.copyfile strtruepath, strtruepath & ".bak", true
end if
set otemplate = ofso.opentextfile(strtruepath,forwriting,boolcreatefile)
if err <> 0 then
err.clear
exit sub
end if
write the whole template to the server
otemplate.write thistemplate
close the template
otemplate.close
free the server resources
set otemplate = nothing
set ofso = nothing
end sub
this function encodes text to html
private function tohtml(temptemplate)
temptemplate = replace(temptemplate,"<","<")
temptemplate = replace(temptemplate," "," ")
temptemplate = replace(temptemplate,vbcrlf,"<br />")
tohtml = temptemplate
end function
this procedure clears the variable that the current template
is stored in when the object is created.
private sub class_initialize()
mytemplate = ""
end sub
this procedure clears the variable that the current template
is stored in when the object is terminated.
private sub class_terminate()
set mytemplate = nothing
end sub
end class
%>
howtouse.tpl
————————————————————————
<h3>objects properties:</h3>
<div style="background:#dddddd"><p><b>thistemplate</b> = <i>string</i>
loads a dynamically built template into the template object. use this method
when there isnt a file to read from.</p></div>
<h3>objects procedures:</h3>
<div style="background:#dddddd"><p><b>tag(<i>tag name as string</i>)</b> = <i>string</i>
replaces all occurrences of a tag within the current template with the specified string.</p>
<p><i>variable</i> = <b>gettemplate(<i>path as string</i>,<i>[encode to html] as boolean</i>)</b>
returns the template from the specified path; however, it isnt loaded into the object.</p></div>
<h3>objects methods:</h3>
<div style="background:#dddddd"><p><b>usetemplate(<i>path as string</i>)</b>
loads the template from the specified path into the object.</p>
<p><b>htmlencode()</b>
encodes the current template loaded into html.</p>
<p><b>display()</b>
writes the current template to the users browser.</p>
<p><b>removetags()</b>
removes all tags that havent been replaced.</p>
<p><b>saveas(<i>path as html</i>)</b>
saves the current template to the specified path. use this method when you
want to save the changes made to the template.
note: if a file of the same name exists at the specified path, ".bak" will be
added to the end of its name; thus "myfile.tpl" would become "myfile.tpl.bak".</p></div>
</div>
message.tpl
————————————————————————
<html>
<body>
<pre>
this is an example of how to use my template class.
dynamic data for your template can come from anywhere.
it can come from a built-in asp function: <!–date–>
it can come from a file:
<!–self–>
you can even use it as an online source editing tool for your asp code:
<form><textarea cols="80" rows="30"><!–aspcode–></textarea></form>
<!–howtouse–>
note: templates cant have any asp code in them. itll only process html.
this is good practice, because it produces cleaner code and html.
</pre>
</body>
</html>
希望能对你有所帮助!
