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?
You can load a different template file for a different view manually, by simply requiring it. The following is for a view called "nameofotherview" with the layout "layoutname". If this is for an admin view use
JPATH_COMPONENT_ADMINSTRATOR
instead.Remember that the data set up in the view class needs to be compatible with the main layout as well as the layout you're loading from elsewhere.
A side effect of doing this is that template overrides won't work. The loadTemplate function is doing a require but it checks the template paths for overrides first.
A Joomla! provides the
loadTemplate
method to views.So if you're currently in a
tmpl
file loaded for layoutedit
(ie.tmpl/edit.php
) you can call$this->loadTemplate('tab1');
and Joomla! will load thetmpl/edit_tab1.php
file in the same view as youredit.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: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.NB: this last bit I haven't had time to test but it should work.