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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀