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
developerWorks - FREE Tools! |
Build secure Web services with transport-level security using IBM Rational Application Developer V7 and IBM WebSphere Application Server V6.1. Follow this three-part series for step-by-step instructions about how to develop Web services and clients, configure HTTP basic authentication, and configure HTTP over SSL (HTTPS). This first part of the series walks you through building a Web service for a simple calculator application. You generate and test two different types of Web services clients: a Java Platform, Enterprise Edition (Java EE) client and a stand-alone Java client. You also handle user-defined exceptions in Web services. FREE! Go There Now!
|
|
|
|
Download a free trial version of IBM Rational Developer for System z, software that can help you deliver core development capabilities; the power of Java Platform, Enterprise Edition (Java EE); and rapid application development support to diverse enterprise application development teams. With comprehensive development tools to help create, deploy and maintain traditional enterprise and composite applications, Rational Developer for System z enables developers with different technical backgrounds to easily participate in important technology projects. FREE! Go There Now!
|
|
|
|
Analysts, architects, and developers who have existing COBOL or PL/I skills and want to extend those skills to deploy new workloads on the mainframe can use the IBM Enterprise Modernization Sandbox for System z to find hands-on walkthroughs of common real world scenarios. The scenarios provide examples of how to rapidly design, create, assemble, test, and deploy high-quality Web, Web services, portal, and SOA applications for IBM CICS, IBM IMS, and IBM WebSphere Application Server. FREE! Go There Now!
|
|
|
|
As organizations have grown increasingly dependent on online software, the risk of malicious attacks has also become far more serious. Fortunately, well-governed organizations can protect their Web applications by injecting vulnerability assessments and ethical hacks into their software development and delivery processes. This paper describes 12 of the most common hacker attacks and provides basic rules that you can follow to help create more hack-resistant Web applications. FREE! Go There Now!
|
|
|
|
Get a free trial download of the latest version of IBM Rational Performance Tester V7.0.1, a load and performance testing solution for teams concerned about the scalability of their Web-based applications. Combining multiple ease-of-use features with granular detail, Rational Performance Tester simplifies the test-creation, load-generation and data-collection processes that help teams ensure the ability of their applications to accommodate required user loads. FREE! Go There Now!
|
|
|
|
Get a free trial download of the latest version of IBM Rational Tester for SOA Quality V7.0.1, a functional and regression testing tool that enables the creation, comprehension, modification and execution of testing GUI-less Web services. FREE! Go There Now!
|
|
|
|
You can now evaluate IBM Rational Asset Manager V7.0 online without installing or configuring it on your own system! Rational Asset Manager helps create, modify, govern, find, and reuse any type of development assets, including SOA and systems development assets. Rational Asset Manager helps you reduce software development costs and improve quality by facilitating the reuse of all types of software development-related assets. Visit developerWorks to learn more about this product and register to explore its capabilities online. FREE! Go There Now!
|
|
|
|
In this webcast, you'll get an introduction to the eXtreme Transaction Processing (XTP) features of WebSphere Extended Deployment and the common architectural traits required by XTP applications. See how WebSphere Extended Deployment's ObjectGrid feature provides a state-of-the-art infrastructure for hosting XTP applications. FREE! Go There Now!
|
|
|
|
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!
|
|
|
|
With IBM Rational Systems Development Solution, you can deliver products faster with higher quality. Within this kit, Read the “Model Driven Systems Development” white paper to see how to improve product quality and communication. Then check out the rest of the e-Kit to learn more about important topics that can affect the success of any software project through customer examples, tutorials, informative Webcasts, and best practices for designing, building and managing systems. From start to finish, at every stage in your projects, Rational Systems Development Solution can help your company reach its full potential. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |