Here goes MVC again: I was a bit confused that every time I created a second or third view for a component, I had to create a separate model for it. Did not make sense to me - I had the necessary algorithms in the model for the first view, did I really need to do copy & paste? I hate it! Redundancy, redundancy, redundancy.
That case occurred in my current project - I created user profiles and needed an "edit" view. The data is the same, but I need to have the data in editable text fields instead of fixed labels, and maybe some other buttons here and there...
Well, some time later I realized that I just did not quite understand what Joomla! acutally means by "view". And that there is something called "layout".
Long story short: If you have a model that provides all necessary information and you just want to have a different presentation of it, just add a new .php file to your views/<yourviewname>/tmpl folder - let's call it "layout2". Layout2.php has acces to all data default.php can access - they use the same model. If you want to use it on your site, just add &layout=layout2 to your URL (it would then be something like index.php&option=com_yourcomponent&view=yourview&layout=layout2...).
That case occurred in my current project - I created user profiles and needed an "edit" view. The data is the same, but I need to have the data in editable text fields instead of fixed labels, and maybe some other buttons here and there...
Well, some time later I realized that I just did not quite understand what Joomla! acutally means by "view". And that there is something called "layout".
Long story short: If you have a model that provides all necessary information and you just want to have a different presentation of it, just add a new .php file to your views/<yourviewname>/tmpl folder - let's call it "layout2". Layout2.php has acces to all data default.php can access - they use the same model. If you want to use it on your site, just add &layout=layout2 to your URL (it would then be something like index.php&option=com_yourcomponent&view=yourview&layout=layout2...).
THANK YOU. I HATE redundancy with a passion, and the more I study MVC, the more I see what a code-heavy mess it can be if used thoughtlessly. Respect the principles, but common sense does need to come into play. :)
ReplyDelete