经典 ASP 包含模式的有效性
这是我在 ASP 中考虑的一种模式:
假设您有一个文件 main.asp ,其中包含
<!--#include file="1.asp"-->
代码 1.asp
...my code...
您认为将其重构为有效吗
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
这样我就可以重构所有 SSI 包含内容,同时确保它们只执行一次。当然,包含的内容将被包含在内,但执行将受到 if 的保护。
我读到 if 语句在经典 ASP 中没有自己的作用域,因此在我看来代码的行为不会受到重构的影响。
如果相同的文件多次包含在 SSI 中,我会遇到瓶颈吗?
非常感谢您的帮助,
杰罗姆·瓦格纳
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
AFAIK 您不能多次包含代码(您将收到重复标识符的错误)。
我创建类,并在需要时创建它们。
AFAIK You cannot include code more than once (you will get errors with duplicate identifiers).
I create classes , creating them if and when needed.
如果您需要多次包含任何代码段,则应将其设为 Sub 或 Function。根据我的经验,SSI 用于存储这些子函数和函数。所以你可以做的是在 1.asp 中创建一个 Sub,然后在 main.asp 中执行以下操作:
Sub 类似于
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:
With the Sub being something like
SSI 包含是在页面呈现之前完成的。这意味着 1.asp 被包含两次,从而给您带来变量声明和各种错误问题的问题。应该不惜一切代价避免这种情况。相反,您可以做的(这是更好的设计和编程实践)是将 1.asp 中的代码放入 Sub 中,然后您可以在逻辑需要时调用该 sub。如果这是一个更复杂的问题,您可以为 1.asp 中的内容创建类或将其分解为许多子函数和函数。更加干净,更有利于以后的维护。
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.