How to load a custom PHP Magento block inside a template file

I created a custom block based on this tutorial http://blog.magikcommerce.com/how-to-show-most-viewed-best-selling-products-in-magento-store

I would like to call a block from my home.phtml file.

I call my static blocks from:

<?php $helper = Mage::helper('cms'); $source = Mage::getModel('cms/block')->load('my-block'); $processor = $helper->getPageTemplateProcessor(); $html = $processor->filter($source->getContent()); echo $html; ?> 

And it works like a charm, of course! 'But how can I load dynamic blocks, in my case, inside template files.

My bestseller.phtml file:

 app/design/frontend/default/default/template/catalog/product/bestseller.phtml 

And my class:

 Mage_Catalog_Block_Product_Bestseller 
+6
source share
2 answers

Loading a block from a template file is a very bad style, but it is possible.

dirty path from template file

 echo $this->getLayout()->createBlock('catalog/product_bestseller')->toHtml(); 

Clean way:
go to your XML layout file, add a block like any other, and access it using

 echo $this->getChildHtml('product_bestseller'); 

if you are on the cms page, use the "Xml Updates Layout" section in the "Design As

 <reference name="content"> <block type="catalog/product_bestseller" name="product_bestseller" /> </reference> 
+17
source

this worked with 1.5.1, also allows you to move the template

 $block = $this->getLayout() ->createBlock('catalog/product_bestseller','product_bestseller', array('template' => 'pathTo/template.phtml')); echo $block->setBlockId('whatever')->toHtml(); 
+2
source

Source: https://habr.com/ru/post/954517/


All Articles