Custom content post procession for TYPO3

Compress or pretty print HTML output by your own

There are many HTML compression extensions for TYPO3 in the TER. Many of them aren't maintained anymore or just have a huge overhead, you will mostly not need for anything. So why not build a content compression by your own?

It's pretty easy to implement. Basically it just require the use of two Hooks from tslib/class.tslib_fe.php. The Hooks are called contentPostProc-all and contentPostProc-output. The first one handles normal pages with cached output, the second one does the same, just for uncached pages with COA or USER_INT objects. This Hooks will be bind to a class and function of your choice in ext_localconf.php of your extension.

As seen above, the class is named ContentPostProcessor and the functions cleanUncachedContent and cleanCachedContent are used. This needs to be created in the correct folder of your module exactly like this.

That basically just does the trick. Now it's possible to compress, pretty print, extend or edit the HTML output in any way you want. Only thing to keep in mind is the time consumption. The Hook for uncached content will be run right in front of the page delivery. The time used there will be added to the normal page load itself. Keep it as small as possible.

For better understanding how this hooks work, I've added a more complex example below. This hooks will remove all comments from the content and tidy the output.

If you want to compress the content instead, it's a good idea to use a third-party library, like WyriHaximus/HtmlCompress. So you don't have to implement anything on your own again. Compression can become very complex otherwise.