簡體   English   中英

無法顯示模板中的圖塊-Magento

[英]Cannot display block from template - magento

我正在從模板中調用一個塊,如下所示。 我知道這不是推薦的方法,但是layout.xml方法給我帶來了麻煩

echo $this->getLayout()->createBlock('shoppingbasket/options')
                    ->setTemplate('shoppingbasket/cart/item/default/options.phtml')
                    ->setBlockId('options')
                    ->setProduct($_item->getProduct())
                    ->setSelectedOptions($this->getOptionList())
                    ->toHtml() ?>

塊被渲染。 現在,我正在嘗試通過layout.xml方法添加它

我將塊添加到layout.xml中,如下所示:

<layout>    
    <checkout_cart_index>    
        <reference name="checkout.cart">
            <block type="shoppingbasket/options" template="shoppingbasket/cart/item/default/options.phtml" name="options"></block>
            <action method="addItemRender"><type>simple</type><block>checkout/cart_item_renderer</block><template>shoppingbasket/cart/item/default.phtml</template></action>
        </reference>
    </checkout_cart_index>
</layout>

我從模板中調用該塊,如下所示:

 <?php echo $this->getChildHtml('options') ?> 

塊沒有出現,我嘗試將塊移動到layout.xml中的任何位置。 我究竟做錯了什么? 也有一種方法可以像我一樣使用layout.xml方法將參數傳遞給塊

->setProduct($_item->getProduct())
                    ->setSelectedOptions($this->getOptionList())

謝謝!

您做了正確的一切,除了一件事:您應該設置塊別名-塊的“ as”屬性。

<block type="shoppingbasket/options" template="shoppingbasket/cart/item/default/options.phtml" name="options" as="options"></block>

方法getChildgetChildHtml使用塊別名從布局中檢索塊實例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM