I've created a basic module that has a block (extending Mage_Catalog_Block_Product_List). The purpose of the module is list products of a certain attribute set, and allow the user to filter them by defined attributes, in a certain way. 
I have the module/block working, being called from a CMS page as follows:
{{block type="abc_pdn/list" name="pdn" template="catalog/product/list.phtml" attribute_set_id="9" attribute_list="rel_manu,rel_make"}} 
This works fine, and allows me to list products using the default Magento template. The bit I'm struggling with is to inject a custom block above the product list that contains 2 dropdowns, one for each of the attributes. I have some code written that will output dropdowns as HTML, but I'm not sure how I add these through the template.
What is the Magento way of adding another block template within my module containing my custom dropdown HTML code?
2 Answers 2
Here is an idea.
Set a custom template for your block instead of catalog/product/list.phtml. Let's call it: abc/pdn/list.phtml.
Now in your block class add a new method that will retrieve the product list block:
public function getProductsBlock(){
 if (!$this->hasData('products_block')){ //cache in variable
 $productsBlock = Mage::app()->getLayout()->createBlock('catalog/product_list')
 ->setAttributeSetId($this->getAttributeSetId()) //pass the attribute set to the product list
 ->setTemplate('catalog/product/list.phtml');//set the template
 $this->setData('products_block', $productsBlock);
 }
 return $this->getData('products_block');
}
In your new template abc/pdn/list.phtml do something like this:
<div class="attribute_list">
 <!-- list here your attribute dropdowns -->
</div>
<?php echo $this->getProductsBlock()->toHtml(); ?><!-- list the products -->
- 
 Thanks, this answer fits in best with how my code is structured. I had to tweak it slightly to get the collection first into a new object, then apply that to the block with->setCollection($collection). Just need to do some tidying up now to make it work correctly. Thanks for your help.fistameeny– fistameeny2013年12月17日 16:34:05 +00:00Commented Dec 17, 2013 at 16:34
If you use the update layout text area of the CMS page this would be easy.
<reference name="content">
 <block type="abc_pdn/list" name="pdn" template="catalog/product/list.phtml" attribute_set_id="9" attribute_list="rel_manu,rel_make">
 <block "your/custom_block" name="custom_block" as="custom_block" template="your/custom/template.phtml"/>
 </block>
</reference>
Now in the catalog/product/list.phtml you can call it using
echo $this->getChildHtml('custom_block');
Explore related questions
See similar questions with these tags.