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

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?