ASP
  Home arrow ASP arrow What's Wrong With ASP Render blocks?
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP

What's Wrong With ASP Render blocks?
By: James Shaw
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 2 stars2 stars2 stars2 stars2 stars / 4
    2003-03-07

    Table of Contents:

    Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    See what James Shaw from www.CoverYourASP thinks about render blocks. Once you have read this article you will think twice about using them.

    A simple, normal, ASP page

    Consider this simple ASP file below:

    <%@ Language=JavaScript %>
    <html>
    <head>
       <title>My page</title>
    </head>
    <body>   
    We're in "HTML mode" here...
    <%
       var sScript = Request.ServerVariables ( "SCRIPT_NAME" );
      
    Response.Write ( '<p>I am called ' + sScript + '<p>' );
    %>
    ...and here.
    </body>
    </html>
     
    Most of this file is plain HTML. It is simply streamed out to the browser as-is. The section of JavaScript wrapped in a <% %> is called a render block.

    Render blocks aren't a terribly good idea. Many ASP examples out there use them, but I'm going to try and persuade you never to use them again. This may be an uphill struggle!

    System Inefficiency

    Let's bring out the big guns first. Microsoft doesn't think you should use them. They are inefficient because they force the system to constantly switch back and forth between "HTML mode" and "ASP mode". You know how hard it is to work when your kids are playing in the same room? That's what it's like having to stop and output some HTML when you're busy interpreting script.

    Since we're not likely to be writing the next eBay any time soon, that might not be the most relevant argument, so let's try another.

    Wasting Bandwidth

    You see how the file above is nicely formatted - the <title> tag is indented from the <head> tag? You need to do that when you're coding to make the code easily readable, and hence maintainable.

    But you're punishing your users unnecessarily! They have to download your extra line feeds and tabs, when their browser could care less that they are there. The file below renders exactly the same:

    <%@ Language=JavaScript %>
    <html><head><title>My page</title></head><body>We're in "HTML mode" here...
    <%
       var sScript = Request.ServerVariables ( "SCRIPT_NAME" );
       Response.Write ( '<p>I am called ' + sScript + '<p>' );
    %>
    ...and here.</body></html>

     
    So, the user is now happier because your page loads faster. If you don't think this matters much do a View/Source on some major sites and see the extra baggage they send out. Remove the HTML comments and the extra spaces/tabs and you'll be surprised how much quicker they download.

    I did this exercise on a page generated with FrontPage. It started out as a 40k file - that's a sizeable page to download at 28.8kb. I removed the indentation (which were all spaces) and the HTML comments and the page reduced to 3k. The browser didn't render anything differently, just 10 times quicker!

    But what about readability?
    Ok, let's say you're with me on the bandwidth issue. But now the code isn't readable, right? Here's the real answer (using my Out function to wrap Response.Write):

    <%@ Language=JavaScript %>
    <%
    // the statement below enables buffering of the data sent to the browser.
    // see
    http://learnasp.com/learn/whybuffer.asp

    %>
    <% Response.Buffer=true %>
    <%
          Out ( '<html>' );
          Out ( '<head>' );
          Out ( '<title>My page</title>' );
          Out ( '</head>' );
          Out ( 'We're in "HTML mode" here...' );
    // output the name of the script itself
          var sScript = Request.ServerVariables ( "SCRIPT_NAME" );
          Out  ( '<p>I am called ' + sScript );
          Out ( '<p>...and here.' );
          Out ( '</body>' );
          Out ( '</html>' );
    %>

    Indentation, comments, efficiency. What more could you possibly want? You want MORE?

    Security

    I'm pushing it to say security, but a side-effect of the bandwidth fix is that when everyone in the whole world does a View/Source on your page, they see the almost unintelligible code below. Yes, they can buy a code formatter that will make it look pretty again, but at least admit it's making it more difficult for them?

    <html><head><title>My page</title></head><body>We're in "HTML mode" here...<p>I am called temp.asp<p>...and here.</body></html>

    Try it now - View/Source - apart from the source code blocks it is just one long line of HTML. (Actually there is one exception. If you have to output an HTML comment add a \n (newline) after it)

    It's out there - ASP.NET
    My last ditch attempt to persuade you now - ASP.NET. Well it is coming you know, and ASP.NET doesn't allow render blocks that include functions anymore. So this particularly nasty piece of code (IMHO)...

    <% function DoSomething ( ) { %>
       something in HTML
    <% } %>

    ...now has to be written like this:

    <script language="JavaScript" runat="server">
    function DoSomething ( )
    {
       Out ( 'something in HTML' );
    }
    </script>

     
    I hope you're convinced now? If not, begone! Back to FrontPage with you...


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

    More ASP Articles
    More By James Shaw

     

    IBM® developerWorks developerWorks - FREE Tools!


    NEW! Download DB2 Express-C 9.5

    Visit IBM developerWorks to download IBM DB2 Express-C 9.5, a no-charge version of DB2 Express 9 database server. DB2 Express-C offers the same core data server base features as other DB2 Express editions and provides a solid base to build and deploy applications developed using C/C++, Java, .NET, PHP, and other programming languages.
    FREE! Go There Now!


    NEW! Evaluate IBM Rational Developer for System i V7.1

    Download a free trial version of IBM Rational Developer for System i V7.1, which provides a complete development environment for traditional i5/OS application development. IBM Rational Developer for System i is a new eclipse-based workstation offering for i5/OS application development that provides a comprehensive Integrated Development Environment for edit/compile/debug of traditional RPG/COBOL/C/C++ i5/OS applications.
    FREE! Go There Now!


    NEW! Evaluate Rational Host Access Transformation Services (HATS) Toolkit V7.1

    Visit IBM developerWorks to download a free trial of the Rational Host Access Transformation Services (HATS) Toolkit. The HATS toolkit provides a set of plug-ins for the IBM Rational Software Delivery Platform to help you easily extend your legacy applications. HATS makes your 3270 and 5250 applications available as HTML through the most popular Web browsers, while converting your host screens to a Web look and feel and it also enables you to develop new Web, portal, and rich-client applications.
    FREE! Go There Now!


    NEW! Harnessing the power of SQL and Java for high performance data access

    Join this webcast to see how IBM Data Studio Developer and pureQuery can take the pain out of Java data access. uApplications developed using both Java and SQL have become a common requirement. Database connectivity using Java Database Connectivity (JDBC) to create an application is a multi-step tedious process, and tooling that covers both SQL and Java has been unavailable, until now. IBM Data Studio introduces the pureQuery platform: a high-performance, Java data access platform focused on simplifying the tasks of developing, managing, and optimizing database applications and services.
    FREE! Go There Now!


    NEW! Hello World: Monitor a simple business process using WebSphere Business Monitor V6.0.2

    This tutorial shows new users of IBM WebSphere Business Monitor Version 6.0.2 how to perform the "Hello World" equivalent for monitoring business process applications. It is intended to help you get familiar with the capabilities of the product.
    FREE! Go There Now!


    NEW! Rational Talks to You: Manage RUP-based CMMI initiatives

    Join this Rational Talks to You teleconference on December 4 at 1:00 pm ET to discuss how Rational Method Composer can help meet your compliance objectives. Get your questions answered!
    FREE! Go There Now!


    NEW! The role of integrated requirements management in software delivery

    This paper is about the critical role that a discipline called integrated require­ments management can play in helping to ensure that your business goals and IT investments are continuously aligned—whether you are sourcing, integrat­ing, building or maintaining software. It also looks at ways that automated IBM Rational® products can work together to help you use requirements in the very best way.
    FREE! Go There Now!


    NEW! Try the IBM SOA Sandbox for Connectivity

    Visit IBM developerWorks to try the IBM SOA Sandbox for connectivity. The SOA Sandbox for connectivity provides a trial environment with the tooling and components to help you explore how to effectively connect your infrastructure and integrate all of the people, processes and information in your company. Use the hosted sandbox to explore SOA techniques that streamline connecting existing IT assets together, as well as learn how to connect them to new business logic.
    FREE! Go There Now!


    NEW! Try the IBM SOA Sandbox for Process

    Visit IBM developerWorks to try the IBM SOA Sandbox for process. The SOA Sandbox for process focuses on providing a trial environment with the necessary tooling and components required to gain a better understanding of business processes and how to best improve existing business processes to derive value quickly.
    FREE! Go There Now!


    NEW! Whitepaper: Delivering SOA solutions: service lifecycle management

    The unprecedented scope of a service-oriented architecture (SOA) initiative brings to the forefront a number of management and governance issues that were sidestepped in the past. The key to a successful SOA implementation is managing and governing activities throughout the entire SOA delivery lifecycle by ensuring that services conform to the needs of all of the business’s stakeholders. Learn how service lifecycle management allows the business to ensure that the process by which services are defined, created, tested, deployed, optimized and retired is manageable, repeatable and auditable.
    FREE! Go There Now!



    All FREE IBM® developerWorks Tools!

    ASP ARTICLES

    - Central Scoreboard with Flash and ASP
    - Calorie Counter Using WAP and ASP
    - Creating PGP-Encrypted E-Mails Using ASP
    - Be My Guest in ASP
    - Session Replacement in ASP
    - Securing ASP Data Access Credentials Using t...
    - The Not So Ordinary Address Book
    - Adding and Displaying Data Easily via ASP an...
    - Sending Email From a Form in ASP
    - Adding Member Services in ASP
    - Removing Unconfirmed Members
    - Trapping HTTP 500.100 - Internal Server Error
    - So Many Rows, So Little Time! - Case Study
    - XDO: An XML Engine Class for Classic ASP
    - Credit Card Fraud Prevention Using ASP and C...







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway