Magento:缩小 HTML 输出?

发布于 2024-10-07 08:30:28 字数 57 浏览 2 评论 0原文

magento中是否有任何文件可以输出所有html?

我想缩小所有 html 输出。

Is there any file in magento where all html will be output?

I want to minify all html output.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

阳光①夏 2024-10-14 08:30:28

Magento 使用响应对象发送所有输出。

所有输出都添加到该对象,然后调用其 sendResponse 方法。

如果您想更改输出,请为 http_response_send_before 事件设置一个侦听器

<!-- in your module's config.xml -->
<http_response_send_before>
    <observers>
        <unique_name>
            <type>singleton</type>
            <class>group/observer</class>
            <method>alterOutput</method>
        </unique_name>
    </observers>
</http_response_send_before>

然后在观察者中您可以获取并设置正文

class Packagename_Modulename_Model_Observer
{
    public function alterOutput($observer)
    {
        $response = $observer->getResponse();       
        $html     = $response->getBody();           
        //modify html here          
        $response->setBody($html);
    }
}

如果您感兴趣,可以在 sendResponse 中调用此事件以下类的 方法

app/code/core/Mage/Core/Controller/Response/Http.php

和输出本身在以下类的 sendResponseoutputBody 方法中发送

lib/Zend/Controller/Response/Abstract.php   

Magento uses a response object to send all output.

All output is added to this object, and then its sendResponse method is called.

If you want to alter the output, setup a listener for the http_response_send_before event

<!-- in your module's config.xml -->
<http_response_send_before>
    <observers>
        <unique_name>
            <type>singleton</type>
            <class>group/observer</class>
            <method>alterOutput</method>
        </unique_name>
    </observers>
</http_response_send_before>

And then in your observer you may get and set the body

class Packagename_Modulename_Model_Observer
{
    public function alterOutput($observer)
    {
        $response = $observer->getResponse();       
        $html     = $response->getBody();           
        //modify html here          
        $response->setBody($html);
    }
}

If you're interested, this event is called in the sendResponse method of the following class

app/code/core/Mage/Core/Controller/Response/Http.php

and the output itself is sent in the sendResponse and outputBody methods of

lib/Zend/Controller/Response/Abstract.php   
天邊彩虹 2024-10-14 08:30:28

理想情况下,您希望在缓存输出之前执行缩小操作,以避免过于频繁地执行此操作。我能想到的最好的地方是覆盖 Mage_Page_Block_Html 并将以下函数添加到您的新类中:

protected function _toHtml()
{
    $html = parent::_toHtml();
    // MINIFY CONTENTS OF $html HERE
    return $html;
}

这样它会对整个页面执行一次操作,然后返回的值可能会被 Magento 缓存在这是通常的方式。它不会单独在每个块上执行,这可能会降低效率。

Ideally you want to perform minification before the output is cached to avoid doing it too often. The best place I can think of is by overriding Mage_Page_Block_Html and adding the following function to your new class:

protected function _toHtml()
{
    $html = parent::_toHtml();
    // MINIFY CONTENTS OF $html HERE
    return $html;
}

This way it performs the action once for the whole page, the returned value may then be cached by Magento in it's usual manner. It's not performing on each block individually which might be less efficient.

镜花水月 2024-10-14 08:30:28

您始终可以使用 ob 函数来获取 index.php 中的输出,然后根据需要处理内容。但我怀疑它是否会像启用 gzip 或 deflate 一样增强您的网站

you can always use ob functions to get the output in index.php and then do with the content whatever you need. but i doubt if it will boost your site as much as enabling gzip or deflate

淤浪 2024-10-14 08:30:28

也许来自 Google 的 mod_pagespeed ?这将为你透明地做到这一点。 +1 表示 gzip 和 deflate 两种方式。

Maybe mod_pagespeed from Google? That would do it transparently for you. +1 for gzip and deflate either way.

沦落红尘 2024-10-14 08:30:28

也许来这里的人可能会发现这个 Magento 扩展很有帮助: http:// www.magentocommerce.com/magento-connect/html-minify-by-jemoon.html

Maybe someone coming here might find this Magento extension helpful: http://www.magentocommerce.com/magento-connect/html-minify-by-jemoon.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文