繁体   English   中英

检测Magento .phtml中的主页,该主页将与启用BLOCK_HTML缓存一起使用

[英]Detect home page in Magento .phtml that will work with BLOCK_HTML cache enabled

我在catalog / navigation / vert_nav.phtml中尝试了以下两种方法来添加或禁止特定于主页的内容:

if($this->getUrl('') == $this->getUrl('*/*/*', array('_current'=>true, '_use_rewrite'=>true))):

要么

if(
Mage::getSingleton('cms/page')->getIdentifier() == 'home'  &&
Mage::app()->getFrontController()->getRequest()->getRouteName() == 'cms' 
) :

两者都工作正常,但是当BLOCK_HTML缓存打开时,它首先工作,然后一段时间后主页开始显示仅用于其他页面的内容(在我使用较低的else子句之后)。 当我关闭BLOCK_HTML时,它的行为与预期的一样。

有趣的是,我在page / html / head.phtml(对于主页特定的javascript / css)和page / html / header.phtml中使用相同的代码(第一个)(对于应该只出现在页面/ html / header.phtml中的标题横幅)主页),即使BLOCK_HTML为ON,这些工作也很好。

(Magento 1.4.1.1)

以下是您想要了解的有关Block Html缓存的来源:

  1. magento论坛
  2. 一些博客
  3. 英博博客

性能最好不要完全禁用块,而是以智能方式指定缓存键。 所以这就是你应该做的:

  1. 首先 - 为.phtml文件指定自定义块。 如果你不知道什么是座,或如何将块分配给一个模板文件, 这里的参考阿兰风暴博客。
  2. 其次 - 您必须将下一个代码添加到Block构造函数:

     $this->addData(array( 'cache_lifetime' => 3600, 'cache_tags' => array(Mage_Cms_Model_Block::CACHE_TAG), 'cache_key' => $this->getCacheKey(), )); 

    如您所见,我在这里getCacheKey了抽象类Mage_Core_Block_AbstractgetCacheKey方法。

  3. 现在您需要确保cache_key适用于您的逻辑。 Mage_Core_Block_Abstract::getCacheKey使用其他方法,它应该实际指定块的唯一值 - getCacheKeyInfo 您需要使用您的逻辑重新定义它:

     public function getCacheKeyInfo() { $isHomepage = 0; if (Mage::getSingleton('cms/page')->getIdentifier() == 'home') { $isHomepage = 1; } return array( $this->getNameInLayout(), $isHomepage, ); } 

    现在,您可以确定主页的缓存键与缓存键的所有其他页面不同,并且缓存将返回有效信息。

以上答案是最佳解决方案。

你可以简单地复制app / code / core / Mage / Catalog / Block / Nagivation.php

至:

应用程序/代码/本地/法师/目录/座/ Nagivation.php

然后如上所述更改getCacheKeyInfo()方法。

/**
 * Get Key pieces for caching block content
 *
 * @return array
 */
public function getCacheKeyInfo()
{
    $shortCacheId = array(
        'CATALOG_NAVIGATION',
        Mage::app()->getStore()->getId(),
        Mage::getDesign()->getPackageName(),
        Mage::getDesign()->getTheme('template'),
        Mage::getSingleton('customer/session')->getCustomerGroupId(),
        'template' => $this->getTemplate(),
        'name' => $this->getNameInLayout(),
        $this->getCurrenCategoryKey(),
        // Your logic to make home/none home have different cache keys
        Mage::getSingleton('cms/page')->getIdentifier() == 'home' ? '1' : '0'
    );
    $cacheId = $shortCacheId;

    $shortCacheId = array_values($shortCacheId);
    $shortCacheId = implode('|', $shortCacheId);
    $shortCacheId = md5($shortCacheId);

    $cacheId['category_path'] = $this->getCurrenCategoryKey();
    $cacheId['short_cache_id'] = $shortCacheId;

    return $cacheId;
}

这将使主页/非主页页面的缓存键不同,这将缓存两个副本,而不是缓存单个模板副本以在所有页面上使用。

我们用

<!-- SNH CUSTOM -->

    $route = Mage::app()->getFrontController()->getRequest()->getRouteName();

    $action = Mage::app()->getFrontController()->getRequest()->getActionName();

if($route == 'cms' && $action == 'index'):

    <div class="grid_12">

        echo $this->getChildHtml('shopper_footer_partners');

    </div>

endif;

只是添加这些答案建议检查当前页面标识符是否等于“home”。

将它与Mage::getStoreConfig('web/default/cms_home_page')进行比较肯定会更安全。

真正最好的方法是:

1更新布局XML(local.xml或theme custom.xml)

<!--  CUSTOM: ADD NEW FOOTER BLOCK AT BOTTOM FOR PARTNERS -->
<cms_index_index>
    <reference name="footer">
    <block type="cms/block" name="footer_block_extra">
        <action method="setBlockId"><block_id>footer_block_extra</block_id></action>
    </block>
    </reference>
</cms_index_index>

和步骤2将此代码添加到模板phtml中的块(通常是/page/html/footer.phtml)

<!-- SNH CUSTOM -->
<div class="grid_12">
    <?php echo $this->getBlockHtml('footer_block_extra'); ?>
</div>

和步骤3在后端创建一个新的CMS块,ID为“footer_block_extra”...并添加您的内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM