开发者

Layout Cache Issue

开发者 https://www.devze.com 2023-02-27 10:47 出处:网络
I have a custom module I wrote that is pretty basic...it just adds a small block to the footer for tracking using Media Forge.Thetag it adds is different depending on whether you\'re on a product view

I have a custom module I wrote that is pretty basic...it just adds a small block to the footer for tracking using Media Forge. The tag it adds is different depending on whether you're on a product view page or not. This worked GREAT....until I turned on caching. Now, if you flush the cache and load a product view page, you get the correct block for the product view page. If you then go to another page (home, for instance), it still uses the product view page's block. If I flush the cache and reload the home page, it's now using the right one, but if I 开发者_如何转开发go to a product page now, it's using the wrong one there. So it's definitely a cache issue, I just don't understand how I'm supposed to correct this problem.

I'll paste the contents of my files below. I look forward to any responses!

Layout XML file:

<layout version="0.1.0">
    <!-- DEFAULT TAG -->
    <default>
        <reference name="footer">
            <block type="core/template" name="mediaforge_footer" as="mediaforge_footer" template="tracking/mediaforge_default.phtml"/>
        </reference>
    </default>

    <!-- PRODUCT VIEW PAGES -->
    <catalog_product_view>
        <reference name="mediaforge_footer">
            <action method="setTemplate"><template>tracking/mediaforge_product.phtml</template></action>
        </reference>
    </catalog_product_view>
</layout>

config.xml for my custom module:

<config>
    <modules>
        <VPS_Tracking>
            <version>0.1.0</version>
        </VPS_Tracking>
    </modules>
    <frontend>
        <layout>
            <updates>
                <vps_tracking>
                    <file>vps_tracking.xml</file>
                </vps_tracking>
            </updates>
        </layout>
    </frontend>
</config>

Added this to the end of footer.phtml:

<?php echo $this->getChildHtml('mediaforge_footer'); ?>

The module definition is pretty basic and the two template files mediaforge_default.phtml and mediaforge_product.phtml are pretty simple so I won't bother including them.

Any ideas?


In a nutshell, you need to define a cache key for your block, which means you'll need to use something other than Mage_Core_Block_Template. When you create your own block, add this to the constructor:

protected function _construct() {
    $this->addData(array(
        'cache_lifetime' => 3600,
        'cache_key'      => $this->someMethodToDifferentiatePages(),
    ));
}

That last method needs to return a different string to every use case of the block (e.g. one for catalog pages, one for "other" if that's all you need). This will tell Magento which cached version to use

Hope that helps!

Thanks, Joe

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号