3

I know there are several similar topics around but I read and tried most of them but still can't figure out how to do this.

I have a written a component in Joomla 2.5 and it works so far. I have different views and I can load the views using the controller.php. One of the views shows a table out of my data base (data about teams).

Now I'd like to have another layout of the same view which would display the data base table as a form so can change the content.

That's the file structure:

views/
- teams/
- - tmpl/
- - - default.php
- - - modify.php
- - view.html.php

That's out of the view.html.php file:

...
// Overwriting JView display method
function display($tpl = null) {

    ...

    $this->setLayout('modify');
    echo $this->getLayout();
    // Display the view
    parent::display($tpl);
}

I tried different combinations of setLayout, $tpl = ..., default_modify.php, etc. but I always either get the default layout or some error like 'can't find layout modify'

I load the site with .../index.php?option=com_test&task=updateTeams

And the controller.php looks like this:

function updateTeams(){
    $model = $this->getModel('teams');
    $view = $this->getView('teams','html');
    $view->setModel($model);

    $view->display();
}
tereško
  • 58,060
  • 25
  • 98
  • 150
jost21
  • 1,084
  • 3
  • 15
  • 29

3 Answers3

5

I had a similar problem, I created some kind of user profile view and wanted them to be able to edit the fields without having to create a new model for it (would have similar functions, hate redundancy...). What worked for me is to simply call the layout like this:

index.php?option=com_mycomponent&view=myview&layout=edit ("edit" would be "modify" in your case)

To do this I didn't touch the view.html.php (well I did at first but I didn't have to.). And you don't need to use the controller either. If you want to load the modify view, just add a button to your regular view linking to the modify layout. No need to change anything else.

I happen to have written a blog article about it, check it out if you want: http://violetfortytwo.blogspot.de/2012/11/joomla-25-multiple-views-one-model.html

Hope this helps.

elk
  • 687
  • 6
  • 13
  • Thank you, I will definitely try that tonight and see if it works for me. I probably also try to do an new view as well, since I wanna learn how all this works. Just out of interest, could you figure out how do the same without using a different url (...&layout=edit) but using the setLayout() method in the view.html.php? Because if I use getLayout() it says 'modify' but it actually shows the default.php layout. – jost21 Sep 17 '13 at 16:48
  • Well I remember that I tried a lot of different things, too, but I ended up using the URL because it worked :-). But I just had a look at the viewhtml API (here: http://api.joomla.org/Joomla-Platform/View/JViewHtml.html) and found that there is a method called "setPath()" which sets the path to the layout. Did you try using this one in addition to setLayout()? Another thing I remember: I had some problems with the display() method not acting as I wanted it to. I just put a mydisplay() method into the view.html.php and let it call parent::display(). You could try putting your stuff there... – elk Sep 17 '13 at 22:08
  • It doesn't work for me so far. But I made up a new component that does nothing except showing 'default' and a second layout that shows 'alternative'. Both methods setLayout() and the url one work there pretty well. So something else must be interfering in my other component?!? – jost21 Sep 18 '13 at 04:27
  • 1
    I figured it out. I simplified the code I posted here, I missed that my layout name is actually modifyTeam.php and the capital 'T' was the problem. I remembered that I actually read before that one should avoid capital letters in the file names. My bad – jost21 Sep 18 '13 at 06:17
0

Ok this is the problem .. you don't want another layout, you want a new MVC triad that is based on forms rather than rendering. So if you look at any of the core content components you will see in the backend they have a mvc for say ... contacts and one for contact and contact is the editor. If in the front end you will notice that com_content and com_weblinks have mvc for artice/weblink and then separate ones for editing.

You need a really different model and layout and set of actions for editng than for just rendering.

Elin
  • 6,507
  • 3
  • 25
  • 47
  • Sorry, I'm pretty new with MVC and a little confused. So I need a new model, a new view and a new method in the controller? I thought I might need an separate view, but why a extra model. Wouldn't many of the methods be the same or at least really similar? – jost21 Sep 15 '13 at 21:17
  • Well some might be the same but rendering and interacting with a form are pretty different. – Elin Sep 18 '13 at 03:44
  • Could I use a common model for both views? I guess that would work, but would it conflict with MVC conventions? I just can't get over the fact that I would have the same functions in two files – jost21 Sep 18 '13 at 06:23
0

Old topic, but it might still help.
It seems that when one wants to change the layout, the $tpl must not be included in the display() or must be null.

So the previous code would be:

function display($tpl = null) {
    /* ... */
    $this->setLayout('modify');
    // Display the view without the $tpl (or be sure it is null)
    parent::display();
}
Axel
  • 3,331
  • 11
  • 35
  • 58
Afonafon
  • 1
  • 1