Magento:创建一个自定义块并在cart.phtml中使用

I am trying to create a custom magento block so that i can use it on my cart.phtml. I have seen codes which does $this->getChildHtml('totals'); .

I wonder if i could also create a custom block and then access it like this

$this->getChildHtml('myblock');

Can anyone point me the way to do it or any references as i don't find any useful resources.

Create your custom block, for simplicity we'll just use the Mage namespace so we don't need to create a full module, however you should look into creating custom modules too.

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 (use your templates config files)

<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 (or in your template custom)

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

You can use your child block inside your cart block as your require

$this->getChildHtml('myblock');