Response.Write vs <%=%> [英] Response.Write vs <%= %>

查看:22
本文介绍了Response.Write vs <%=%>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

记住这是针对经典asp

哪个更好,包含在 Response.Write Statements 中的所有 HTML 或通过 <%= %> 将变量插入 HTML.
例如

Which is better, all HTML contained within Response.Write Statements or inserting variables into HTML via <%= %>.
Eg

Response.Write "<table>" & vbCrlf
Response.Write "<tr>" &vbCrLf
Response.Write "<td class=""someClass"">" & someVariable & "</td>" & vbCrLf
Response.Write "</tr>" & vbCrLf
Response.Write "</table>" & vbCrLf

VS

<table>
  <tr>
     <td class="someClass"><%= someVariable %></td>
  </tr>
</table>

我主要是从性能的角度来询问,当插入多个变量时,哪个对服务器的影响最小?

I am mainly asking from a performance point of view in which will have the least server impact when there multiple variables to insert?

如果没有技术上的差异,两者之间的论据是什么?

If there are no technical differences what are the arguments for one over the other?

推荐答案

首先,您应该考虑的最重要的因素是易于维护.您可以用金钱和时间购买服务器群,否则您必须破译一个杂乱的网站来维护它.

First, The most important factor you should be looking at is ease of maintenance. You could buy a server farm with the money and time you would otherwise waste by having to decipher a messy web site to maintain it.

无论如何,都无所谓.归根结底,ASP 所做的只是执行一个脚本!ASP 解析器获取页面,并将 <%= 表达式 %> 转换为直接的脚本调用,并且 HTML 的每个连续块都成为对 Response.Write 的一个巨大调用.除非页面在磁盘上发生更改,否则生成的脚本会被缓存并重复使用,这会导致重新计算缓存的脚本.

In any case, it doesn't matter. At the end of the day, all ASP does is just execute a script! The ASP parser takes the page, and transforms <%= expression %> into direct script calls, and every contiguous block of HTML becomes one giant call to Response.Write. The resulting script is cached and reused unless the page changes on disk, which causes the cached script to be recalculated.

现在,过多使用 <%= %> 导致了意大利面代码"的现代版本:可怕的标签汤".你将无法对逻辑做出正面或反面.另一方面,过多使用 Response.Write 意味着在页面呈现之前您将永远无法看到页面.在适当的时候使用 <%= %> 以获得两全其美的效果.

Now, too much use of <%= %> leads to the modern version of "spaghetti code": the dreaded "Tag soup". You won't be able to make heads or tails of the logic. On the other hand, too much use of Response.Write means you will never be able to see the page at all until it renders. Use <%= %> when appropriate to get the best of both worlds.

我的第一条规则是注意可变文本"与静态文本"的比例.

My first rule is to pay attention at the proportion of "variable text" to "static text".

如果您只有几个地方需要替换可变文本,<%= %> 语法非常简洁易读.但是,随着 <%= %> 开始积累,它们会掩盖越来越多的 HTML,同时 HTML 也会掩盖越来越多的逻辑.作为一般规则,一旦您开始处理循环,您需要停止并切换到 Response.Write`.

If you have just a few places with variable text to replace, the <%= %> syntax is very compact and readable. However, as the <%= %> start to accumulate, they obscure more and more of the HTML and at the same time the HTML obscures more and more of your logic. As a general rule, once you start taking about loops, you need to stop and switch to Response.Write`.

没有太多其他硬性规定.您需要为您的特定页面(或页面的一部分)决定哪个更重要,或者更难理解,或者更容易破坏:您的逻辑还是您的 HTML?通常是一种或另一种(我已经看过数百个案例)

There aren't many other hard and fast rules. You need to decide for your particular page (or section of the page) which one is more important, or naturally harder to understand, or easier to break: your logic or your HTML? It's usually one or the other (I've seen hundreds of cases of both)

如果你的逻辑更关键,你应该更多地关注Response.Write;这将使逻辑脱颖而出.如果您的 HTML 更重要,请使用 <%= %>,这将使页面结构更加可见.

If you logic is more critical, you should weight more towards Response.Write; it will make the logic stand out. If you HTML is more critical, favor <%= %>, which will make the page structure more visible.

有时我不得不编写两个版本并并排比较它们以决定哪个版本更具可读性;这是最后的手段,但请在您对代码记忆犹新时进行操作,三个月后您会很高兴必须进行更改.

Sometimes I've had to write both versions and compare them side-by-side to decide which one is more readable; it's a last resort, but do it while the code is fresh in your mind and you will be glad three months later when you have to make changes.

这篇关于Response.Write vs &lt;%=%&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆