How can I name a block in phtml instead of layout?

I turned off the layout for the wish list block:

<block type="catalog/product_view" name="product.info.addtoto" as="addtoto" template="catalog/product/view/addto.phtml"/> 

Now I want to call this block in phtml instead of adding it to another layout.

How do i call?

+8
magento
source share
4 answers

While Pratzky is right that this is a bad form (I am already moving forward as such), there were times when they were developing, when it was either a valuable debugging technology, or it made a difference of several hours of programming. In that spirit, this is a bad way to do this:

 <?php print $this->getLayout() ->createBlock("catalog/product_view") ->setTemplate("catalog/product/view/addto.phtml") ->toHtml(); ?> 

Use sparingly, if at all.

+31
source share
  echo Mage::app()->getLayout() ->createBlock('somemodule/someblock') ->setSomeVariable($variable) ->setTemplate('somemodule/someblock.phtml') ->toHtml(); 

it can be used anywhere to call blocks. setSomeVariable($variable) if the set is available in someblock.phtml at $this->getSomeVariable();

+12
source share

Chris. You will need to ever call the block directly from the template. It would be a bad habit / practice. Find the correct link to the template in which you want to add the block, and add it to the xml layout. Then from the template file use:

 echo $this->getChildHtml('your-block'); 
+3
source share

I struggled with this for ages and found that if you want to call a block from a completely separate part of the layout, you need to use a slightly different code. Using:

 <?php echo $this->getBlockHtml('any_block'); ?> 

Instead:

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

Using this code, you can either create your own blocks anywhere, or select blocks from other modules and place them anywhere.

+2
source share

All Articles