Skip to content

Instantly share code, notes, and snippets.

@0-Sony
Last active July 24, 2017 14:15
Show Gist options
  • Save 0-Sony/e16a7c2914c7fc658add to your computer and use it in GitHub Desktop.
Save 0-Sony/e16a7c2914c7fc658add to your computer and use it in GitHub Desktop.
Use only one template to display product using a helper
<?php
class Namespace_Catalog_Helper_Data extends Mage_core_Helper_Abstract {
const BLOCK_TYPE = "namespace_catalog/product_list_item";
const TEMPLATE = "catalog/product/list/item.phtml";
/**
* Get product list item HTML
*
* @param Mage_Catalog_Model_Product $product
* @param null|string $template
*
* @return mixed
*/
public function getListItemHtml(Mage_Catalog_Model_Product $product, $template = null)
{
return Mage::app()->getLayout()
->createBlock(self::BLOCK_TYPE)
->setTemplate($template ? $template : self::TEMPLATE)
->setProduct($product)
->toHtml();
}
}
<?php
/**
* This file is part of Namespace_Catalog for Magento.
*
* @license All rights reserved
* @author Phuong LE <phuong.le@agence-soon.fr> <@>
* @category Namespace
* @package Namespace_Catalog
* @copyright Copyright (c) 2015 Agence-Soon (http://agence-soon.fr)
*/
/**
* Product_List_Item Block
* @package Namespace_Catalog
*/
class Namespace_Catalog_Block_Product_List_Item extends Mage_Catalog_Block_Product_Abstract
{
//Add your additionnal methods
}
<?php
/** Build Your template for your item
* Make sure your class Block return your product
* Here we have a basic example of template
**/
/** @var Namespace_Catalog_Block_Product_List_Item $this **/
$_product = $this->getProduct();
$_imageWidth = 200;
$_imageHeight = 200;
?>
<div class="product-list">
<ul>
<li><?php echo $_product->getName(); ?></li>
<li><?php echo $_product->getDescription(); ?></li>
<li><?php echo $_product->getPrice(); ?></li>
<li><?php echo $_product->getMyCustomAttribute(); ?></li>
</ul>
<img src="<?php echo $this->helper('catalog/image')->init($_product,'image')->resize($_imageWidth , $_imageHeight);?>"
</div>
<?php
/** Make sure that your class Block return Items
For example Mage_Catalog_Block_Product_List
**/
$_items = $this->getItems();
$_myHelper = Mage::helper('namespace/catalog');
?>
<div>
<?php foreach ($_items as $_item) :?>
<?php echo $_myHelper->getListItemHtml($_item); ?>
<?php endforeach ;?>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment