代码分离办法

代码分离

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>

希望能对你有所帮助!

时间: 2024-08-01 08:24:08

代码分离办法的相关文章

老外的代码分离办法

代码分离 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

老外的代码分离办法,是用模板的,有兴趣的可以研究一下

代码分离|模板 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

ASP关于页面和代码分离的问题

代码分离|问题|页面|代码分离     为了避免ASP程序和HTML代码混写造成维护困难的情况,本文介绍了一种方法,利用模板来分离程序和页面,使程序设计更加轻松.       在使用ASP制作一个站点的时候,常常会出现一个ASP文件中,程序代码和HTML代码混合的情况.这样子做有许多缺点:    1. 且不说编程时就要对页面布局进行设计和编排,造成代码混乱难懂,不规范:    2. 当需要改变页面外观时,你不仅要改变HTML部份,也需要改变ASP代码,不易维护.       那么,要如何才能避免

使用ASP.NET中的一点体会[关于代码分离]

asp.net|代码分离     最近在进行项目开发中,经常会遇到这种情况,就是当用户点击页面上的Button时,系统需要弹出一个窗口.这个窗口可能是标准对话框,也可能是一个新的页面.开始的时候我不知道如何实现这样的功能,按照我以前的编程习惯,我认为应该有一个类似于ShowMessage的方法,但是,可惜这种方法在WEB下是没有的.通过在网上查阅资料,发现一般的做法是在Button的OnClick事件中写下如下代码:   private void Button1_Click(object sen

使用PHP4中的 IntegratedTemplate类实现HTML和PHP代码分离

代码分离 使用PHP编程的人都会碰到这样一个问题:当PHP代码和HTML代码在一起的时候,看PHP代码很费劲,整个文件也无法用Dreamweaver来编辑,这对于PHP程序员和美工来讲,修改这样的文件就象一个噩梦. PHP中的模板(Template)技术就是为了解决这个问题而出现的.PHP模板类有很多,比较常见的是 FastTemplate 和 PHPLib, 因为出现得早,在PHP编程界名声很大.PHP程序员不知道这两个类,就象VB程序员不知道MsgBox函数一样,是一件不可思议的事情. 以前

程序员上看来的文章,也是关于页面和代码分离的

程序|程序员|代码分离|页面 为了避免ASP程序和HTML代码混写造成维护困难的情况,本文介绍了一种方法,利用模板来分离程序和页面,使程序设计更加轻松.     在使用ASP制作一个站点的时候,常常会出现一个ASP文件中,程序代码和HTML代码混合的情况.这样子做有许多缺点:    1. 且不说编程时就要对页面布局进行设计和编排,造成代码混乱难懂,不规范:    2. 当需要改变页面外观时,你不仅要改变HTML部份,也需要改变ASP代码,不易维护.        那么,要如何才能避免这些麻烦呢?

表示代码与逻辑代码分离

代码分离 <%@ Page Inherits="MyCodeBehind" Src="c2.vb" %> There is a nice section in the quickstart docs on this topic also. Click here to read up on it! Here is the code This example uses the following MS-SQL Server 7.0 database Stor

关于页面和代码分离的

代码分离|页面 为了避免ASP程序和HTML代码混写造成维护困难的情况,本文介绍了一种方法,利用模板来分离程序和页面,使程序设计更加轻松. 在使用ASP制作一个站点的时候,常常会出现一个ASP文件中,程序代码和HTML代码混合的情况.这样子做有许多缺点: 1. 且不说编程时就要对页面布局进行设计和编排,造成代码混乱难懂,不规范: 2. 当需要改变页面外观时,你不仅要改变HTML部份,也需要改变ASP代码,不易维护. 那么,要如何才能避免这些麻烦呢? 答案就是使用模板文件,将ASP代码和HTML页

ASP.NET中的代码分离

asp.net|代码分离 ASP.NET中包含了一种新方法:将商业逻辑代码从表达代码中分离出来.这通常被称为背后的代码,功能非常强大,并且非常容易执行.实现步骤就是:向ASP.NET 页面中增加用户界面元素,并为它们指定属性"runat=server".然后,用.NET语言创建一个类文件来操作这些用户界面元素.最后,在ASP.NET 页面顶部增加一个指令,将用户界面与操作它的类文件附着在一起. 用一个简单的例子就能展示它是如何完成的.下面创建一个名叫WebPage.aspx的ASP.N