Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8432265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:04:56+00:00 2026-06-10T06:04:56+00:00

Smarty Templating for php lets your write output filters that are called everytime fetch()

  • 0

Smarty Templating for php lets your write output filters that are called everytime fetch() or display() 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).

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.

The code I have works fine:

function tidy_html(&$output, &$smarty){
     $config = array(
           'indent'         => true,
           #'output-html'   => true,
           'wrap'           => 0,
           'drop-proprietary-attributes'    =>    false,
           'indent-cdata' => true,
           'indent-spaces' => 5,
           'tab-size' => 5,
           'show-warnings' => true
         #'markup' => false ,
         #'sort-attributes' => 'alpha',
         #'char-encoding' => 'utf8'
    );
    try {
        $tidy = new tidy;
        $tidy->parseString($output, $config, 'utf8');
        $tidy->cleanRepair();

    } catch (Exception $e) {
        $tidy = $output;
    }
    return $tidy;
}


$view->register_outputfilter('tidy_html');

However because it runs on pieces as fetch() or display() 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T06:04:58+00:00Added an answer on June 10, 2026 at 6:04 am

    I turned on php tags in the templates and put this at the beginning of the file that is called on any request:

    {php}
        ob_start('tidy_html_buffer');
    {/php}
    

    And this at the end of the file:

    {php}
        ob_end_flush();
    {/php}
    

    And this is the callback function:

    function tidy_html_buffer(&$output){
        $config = array(
            'indent'         => true,
            #'output-html'   => true,
            'wrap'           => 0,
            'drop-proprietary-attributes'    =>    false,
            'indent-cdata' => true,
            'indent-spaces' => 5,
            'tab-size' => 5,
            'show-warnings' => true
            #'markup' => false ,
            #'sort-attributes' => 'alpha',
            #'char-encoding' => 'utf8'
        );
        try {
            $tidy = new tidy;
            $tidy->parseString($output, $config, 'utf8');
            $tidy->cleanRepair();
    
        } catch (Exception $e) {
            if(!empty($e)) print_r($e);
            $tidy = $output;
        }
        #print_r($tidy);
        return $tidy;
    }
    

    Since gzip compression is on you can’t get performance benefits from flushing early so this achieves the result with minimal overhead or performance hits especially after cached.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Besides the existence of Smarty.class.php (which is also arguable), how do we know that
I work on a very high trafficked website that uses a Smarty templating system.
Are there any third-party Templating Engines for Asp.net like we have smarty,savant for php
Today at work someone tried to convince me that: {$obj->getTableInfo()} is fine for smarty/mvc/templating
I found that using Smarty with PHP, sometimes extra time will need to be
I have a smarty template, that I want to display a random sentence on
I was looking at templating systems for php, and I've come to believe that
I have the following string in the smarty (php templating system) variable $test: <img
I believe Smarty templates has functionality built in that allows you to manage your
What is wrong with this Smarty php templating code? If I include either of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.