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?
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.
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.
There are two options:
You can load product by Mage::getModel('catalog/product')->load(<product_id>)
with id each time within foreach loop.
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();
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With