Compress Your Output With PHP & Savant3



Apache/2.4.18 (Ubuntu) Server at Port 80
" data-via="andrewsirianni">Tweet

Once you've finished your website (NOTE: not that you ever really FINISH the site) and are ready to go live, there are a number of refinements that you can start to look to. One area of refinement is around the performance of your website. And in relation to your code, a way of improving the performance is through "minimising" your scripts.

There are a number of tools available to put your scripts (HTML, CSS or JS) through a "minimiser" to reduce white space. However a great option is to do the minimisation "on-the-fly". The benefit of this approach is that you don't have to compile your client code and that the minimised version is always the current version.

I often use templating engines when building a site on a LAMP platform. My preference for a templating engine on PHP is Savant which is now at Version 3.

Using Savant3, we can display output with the following command:


Using this command, we run the following functions from the Savant3.php file:

public function display($tpl = null) {
  echo $this->getOutput($tpl);

public function getOutput($tpl = null) {
  $output = $this->fetch($tpl);
  if ($this->isError($output)) {
    $text = $this->__config['error_text'];
    return $this->escape($text);
  } else {
    return $output;

So in order to compress our files on-the-fly, all we need to do is change the way that these functions operate. We should look to getOutput() then remove all the whitespace, new lines and comments. The following displayOptimised() show an example of this:

* We will call this function instead of the display($tpl = null).
* This function will return the optimised data.
public function displayOptimised($tpl = null){
  echo $this->fetchOptimised($tpl);

* This function will getOutput($tpl) but then strip all the additional data
public function fetchOptimised($tpl = null){
  $search_pre = array(
    "/ +/" => " ",
    "~[\t\r\n]~" => ""
  $search_post = array(
    "~<!-\{(.*?)\}->|<!-(?!-\[)(.*?)->|<!-(?!-\[)|(?<=\[endif\])->|// <!-|// ->|<!\[CDATA\[|// \]\]>|\]\]>|//\]\]>|//<!\[CDATA\[~" => ""

  return preg_replace(array_keys($search_post), array_values($search_post), preg_replace(array_keys($search_pre), array_values($search_pre), $this->getOutput($tpl)));

You can see the result of this by viewing the source of this web page (Right click with your mouse and select View Source). You will notice that the code is now compacted.

In reality, on a web page of this size, this is likely to have minimal impact. However, as a site grows in size and requests become substantial, this can be a good performance saver. At the same time, it's a fairly simple measure to take!

comments powered by Disqus

About Me

I design & develop software that runs on the Internet. As a qualified analyst, accountant and real estate agent, I can deploy systems that improve performance.


Warning: DOMDocument::load( failed to open stream: HTTP request failed! HTTP/1.0 410 Gone in /home/andrxvjf/public_etc/templates/rmargin/twitter.tpl.php on line 8

Warning: DOMDocument::load(): I/O warning : failed to load external entity "" in /home/andrxvjf/public_etc/templates/rmargin/twitter.tpl.php on line 8

Recent Tweets

Follow me: @andrewsirianni

Copyright ©2012 Andrew Sirianni, All Rights Reserved   LinkedIn Profile