老外的代码分离办法

 
 
 
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>

希望能对你有所帮助!
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
LabVIEW是一个非常强大的、全面的、图形化的编程语言和编程环境,主要用于控制系统、测试测量、数据采集和分析。由于独特的图形化编程方式,“老外”看到LabVIEW代码框架可能与传统的编程语言有所不同。 在LabVIEW中,程序被表示成一个数据流图,也称为VI(虚拟仪器)。每个VI包含一个用于控制程序流程的前导程序和一个后续程序,序列结构确定了操作执行的顺序,同时数据流对程序的执行方式也有很大影响。 在编写LabVIEW代码时,首先需要考虑的是程序的整体架构,即程序的输入与输出,以及数据类型和数据格式。然后,根据设计要求选择相应的控件和函数库,并将它们组合成一个VI。 程序流程的控制主要通过不同类型的结构完成,如条件结构、循环结构和事件结构等。此外,LabVIEW还使用图形化面板来显示和调节程序执行过程中的输入参数和输出结果。 需要注意的是,LabVIEW中的代码框架与传统的编程语言有所不同,对于不熟悉的人来说,其可视化界面可能更加直观和易于理解。因此,在设计和编写LabVIEW程序时,要根据特定的应用需求和功能要求考虑它的可读性和可维护性,以方便其他程序员进行修改和维护。 总之,了解LabVIEW的编程框架是非常重要的,这将有助于正确地设计和编写程序,并使其易于理解、可读性强、流程清晰、易于维护。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值