I have been using joomla more than for a year now and familiar with the MVC pattern as well. But I’m unclear how Joomla uses MVC when showing a view. In the components deveopers use below two method.
1st method
class myView extends JView
{
function display($tpl = null)
{
//HTML & PHP goes here
}
}
2nd method
class myView extends JView
{
function display($tpl = null)
{
parent::display($tpl);
}
}
In the 2nd method they create a tmpl folder and place a default.php inside of it. All the HTML and PHP code then goes inside that file.
Now problem is why they use 2nd method when we can right away use 1st method? What’s the industry standard? What are the pros and cons of above methods? What should be used and why and depending on what? Thanks
There is no “industry standard”. But MVC has such concept as “separation of concerns”.
The second approach is much closer to the spirit of MVC-inspired patterns. Views are supposed to be responsible for UI logic. And, if this logic require to generate response, views multiple templates to create it. Or it might choose just to sent an HTTP header.
The first approach is “quick’n’dirt” version. It often means, that UI logic has leaked in controller. Or, that developer does not understand why spaghetti code is a bad thing.