设为首页
联系站长
加入收藏

您的位置: 首页>>IT业界>>互联网>>正文
 代码分离办法
 日期:2005-6-21 22:07:31  来源:100down.com
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


' 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>Object's 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 isn't a file to read from.</p></div>


<h3>Object's 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 isn't loaded into the object.</p></div>

<h3>Object's 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 user's browser.</p>

<p><b>removetags()</b>
Removes all tags that haven't 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 can't have any ASP code in them. It'll only process HTML.
This is good practice, because it produces cleaner code and HTML.
</pre>
</body>
</html>

希望能对你有所帮助!

相关文章

·关闭窗口时保存数据的办法
·用正则解析图片地址,并利用XMLHT
·实现搜索结果的关键词变色标注的
·关于处理GET方式提交的含有特殊字
·ASP判断Cookies是否处于开启状态
·NextRecordset 和 GetRows 双簧合
·ASP自动生成编号的方法
·Filter与updatebatch混合使用实现
·asp中利用数组实现数据库记录的批
·asp中通过getrows实现数据库记录


阅读排行

·Win 2000不能访问Win XP的原因及
·解析Windows中的帐户和权限功能
·如何共享Windows XP操作系统
·Windows XP Service Pack 1
·通过 Windows XP 注册表自定义您
·Windows 2000 TCP/IP协议概述
·Windows 2000 Server TCP/IP协议
·dos常用命令使用说明
·Windows变慢原因分析及解决方法(
·Windows变慢原因分析及解决方法(
·WINXP下强行关闭“杀”不了的进程
·Windows XP系统注册表的恢复

最新文档

·创业激情需冷静 网络代理选择应理
·域名交易平台亟需规范 预防域名买
·Travel旅游域名即将引发抢注潮 
·从原理深处分析如何预防arp攻击
·突破建站弊端 企业网络营销大有可
·如何区分进程和病毒?
·java的基础知识,如何学好java
·微机原理与接口技术基础知识
·如何解决青少年沉迷网络的问题,
·全面分析主板BIOS报警信号

请您注意:
·尊重网上道德,遵守中华人民共和国的各项有关法律法规
·您在本站发表的作品,本站有权在网站内转载或引用
·其他网站如果需要转载 本站文章请在贵站著名来源,谢谢合作