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();
}
- Here the checkout.cart1 is block name.
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>Here my tag is
<quickbuy_index_index>- Now the controller code not load my block .
- 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.
- 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.
-
are you using ajax to load this block?Qaisar Satti– Qaisar Satti2016年03月04日 06:21:28 +00:00Commented Mar 4, 2016 at 6:21
-
yes i using ajax to load this blockGuest– Guest2016年03月04日 09:44:15 +00:00Commented Mar 4, 2016 at 9:44
2 Answers 2
To load a block in controller file:
$block = $this->getLayout()->createBlock('checkout/cart')->setTemplate('checkout/cart1.phtml');
$html = $block->toHtml();
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
Prashant Valanda
12.7k5 gold badges44 silver badges70 bronze badges
default