【问题标题】:Detect home page in Magento .phtml that will work with BLOCK_HTML cache enabled在 Magento .phtml 中检测将启用 BLOCK_HTML 缓存的主页
【发布时间】:2012-09-13 09:53:42
【问题描述】:

我在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(用于应只出现在主页上),即使在 BLOCK_HTML 为 ON 时也能正常工作。

(Magento 1.4.1.1)

【问题讨论】:

    标签: php magento magento-1.4


    【解决方案1】:

    以上答案是最好的解决方案。

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

    到:

    app/code/local/Mage/Catalog/Block/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;
    }
    

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

    【讨论】:

    • 对我的帖子有很好的补充,这行只有一个小错字:Mage::getSingleton('cms/page')->getIdentifier() == 'home' ? '1', '0' - 应该是Mage::getSingleton('cms/page')->getIdentifier() == 'home' ? '1': '0' 才能正常工作
    • 谢谢,它是直接在浏览器中输入的,没有经过测试:)
    【解决方案2】:

    以下是您想要阅读的有关 Block Html 缓存的资源:

    1. magento forum
    2. some blog
    3. inchoo blog

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

    1. 首先 - 为您的 .phtml 文件指定一个自定义块。如果您不知道 Block 是什么,或者不知道如何将块分配给模板文件,请here's 参考 Alan Storm 博客。
    2. 第二个 - 您必须将下一个代码添加到 Block 构造函数:

      $this->addData(array(
          'cache_lifetime' => 3600,
          'cache_tags'     => array(Mage_Cms_Model_Block::CACHE_TAG),
          'cache_key'      => $this->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,
          );
      }
      

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

    【讨论】:

    • 很好的答案,谢谢。我最终选择了 Andrew 的答案,因为这是一个快速修复,适用于我最初未创作的模板。
    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:

      我们使用

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

      【讨论】:

        【解决方案5】:

        最好的方法是:

        1 更新您的布局 XML(local.xml 或主题 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 步在您的后端创建一个 ID 为“footer_block_extra”的新 CMS 块...并添加您的内容。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-08-05
          • 2014-04-26
          • 1970-01-01
          • 2012-09-30
          相关资源
          最近更新 更多