title: Smarty output filter
body: <p>Smarty Templating for php lets your write output filters that are called everytime <code>fetch()</code> or <code>display()</code> is called.  Smarty also uses output buffers and you can't create your own (you can't have an output buffer while the other one is still active).</p>
  
  <p>My question is, I want to run tidy on the entire document after it is done, rather than in pieces as it displays templates.  I can't rewrite the software I'm using to the degree that would make it use display or fetch only once, but I still need to use the output filter/tidy before the output is flushed in smarty, on the entire document at once.  But I see no way inherent in smarty to do this.</p>
  
  <p>The code I have works fine:</p>
  
  <pre><code>function tidy_html(&amp;$output, &amp;$smarty){
       $config = array(
             'indent'         =&gt; true,
             #'output-html'   =&gt; true,
             'wrap'           =&gt; 0,
             'drop-proprietary-attributes'    =&gt;    false,
             'indent-cdata' =&gt; true,
             'indent-spaces' =&gt; 5,
             'tab-size' =&gt; 5,
             'show-warnings' =&gt; true
           #'markup' =&gt; false ,
           #'sort-attributes' =&gt; 'alpha',
           #'char-encoding' =&gt; 'utf8'
      );
      try {
          $tidy = new tidy;
          $tidy-&gt;parseString($output, $config, 'utf8');
          $tidy-&gt;cleanRepair();
  
      } catch (Exception $e) {
          $tidy = $output;
      }
      return $tidy;
  }
  
  
  $view-&gt;register_outputfilter('tidy_html');
  </code></pre>
  
  <p>However because it runs on pieces as <code>fetch()</code> or <code>display()</code> are called, if the closing brackets for say, a table aren't present in that file it closes them for me and disrupts the layout, breaking my site.  Most of it displays fine it only has issues closing tables and some div boxes early because of how this software is set up it puts the page into blocks and calls display on each block.  If that block contains pieces of a table it closes them early resulting in layout breakage.  At least I think that is what is going on any help would be appreciated.  Maybe it's possible to grab the output buffer before it's flushed somehow even tho smarty is controlling it?</p>