【问题标题】:How can I display a magento block in more than one location?如何在多个位置显示 magento 块?
【发布时间】:2013-02-22 00:44:44
【问题描述】:

我正在尝试在其正常位置以及页面底部显示交叉销售块。涉及的 phtml 模板文件不止一个,因此 $this->getChildHtml 在第二个位置不起作用,因为该块设置为目前仅位于 cart.phtml 文件中。

总而言之,我怎样才能在多个模板文件中显示同一个块?我不想将这些更改放在我的 local.xml 中,也不想修改核心 Magento 模板文件。

【问题讨论】:

    标签: magento block


    【解决方案1】:

    此块的核心布局指令设置为 checkout.cart 块的子块:

    <checkout_cart_index translate="label">
        <!-- ... -->
        <reference name="content">
            <block type="checkout/cart" name="checkout.cart">
                <!-- ... -->
                <block type="checkout/cart_crosssell" name="checkout.cart.crosssell" as="crosssell" template="checkout/cart/crosssell.phtml"/>
            </block>
        </reference>
     </checkout_cart_index>
    

    要将其添加到页脚,可能只需要在 local.xml 中建立另一个父子关系:

    <checkout_cart_index>
        <reference name="footer">
            <action method="insert"><!-- or "append" to add to the end -->
                <child>checkout.cart.crosssell</child>
            </action>
        </reference>
    </checkout_cart_index>
    

    这种方法依赖于股票 page/html/footer.phtml 模板,该模板包含一个空的 getChildHtml() 调用,这会导致它呈现所有子块。

    请注意,页脚块类 Mage_Page_Block_Html_Footer 具有永不过期的 block_html 缓存生命周期,并且它不会评估其缓存条目的子内容。可能需要禁用此块的缓存或重写块类以考虑交叉销售块的不同内容。

    【讨论】:

    • 这真的很有帮助。谢谢!为避免缓存问题,我将如何设置自定义块?我知道我必须将引用名称更改为新的块名称,但是如何设置块本身?
    猜你喜欢
    • 2014-11-30
    • 2015-09-24
    • 1970-01-01
    • 2015-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-10
    相关资源
    最近更新 更多