[英]Magento call a .phtml file in template with product collection
I can call a .phtml file to my .phtml template like a list.phtml. 我可以将.phtml文件调用到我的.phtml模板,就像list.phtml一样。
<?php
echo $this->getLayout()->createBlock('core/template')->setTemplate('goodtest/test.phtml')->toHtml();
?>
But in test.phtml i cannot call $_product values. 但在test.phtml中,我无法调用$ _product值。
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() ?>
. 在文件中: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? 我是否必须在每个包含的文件中再次加载产品中的完整集合,如何才能在test.phtml中获得$ _product值最有效的方法?
There are two options: 有两种选择:
You can load product by Mage::getModel('catalog/product')->load(<product_id>)
with id each time within foreach loop. 您可以在foreach循环中每次使用带有id的Mage::getModel('catalog/product')->load(<product_id>)
。
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 您可以通过控制器分配模板,如@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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.