1

我正在尝试创建一个自定义 magento 块,以便我可以在我的 cart.phtml 上使用它。我已经看到了代码$this->getChildHtml('totals');

我想知道我是否也可以创建一个自定义块然后像这样访问它

$this->getChildHtml('myblock');

任何人都可以指出我的方法或任何参考,因为我没有找到任何有用的资源。

4

1 回答 1

4

创建您的自定义块,为简单起见,我们将只使用 Mage 命名空间,因此我们不需要创建完整的模块,但是您也应该考虑创建自定义模块。

app/code/local/Mage/Checkout/Block/Myblock.php

class Mage_Checkout_Block_MyBlock extends Mage_Core_Block_Template
{
    public function test()
    {
        return 'testing';
    }
}

app/design/frontend/default/default/layout/checkout.xml(使用您的模板配置文件)

<checkout_cart_index translate="label">
    <!-- other code is in here.. -->
    <reference name="content">
        <!-- other code will be here too -->
        <block type="checkout/cart_totals" name="checkout.cart.totals" 
               as="totals" template="checkout/cart/totals.phtml" />

        <!-- Add your block in here.. -->
        <block type="checkout/myblock" name="checkout.myblock" 
               as="myblock" template="checkout/cart/myblock.phtml" />
    </reference>
</checkout_cart_index>

app/design/frontend/default/default/template/checkout/cart/myblock.phtml(或在您的模板自定义)

<?php echo $this->test() // shows "testing" ?>

您可以根据需要在购物车块内使用子块

$this->getChildHtml('myblock');
于 2013-04-19T09:16:28.517 回答