Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento call a .phtml file in template with product collection

I can call a .phtml file to my .phtml template like a list.phtml.

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

But in test.phtml i cannot call $_product values.

For example:

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

works

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

not works:

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

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

Do I must load full collection in product again in each included file, how can i get $_product values in test.phtml most effective way?

like image 471
Martin Avatar asked Mar 08 '14 06:03

Martin


People also ask

How can I call Phtml file in product detail page in Magento 2?

Call phtml using block codelogin to magento admin. Open any cms page and write block code in content section. After using the block code phtml file will be called on cms page. If you want to call phtml file on all cms pages then you can create a layout file to achieve this.

How do you call one Phtml file into another Phtml file in Magento 2?

then you can use like refrenceBlock="mytestblock" to call another phtml using layout. You want to call phtml into another phtml then you can use this code. <? php echo $this->getLayout()->createBlock("Magento\Framework\View\Element\Template")->setTemplate("Magento_Test::newtest.


2 Answers

There are two options:

  1. You can load product by Mage::getModel('catalog/product')->load(<product_id>) with id each time within foreach loop.

  2. Use below

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

instead of

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

like image 87
Mohit Kumar Arora Avatar answered Sep 30 '22 14:09

Mohit Kumar Arora


You can assign template through controller like @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();
like image 24
raheem.unr Avatar answered Sep 30 '22 16:09

raheem.unr