Search code examples
magento-1.4magento-1.5magento

Can I display Categories On Homepage using a template in magento?


I have created a simple customized module that shows me categories on home page,and to do it I created a model file and a Block file in my customized module. and entered this line in my CMS (Home page)

{{block type="Definitivemagento_Helloword/randomproducts"}}

now i wish to do same with using a template (Phtml file and Xml file) because I want to add some some css and designing part which i would like to do with Phtml file. Is It Possible To do.. here goes my current code for model class and Block Class.

class Pragtech_Sweet_Block_Category extends Mage_Core_Block_Template
{
    protected function _toHtml()
    {   $catModel = Mage::getModel('Pragtech_Sweet/category');
        $myCategory = $catModel->getCategory();
        $html = "<ul>";
        foreach ($myCategory as $category)
        {
        $name = $category->getName();

        $categoryLink = this->helper('catalog/category')->getCategoryUrl($category);
        $html .= "
        <p>
        <a href='$categoryLink'>$name</a><br/>
        <!-- $name <br/> -->
        </p>";
        }
        $html .= "<ul>";
        //echo "<pre>"; print_r($html); exit;
        return $html;
    }
}

And My Model Class Is As :-

Class Pragtech_Sweet_Model_Category extends Mage_Core_Model_Abstract
{
    public function getCategory()
    {
        $categoryCollection = Mage::getModel('catalog/category')
        ->getCollection()
        ->addAttributeToSelect('name');

        $categoryCollection->getSelect()->order(entity_id);
        //print_r($categoryCollection); exit;
        return $categoryCollection;
    }

Solution

  • You can render any arbitrary block from another phtml block template with something like

    <?php echo $this->getLayout()->createBlock('definitivemagento_helloword/randomproducts')->toHtml(); ?>