HTML Tidy is my daddy!

I will admit I tend to be a little picky when it comes to how my code looks, HTML included. I hate having to go through all my PHP to try and make sure all my included files are outputting in the same structure as everything else so that when I view the source of the final product it is all nice and readable. Most people probably don’t care. There are others that don’t want any formatting at all believing that it saves load time not having all those extra tabs and spaces. Which is true by the way…remove all formatting from your html and you won’t believe how much it lowers your file sizes, especially with large web sites. But when it comes to maintaining the site (if you aren’t using some wysiwyg) you will curse the day you removed all that nice structure.

Earlier today I was working on this new WordPress theme and getting frustrated at how many of the WordPress functions don’t have any concept of structure. Before I decided to track down and modify their functions myself or even worse, scrap the project altogether, I thought why not give Tidy a shot. I have never really used it before and now that I see how awesome it is I’m kicking myself for not trying it sooner!

Imagine if you will, having the ability to structure the HTML in your PHP files however you like and the output HTML being either perfectly structured HTML or void of any structure at all without having to do a damn thing! Sounds nice doesn’t it? Well my friend, that time has come. That is of course assuming your hosting provider has lib-tidy installed and enabled on your server. If I am hosting your site it is already enabled and you may proceed with fortitude. Otherwise read up on what you need to do at php.net.

To get started, do something like this above your html code:

<?php ob_start(); ?>
<html>

And then below your html:

</html>
<?php
$html = ob_get_clean();
$config = array(
  'indent' => true,
  'output-xhtml' => true,
  'wrap' => 0,
  'indent-spaces' => 4,
  'add-xml-decl' => true,
  'doctype' => 'strict'
);
$tidy = new tidy();
$tidy->parseString($html, $config, 'utf8');
$tidy->cleanRepair();
echo $tidy;
?>

And that’s it! If tidy is set up correctly you should be able to view the source of your site and be amazed at the majesty of it all. To remove the formatting just change indent in the config array to false. If you want to play around with the options a bit more than that check out the quick reference. And if you ever run in to Dave Raggett give him a big hug for the both of us!

Comments

Leave a Reply