1

I use the following code to load a block in controller

$this->loadLayout();
 if ($this->getLayout()->getBlock('checkout.cart1')) {
 $cartlink = $this->getLayout()->getBlock('checkout.cart1')->toHtml();
 }
  1. Here the checkout.cart1 is block name.
  2. My layout file code is

    <quickbuy_index_index> 
    <reference name="content">
    <block type="checkout/cart" name="checkout.cart1">
    <action method="setCartTemplate"><value>checkout/cart1.phtml</value></action>
    <action method="setEmptyTemplate"><value>checkout/cart/noItems.phtml</value></action>
     .
     .
     .
    </block>
    </reference>
    </quickbuy_index_index>
    
  3. Here my tag is <quickbuy_index_index>

  4. Now the controller code not load my block .
  5. But When i put the layout code within <default> tag instead of <quickbuy_index_index> tag, the controller load the block. like this
<default>
<block type="checkout/cart" name="checkout.cart1">
<action method="setCartTemplate"><value>checkout/cart1.phtml</value></action>
<action method="setEmptyTemplate"><value>checkout/cart/noItems.phtml</value></action>
 .
 .
 .
</block>
</default>

6. How i load the block in controller from <quickbuy_index_index> tag.

  1. My problem is, When i put a block in <default> tag, the controller load it. when i put a same block in url tag (<quickbuy_index_index>) the controller not load it.
asked Mar 4, 2016 at 6:08
2
  • are you using ajax to load this block? Commented Mar 4, 2016 at 6:21
  • yes i using ajax to load this block Commented Mar 4, 2016 at 9:44

2 Answers 2

3

To load a block in controller file:

$block = $this->getLayout()->createBlock('checkout/cart')->setTemplate('checkout/cart1.phtml');
$html = $block->toHtml();
answered Mar 4, 2016 at 6:21
0

You need to simple render layout in your controller action as below

public function indexAction()
 {
 $this->loadLayout();
 $this->renderLayout(); 
 }
answered Mar 4, 2016 at 6:21

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.