【问题标题】:Clean HTML output in ZF2ZF2 中的干净 HTML 输出
【发布时间】:2013-07-20 10:13:24
【问题描述】:

是否可以在 Zend Framework 2 中格式化(清理)html 输出?现在它在没有正确的换行符和制表符的情况下输出。

我知道这种方法:

$dom = new DOMDocument();
$dom->preserveWhiteSpace = FALSE;
$dom->loadHTML($html);
$dom->formatOutput = TRUE;
echo $dom->saveHTML();

但我不明白在哪里可以截获 ZF2 中的输出。任何帮助将不胜感激。

现在所有的 html 代码都是在布局中创建的。

【问题讨论】:

    标签: html frameworks zend-framework2 format output


    【解决方案1】:

    您可以将侦听器附加到“完成”mvc 事件 (http://framework.zend.com/manual/2.1/en/modules/zend.mvc.mvc-event.html) 并让它在发送响应之前清理响应。

    在您应用程序的 module.php 中,将类似以下内容添加到 Module 类中。

    public function onBootstrap(MvcEvent $e)
    { 
        $eventManager = $e->getApplication()->getEventManager();
        $eventManager->attach(MvcEvent::EVENT_FINISH, function(MvcEvent $e){
            $content = $e->getResponse()->getContent();
            $cleancontent = clean_up_html(content); // clean_up_html() not provided
            $e->getResponse()->setContent($cleancontent);
        });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-23
      • 1970-01-01
      • 1970-01-01
      • 2012-06-01
      • 2021-12-04
      • 1970-01-01
      • 2016-09-18
      • 2015-02-13
      相关资源
      最近更新 更多