Wednesday, April 25, 2007

Bandwidth saver

I never miss an opportunity to push for Freemarker, the most powerful template engine in the world, so here's a quick tip on how to strip all whitespace from your HTML pages:

<#assign out>
<#compress>
!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<HEAD>

// Your page here

</BODY>
</HTML>
</#compress>
</#assign>
${out?replace('\n','')}


It assigns the compressed body of the outer assign macro to the variable "out", and the finally writes the value of "out" with all newlines stripped.

The entire page is now a singe line of HTML - a developer's nightmare, but a nice a bandwidth saver.

Combined with Sitemesh, another one of my favorite tools, this is easily and transparently applied to all your pages.

No comments: