ehz350
ehz350

Reputation: 73

Joomla 3.0 One component with multiple views

I'm new to Joomla and I'm trying to build a single component that shows categories of items, and when a category is clicked, it leads to a second view listing the relevant items. For now, I only can get the first view working. I am not sure what to do for the base file, controller, and view files to get the second view working. I tried looking for an answer for several days but couldn't find anything relevant.

I want to keep it in a single controller and choose the correct view based on the requested task. For now, I have the request as

index.php?option=com_products&task=listing&cat=

There's only going to be 3 total tasks, thus 3 total views. Therefore I didn't want to bother with multiple controllers.

  1. Is it possible to have one controller choose between 3 different views? If yes, how?
  2. Would having multiple views necessitate multiple controllers, to keep everything MVC style? If yes, how do I do that?

Structure:

com_categories
---categories.php
---controller.php
---models\categories.php
---models\listing.php
---views\categories\view.html.php
---views\categories\tmpl\default.php
---views\listing\view.html.php
---views\listing\tmpl\default.php

categories.php

$controller = JControllerLegacy::getInstance('categories');

$controller->execute(JRequest::getCmd('task'));

$controller->redirect();

controller.php

class categoriesController extends JControllerLegacy
{
   /*
   *  Main controller: Shows categories
   *  This is chosen by default.
   */
   function display()
   {
      $view = $this->getView( 'categories', 'html' );
      $view->setModel($this->getModel('categories'), true );
      $view->setLayout( 'default' );
      $view->display();
   }

   /*
   *  Listing controller: Shows list of items after a category is clicked
   */
   function listing()
   {
      // This passes the category id to the model
      $cat = JRequest::getVar( 'cat', '1' );
      $model = $this->getModel('listing');
      $model->setState('cat', $cat);

      $view = $this->getView( 'listing', 'html' );
      $view->setModel($model, true );
      $view->setLayout( 'default' );
      $view->display();

   }
}

listing\view.html.php

class categoriesViewlisting extends JViewLegacy
{
    function display($tpl = null) 
    {
        $doc =& JFactory::getDocument();

        // Assign data to the view
        $this->item = $this->get('Products');
        $this->title = $this->get('Category');

        // Display the view
        parent::display($tpl);
    }
}

Upvotes: 0

Views: 7287

Answers (3)

CHE
CHE

Reputation: 1

You can load multiple views by adding the following function to your Controller:

public function openView( $viewName ) {
        $document = \JFactory::getDocument();
        $viewType = $document->getType();
        $viewLayout = $this->input->get('layout', 'default', 'string');
        $view = $this->getView($viewName, $viewType, '', array('base_path' => $this->basePath, 'layout' => $viewLayout));

        // Get/Create the model
        if ($model = $this->getModel($viewName))
        {
            // Push the model into the view (as default)
            $view->setModel($model, true);
        }

        $view->document = $document;

        // call $view->display() to render

        return $view;
    }

Then in your view, you can load other views like

$this->view1 = $controller->openView('view1');
$this->view2 = $controller->openView('view2');

and display it in the template by

<?php $this->view1->display(); ?>
<?php $this->view1->display(); ?>

Upvotes: 0

RJParikh
RJParikh

Reputation: 4166

You no need to create new controller file for different views. Just you need to copy one of the view folder of component and give it your new viewname. Also create model file for that view.

com_categories
---categories.php
---controller.php
---models\categories.php
---models\listing.php
---views\categories\view.html.php
---views\categories\tmpl\default.php
---views\listing\view.html.php
---views\listing\tmpl\default.php

Just give your link name like index.php?option=com_categories&view=listing

Upvotes: 0

S&#248;ren Beck Jensen
S&#248;ren Beck Jensen

Reputation: 1676

If you only needs views there is no need to even use sub controllers. Instead of using task=taskname simply use view=viewname and then add a view folder inside /components/com_name/views (copy one of the existing).

Or simply skip all this and build it using the component creator.

Upvotes: 0

Related Questions