开发者

validity of classic ASP include pattern

开发者 https://www.devze.com 2023-01-13 22:09 出处:网络
Here is a pattern I am thinking about in ASP : Imagine you have a file main.asp that contains <!--#include file=\"1.asp\"-->

Here is a pattern I am thinking about in ASP :

Imagine you have a file main.asp that contains

<!--#include file="1.asp"-->

code of 1.asp

   ...my code...

Do you think it is valid to refactor this as

main.asp

Dim defined_1_asp = false
<!--#include file="1.asp"-->

1.asp

if (not defined_1_asp) then
    defined_1_asp = true
    ...my code...
end if

This way I could refactor all my SSI includes while making sure they are executed only once. Of course, the content of the includes would be included, but execution would be protected by the if.

I read开发者_开发技巧 that the if statement does not have its own scope in classic ASP so it seems to me that the behavior of the code would not be impacted by the refactoring.

Would I hit a bottleneck if the same files are SSI-included several times ?

Thanks a lot for your help,

Jerome Wagner


AFAIK You cannot include code more than once (you will get errors with duplicate identifiers).

I create classes , creating them if and when needed.


If you need to include any piece of code more than once, you should make it a Sub or a Function. In my experience, SSIs are used to store those Subs and Functions. So what you could do is create a Sub in 1.asp and then in main.asp do this:

<!--#include file="1.asp"-->

Dim defined_1_asp = false

Call MySub

With the Sub being something like

If Not defined_1_asp Then
  ...code here...
End Sub


The SSI include is done before any rendering of the page is done. That means that 1.asp is included two times, giving you problems with declarations of variables and all kinds of error issues. That should be avoided at all cost. What you can do instead (and it is a much better design and programming practice) is to put the code in 1.asp into a Sub, then you can call the sub whenever the logic requires it. If it is a more complex issue you can create classes for the stuff in 1.asp or break it into many subs and functions. Much cleaner and better for future maintenance.

0

精彩评论

暂无评论...
验证码 换一张
取 消