Skip to content

HTML auto indenting view loader

Derek Jones edited this page Jul 5, 2012 · 8 revisions

Indents and removes blank lines in HTML+Javascript - CodeIgniter version

(based on a snippet by JonHoo @ http://snippets.dzone.com/)

Takes the output from CodeIgniter (when using views) and reformats (prettyprints) it - ie. fixes bad indenting and missing/extra line breaks in the source code before it is sent to the browser.

It does NOT attempt to replace HTML Tidy, which fixes malformed and/or invalid HTML. This function ASSUMES the coders know what they're doing so the input already contains valid HTML.

The code is not 100% foolproof, but close enough, and surprisingly fast. Even so, I wouldn't recommend using it on high-traffic web sites, since there is a processing overhead for every page load.

DOWNLOAD HERE:

File:MY_Loader.php.zip

HOW TO USE:

Simply unzip and place MY_Loader.php in your ./system/app/libraries/ folder. That's it. All output using views will be reformatted automatically.

Known gotchas:

  1. Two closing Javascript brackets on the same line will only count as one, IF there are non-whitespace characters between them (although I can't think of a good reason why there ever would be -- except in minified Javascript, and the kind of performance geeks who use inline minified Javascript would hardly want the overhead of a function like this)

  2. If a Javascript line containing an opening bracket happens to have a bracketed expression later in the same line, the next line will not be indented (again, outside of minified Javascript, I don't see this happening too often)

  3. The script doesn't ignore tags inside string literals and comments

A lot could be done to make this even better, but I'd rather not sacrifice too much more performance for a detail only nitpicking source-snoopers (like myself) will see.

License: Creative Commons Attribution 3.0 (cc) 2009 Jens Roland

Clone this wiki locally