3

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

基本上,我想在我的主页上有一个不同的页脚。我已经设置了两个页面布局并将它们应用到 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' 设置正常。

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

4

3 回答 3

8

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

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

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

这里描述的解决方案要么需要核心重写,这不好,要么需要一些其他条件检查,这将无法合理扩展超出几个条件。

我的解决方案很简单:在新模板中添加一个 cacheKey。定位给定页面的布局句柄,引用页脚,设置模板,然后添加 cacheKey。这只是标准的 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定义了 a ,则该 a 将优先于从getCacheKeyInfoin 方法生成的那个app/code/core/Mage/Page/Block/Html/Footer.php,后者不会生成每个模板的 unique cacheKey。通过提供cacheKey来自布局 XML 的内容,Magento 有效地放弃了默认的、非唯一的页脚cacheKey,转而支持通过布局 XML 为站点的给定部分手动提供的页脚。

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

于 2015-12-07T21:55:08.043 回答
3

你问题的原因应该是magento的块缓存。与页眉一样,页脚默认缓存,缓存键不包含模板

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

检查是否启用了块缓存。然后导航到您的页面。第一页的页脚应位于以下任一位置。因此,如果您的第一个页面视图是您的 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()
    );
}

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

于 2013-09-22T18:48:39.833 回答
0
  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; ?>     
    

希望它会有所帮助。

于 2013-09-23T04:00:14.457 回答