joomla add view into another view

arlievanos picture arlievanos · Jan 30, 2012 · Viewed 9k times · Source

Im using joomla MVC and I want to build a form that has different tabs which are different sections of the form with inputs in it. There are some tabs that are common to other forms that I need to include.

I would like to be able to load this common content from a separate file or view so i dont have duplicate code, plus is easier when I need to do a change to the form so I dont have to do it in all the forms. It's like displaying a view inside another view.

Is there a way to accomplish this?

Answer

Craig picture Craig · Jan 30, 2012

A Joomla! provides the loadTemplate method to views.

So if you're currently in a tmpl file loaded for layout edit (ie. tmpl/edit.php ) you can call $this->loadTemplate('tab1'); and Joomla! will load the tmpl/edit_tab1.php file in the same view as your edit.php.

In that same view if you wanted to include say tmpl/other_tab1.php you would have to temporarily set the layout to other, eg. in one of our components during the Run template we need a tab from the Edit template, so we use:

<?php $this->setLayout('edit'); // This is ugly
      echo $this->loadTemplate('plan');
      $this->setLayout('run'); ?>

To load a template from another view alltogether, I think you would have to temporarily over-ride the view value, load the template then restore the view. eg.

$jinput =  JFactory::getApplication()->input;
$jinput->set('view', 'other');
$this->loadTemplate('tab2');
$jinput->set('view', 'original');

NB: this last bit I haven't had time to test but it should work.