Magento calls .phtml file in product collection template

I can call the .phtml file in my .phtml template like list.phtml.

<?php echo $this->getLayout()->createBlock('core/template')->setTemplate('goodtest/test.phtml')->toHtml(); ?> 

But in test.phtml I cannot call the values ​​of $ _product.

For example:

 <?php $_productCollection=$this->getLoadedProductCollection(); foreach ($_productCollection as $_product): ?> 

work

 <?php echo $_product->getName() ?> 

does not work:

 <?php echo $this->getLayout()->createBlock('core/template')->setTemplate('goodtest/test.phtml')->toHtml(); ?> 

In the file: test.html: <?php echo $_product->getName() ?> .

Should I load the complete collection into the product again in each included file, how can I get the $ _product values ​​in test.phtml in the most efficient way?

+7
object php magento
source share
2 answers

There are two options:

  • You can load the product Mage::getModel('catalog/product')->load(<product_id>) with an identifier each time within the foreach loop.

  • Use below

echo $this->getLayout()->createBlock('catalog/product_list')->setTemplate('goodtest/test.phtml')->toHtml();

instead

echo $this->getLayout()->createBlock('core/template')->setTemplate('goodtest/test.phtml')->toHtml();

+7
source share

You can assign a template through a controller, for example @example

 $this->loadLayout(); $listBlock = $this->getLayout()->createBlock('catalog/product_list') ->setTemplate('catalog/product/list.phtml') ->setCollection($collection); $this->getLayout()->getBlock('content')->append($listBlock); $this->renderLayout(); 
+3
source share

All Articles