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

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

问题描述

请记住,这是针对 classic asp

哪个更好,所有 HTML 都包含在 Response.Write 语句中或通过 <%= %> 将变量插入 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 解析器获取页面,并将 <%= expression %> 转换为直接的脚本调用,每个连续的 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 会越来越模糊您的逻辑.作为一般规则,一旦您开始使用 about 循环,您需要停止并切换到 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天全站免登陆