PHP Smarty Tip: Speed Up Your Site With ‘trimwhitespace’

If you use the Smarty template engine for your site then you can take advantage of the included output filter Smarty plugin called trimwhitespace. Trimwhitespace is an output filter, which is basically a plugin that runs after your source code has been created but before it gets displayed or put into a cache file.

The premise for trimwhitespace is simple. Most HTML source code for most websites includes whitespace put there by the web developer for readability. Since this whitespace is not required for the website to display and takes up needed bandwidth, we can eliminate it. Enter trimwhitespace.

To enable trimwhitespace, place this command before your Smarty display() function:

$smarty->load_filter('output','trimwhitespace');

An added benefit is that while the user recieves a trimmed copy of your HTML source code, you can still see your source in its fully tabbed and spaced glory by examining your template files. It’s a win-win situation!

HTML Code Before ‘trimwhitespace’:

<table>
  <tr>
    <td>
      Content
    </td>
  </tr>
</table>

HTML Code After ‘trimwhitespace’:

<table>
<tr>
<td>
Content
</td>
</tr>
</table>

See the difference?