智能输出过滤器

问题描述 投票:0回答:1

适用于php的Smarty模板技术使您的写入输出过滤器在每次调用fetch()display()时都被调用。 Smarty还使用输出缓冲区,您无法创建自己的缓冲区(当另一个缓冲区仍处于活动状态时,您将没有输出缓冲区)。

我的问题是,我想在完成整个文档后对其进行整洁,而不是在显示模板时将其分成几部分。我无法将我正在使用的软件重写到只能使用显示或获取一次的程度,但是我仍然需要立即使用输出过滤器/整理器,然后才对整个文档立即进行输出刷新。 。但是我认为聪明的人无法做到这一点。

我拥有的代码可以正常工作:

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');

但是因为它以fetch()display()的形式运行,因此,如果说该文件中没有表格,则它将为我关闭它们并破坏布局,从而破坏了我的网站。由于该软件的设置方式,大多数都可以正常显示,只有在关闭表格和一些div框时才会出现问题,它将页面放入块中,并在每个块上显示调用。如果该块包含一张表,它将尽早关闭它们,从而导致布局损坏。至少我认为这是正在进行的任何帮助将不胜感激。也许即使在聪明的控制之下,也有可能在刷新输出缓冲区之前抢走输出缓冲区?

php smarty tidy
1个回答
2
投票

我打开了模板中的php标记,并将其放在可根据任何请求调用的文件的开头:

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

并且在文件末尾:

{php}
    ob_end_flush();
{/php}

这是回调函数:

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;
}

由于gzip压缩已启用,因此您无法从早期刷新中获得性能优势,因此以最小的开销或性能影响达到结果,尤其是在缓存之后。

© www.soinside.com 2019 - 2024. All rights reserved.