Magento:为不同的页面布局使用不同的页脚


Magento: Using different footers for different page layouts

我已经尝试了各种教程,但我无法让它工作。

基本上,我想在我的主页上有一个不同的页脚。 我已经设置了两个页面布局,并将它们很好地应用于cms页面。

所以在主页布局中,我指的是...

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

在所有其他页面上,这...

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

很简单!然后在页面 xml 中,我修改了引用页脚的部分,如下所示......

            <block type="page/html_footer" name="footer_alt" as="footer_alt" template="page/html/footer_alt.phtml">
            <block type="page/html_wrapper" name="bottom.container" as="bottomContainer" translate="label">
                <label>Page Footer</label>
                <action method="setElementClass"><value>bottom-container</value></action>
            </block>
            <block type="page/switch" name="store_switcher" as="store_switcher" template="page/switch/stores.phtml"/>
            <block type="page/template_links" name="footer_links" as="footer_links" template="page/template/links.phtml"/>
        </block>

          <block type="page/html_footer" name="footer_home" as="footer_home" template="page/html/footer_home.phtml">
            <block type="page/html_wrapper" name="bottom.container" as="bottomContainer" translate="label">
                <label>Page Footer2</label>
                <action method="setElementClass"><value>bottom-container</value></action>
            </block>
            <block type="page/switch" name="store_switcher" as="store_switcher" template="page/switch/stores.phtml"/>
            <block type="page/template_links" name="footer_links" as="footer_links" template="page/template/links.phtml"/>
        </block>

我认为这就是问题所在。以上所有页面都显示"footer_alt"页脚,我不确定为什么。

我可以确认"page/html/footer_alt.phtml"和"page/html/footer_home.phtml"设置正常。

我希望这是有道理的。 谢谢。

所有这些答案对于一个简单的解决方案来说都太复杂了。重写是矫枉过正。重写Magento核心,即使做得正确,也应该始终引发警报,并立即迫使开发人员彻底阅读Magento源代码。根据我的经验,每一次Magento心痛都伴随着一个神秘但完全令人满意的解决方案。这是令人心痛的令人满意的解决方案之一。

毫不奇怪,Magento武断地决定确保页脚模板不会生成唯一的缓存键。这意味着页脚不能根据加载的站点部分而有所不同;需要明确的是,它实际上可以,但前提是禁用块缓存。但是,永远不应该禁用块缓存,因此最终,这相当于被限制为整个站点的单个页脚。

在网站的不同部分想要不同的页脚有一些合法的用例。例如,在结账时:结账应该是身临其境且无干扰的。但是,当网站上的任何页面被点击时,这些页面的页脚将被缓存,然后结帐将显示相同的页脚。

此处描述的解决方案要么需要核心重写(这是不好的),要么需要一些其他条件检查,这不会合理地扩展到几个条件之外。

我的解决方案很简单:向新模板添加一个缓存键。定位给定页面的布局句柄,引用页脚,设置模板,然后添加缓存键。这只是标准的Magento布局XML。此布局 XML 更改单页结帐的页脚,并且仅在单页签出时更改页脚。此外,缓存将继续适用于以这种方式定义的每个唯一页脚。

    <checkout_onepage_index>
        <reference name="footer">
            <action method="setTemplate">
                <template>linusmoneymaker/page/html/checkout-footer.phtml</template>
            </action>
            <action method="setCacheKey">
                <key>your-own-unique-cache-key-for-linus-moneymaker</key>
            </action>
        </reference>
    </checkout_onepage_index>

这工作的原因如下。这是 app/code/core/Mage/Core/Block/Abstract.php 的源代码,它处理所有块缓存:

/**
 * Get Key for caching block content
 *
 * @return string
 */
public function getCacheKey()
{
    if ($this->hasData('cache_key')) {
        return $this->getData('cache_key');
    }
    /**
     * don't prevent recalculation by saving generated cache key
     * because of ability to render single block instance with different data
     */
    $key = $this->getCacheKeyInfo();
    //ksort($key);  // ignore order
    $key = array_values($key); // ignore array keys
    $key = implode('|', $key);
    $key = sha1($key);
    return $key;
}

请注意,如果定义了cacheKey,则该将优先于将从 app/code/core/Mage/Page/Block/Html/Footer.php 中的 getCacheKeyInfo 方法生成的,后者不会生成每个模板的唯一cacheKey。通过提供布局XML的cacheKey,Magento有效地放弃了默认的非唯一页脚cacheKey,转而支持通过布局XML为网站给定部分手动提供的页脚。

这不仅是正确的方式,而且可以无限扩展。网站上的每个页面都可以实际定义自己的页脚。

出现问题的原因应该是洋红色的块缓存。与页眉一样,页脚默认缓存,并且缓存键不包含模板

要验证这是缓存问题,请先尝试以下操作:

检查是否启用了块缓存。然后导航到您的页面。第一页上的页脚应位于以下任一位置。因此,如果您的第一个页面视图是您的 alt 页脚,它将在任何其他页面上,反之亦然。

如果问题是缓存,您应该能够解决这个问题通过重写"Mage_Page_Block_Html_Footer"和修改 getCacheKeyInfo() 以包含如下所示的模板

public function getCacheKeyInfo()
{
    return array(
        'PAGE_FOOTER',
        Mage::app()->getStore()->getId(),
        (int)Mage::app()->getStore()->isCurrentlySecure(),
        Mage::getDesign()->getPackageName(),
        Mage::getDesign()->getTheme('template'),
        Mage::getSingleton('customer/session')->isLoggedIn(),
        $this->getTemplate()
    );
}

这应该可以解决您的问题。

  1. 创建 2 个名为"home_page_footer"和"inner_page_footer"的静态块。
  2. 打开 footer.phtml 并放置以下代码。

    <?php $home = Mage::getSingleton('cms/page')->getIdentifier(); ?>
    <?php if ($home):?>     
    <div style="clear:both;">
        <?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('home_page_footer')->toHtml(); ?>
    </div>
    <?php else: ?>      
    <div style="clear:both;">
        <?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('inner_page_footer')->toHtml(); ?>
    </div>
    <?php endif; ?>     
    

希望它会有所帮助。