dtc9222 2013-04-19 07:10
浏览 32
已采纳

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.

  • 写回答

1条回答 默认 最新

  • dongyu5104 2013-04-19 09:16
    关注

    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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题