Response.Write vs <%= %>

后端 未结 15 2580
我在风中等你
我在风中等你 2020-12-28 14:34

Bearing in mind this is for classic asp

Which is better, all HTML contained within Response.Write Statements or inserting variables into HTML via &l

相关标签:
15条回答
  • 2020-12-28 14:58

    The response format will render HTML like so:

    <table>
    <tr>
    <td class="someClass">variable value</td>
    </tr>
    </table>
    

    As a result, not only will the means to produce your code be unreadable, but the result will also be tabbed inappropriately. Stick with the other option.

    0 讨论(0)
  • 2020-12-28 14:59

    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.

    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.

    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".

    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`.

    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)

    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.

    0 讨论(0)
  • 2020-12-28 15:00

    Leaving aside issues of code readibility/maintainibility which others have addressed, your question was specifically about performance when you have multiple variables to insert - so I assume you're going to be repeating something like your code fragment multiple times. In that case, you should get the best performance by using a single Response.Write without concatenating all of the newlines:

    Response.Write "<table><tr><td class=""someClass"">" & someVar & "</td></tr></table>"
    

    The browser doesn't need the newlines or the tabs or any other pretty formatting to parse the HTML. If you're going for performance, you can remove all of these. You'll also make your HTML output smaller, which will give you faster page load times.

    In the case of a single variable, it doesn't really make a lot of difference. But for multiple variables, you want to minimise the context switching between HTML and ASP - you'll take a hit for every jump from one to the other.

    To help with readibility when building a longer statement, you can use the VBScript line continuation charcter and tabs in your source code (but not the output) to represent the structure without hitting your performance:

    Response.Write "<table>" _
            & "<tr>" _
                & "<td class=""someClass"">" & someVar & "</td>" _
            & "</tr>" _
            & "<tr>" _
                & "<td class=""anotherClass"">" & anotherVar & "</td>" _
            & "</tr>" _
            & "<tr>" _
                & "<td class=""etc"">" & andSoOn & "</td>" _
            & "</tr>" _
        & "</table>"
    

    It's not as legible as the HTML version but if you're dropping a lot of variables into the output (ie a lot of context switching between HTML and ASP), you'll see better performance.

    Whether the performance gains are worth it or whether you would be better off scaling your hardware is a separate question - and, of course, it's not always an option.

    Update: see tips 14 and 15 in this MSDN article by Len Cardinal for information on improving performance with Response.Buffer and avoiding context switching: http://msdn.microsoft.com/en-us/library/ms972335.aspx#asptips_topic15.

    0 讨论(0)
  • 2020-12-28 15:00

    I prefer the <%= %> method in most situations for several reasons.

    1. The HTML is exposed to the IDE so that it can be processed giving you tooltips, tag closing, etc.
    2. Maintaining indentation in the output HTML is easier which can be very helpful with reworking layout.
    3. New lines without appending vbCrLf on everything and again for reviewing output source.
    0 讨论(0)
  • 2020-12-28 15:01

    Many of the answers here indicate that the two approaches produce the same output and that the choice is one of coding style and performance. Its seems its believed that static content outside of <% %> becomes a single Response.Write.

    However it would be more accurate to say the code outside <% %> gets sent with BinaryWrite.

    Response.Write takes a Unicode string and encodes it to the current Response.CodePage before placing it in the buffer. No such encoding takes place for the static content in an ASP file. The characters outside <% %> are dumped verbatim byte for byte into the buffer.

    Hence where the Response.CodePage is different than the CodePage that was used to save the ASP file the results of the two approaches may differ.

    For example lets say I have this content saved in a standard 1252 code page:-

    <%
         Response.CodePage = 65001
         Response.CharSet = "UTF-8"
     %>
    <p> The British £</p>
    <%Response.Write("<p> The British £</p>")%>
    

    The first paragraph is garbled, since the £ will not be sent using UTF-8 encoding, the second is fine because the Unicode string supplied is encoded to UTF-8.

    Hence from a perfomance point of view using static content is preferable since it doesn't need encoding but care is needed if the saved code page differs from the output codepage. For this reason I prefer to save as UTF-8, include <%@ codepage=65001 and set Response.Charset = "UTF-8".

    0 讨论(0)
  • 2020-12-28 15:02

    From a personal preference point of view I prefer the <%= %> method as I feel it provides a better separation variable content from static content.

    0 讨论(0)
提交回复
热议问题