Frontend – List
Let us revisit our frontend controller. Surely by now you’ve gotten a better grasp of the controller and models. So we’ll be revisiting those concepts here. So lets say you want to allow users to view you current Twits as well as create a new Twit.

  1. Some new file structure loving
    app/
      design/
        frontend/
          default/
            default/
              template/
                twit/
                  - twits_list.phtml
      etc/
        modules/
          - SavantDegrees_All.xml (Or what ever your company name might be)
      code/
        local/
          SavantDegrees/ (Or what ever your company name might be)
            Twit/ (Or whatever your module name might be)
              Block/
                Admin/
                  - Main.php
                  Main/
                    - Grid.php
                  - Edit.php
                  Edit/
                    - Form.php
                  - New.php
                  New/
                    - Form.php
                - Index.php
              controllers/
                - AdminController.php
                - IndexController.php
              etc/
                - config.xml
              Helper/
                - Data.php
              Model/
                - Twit.php
                Mysql4/
                  - Twit.php
                  Twit/
                    - Collection.php
              sql/
                twit_setup/
                  - mysql4-install-0.2.0.php
                  - mysql4-upgrade-0.1.0-0.2.0.php

    We’ve added a whole nasty branch of subdirectories under app/designs. Read on to understand what its all for…

  2. Let us return to our IndexController.php
    <?php
    class SavantDegrees_Twit_IndexController extends Mage_Core_Controller_Front_Action
    {
     
        public function indexAction()
        {
    	$this->loadLayout();
    	$this->getLayout()
    		->getBlock('content')->append(
    			$this->getLayout()->createBlock('twit/index')
        	);
            $this->renderLayout();
        }
    }

    Hold your horses, this code is EXACTLY the same as the Part 1. Its just a revision. However, that being said, we see that in Part 1, we made the system create a Block called index. That’s an opening…

  3. So let us modify our index.php block:
    <?php
    class SavantDegrees_Twit_Block_Index extends Mage_Core_Block_Template
    {
        public function __construct()
        {
            parent::__construct();
            $this->setTemplate('twit/twits_list.phtml');
        }
     
        public function getTwits()
        {
        	$model = Mage::getModel('twit/twit');
        	$collection = $model
        	    	->getCollection()
        	    	->load();
     
            return $collection->getItems();
        }
    }

    What the difference here? We made the block load the template from twits/twit_list.phtml. We can create/find this file in /app/design/frontend/default/default/template/twits/twit_list.phtml. Some explanation is due here:

    • When you say setTemplate in the block, it means, use this file as the presentation/view
    • This file exists in the some subdirectory of /app/design/frontend/default/default/template. That is the path to your default template. Even though you may have other templates, this is the default 1. So when Magento cant find your template file in the other template directories, it always reverts back to this folder. Its a directory form of inheritance/ancestory/precedence.
  4. Lastly, the twit_list.phtml file:
    <?php
    $_twits = $this->getTwits();
    if ($_twits) {
    	$count = 0;
    	foreach ($_twits as $i=>$twit) {
    ?>
    		<div class="twit">
    			<div class='name'><?= $twit->getName() ?></div>
    			<div class='summary'><?= $twit->getSummary() ?></div>
    		</div>
    <?php
    	}
    }
    ?>

    Since the whole purpose of twits_list.php is to prepare the view for the list of twits, it obviously needs to get the data from somewhere. It turns out, the Template is the extension of the Block. So by calling $this->getTwits(), we’re calling the Block's getTwits() method.

  5. There you have it! check your twits list at http://127.0.0.1/magento/index.php/twit

6 Responses to “Howto: Repackageable custom extension development in Magento – Part 9 – Frontend – List”

  1. Zoran Says:

    Excellent posts you got here… actually this is the best i could find about Magento, i am new at Magento but will start work at company pretty soon and main job will be Magento, i am still fighting around with the code, but your tutorial explains so much, though i have some questions, i need to look at the code first and try to cook it myself then will bother you :) .
    Thanks again
    Zoran

  2. Craig Says:

    Brilliant tutorial! There’s NOTHING on the web that I have found that goes into this much depth. I really think I can now do a bit of Magento development. Thanks gaweee!

  3. Colin Says:

    This series is fantastic, and I recommend it to everyone who wants to develop a module with CRUD functionality, but I do have a bit of a request. I’ve followed this series and have created a working module, but now I would like to create roles that can access only this module. I’m having a hard time getting this working. Any advice or chance of an additional article on the subject? Thanks,
    -Colin

  4. Clint Says:

    Thank you for this how-to. It has been more than helpful to get started in the complex world of Magento and the Zend Framework. And to think that I thought I could program in PHP…

  5. DCKAP Says:

    I hope it can function without any error.. Its helps…

  6. Tips for Twits » Blog Archive » Howto: Repackageable custom extension development in Magento – Part 8 – CRUD – Update Says:

    [...] And we’re finally finally finally done! To test out the edit function, simply click on any row from the Grid! Thats assuming of course that you actually have some data. For frontend stuff, carry on … Fontend – List [...]

Leave a Reply