Doymer Consultores

  • Increase font size
  • Default font size
  • Decrease font size
Inicio Blog Joomla! Understanding Joomla! 1.5 - Part Two

Understanding Joomla! 1.5 - Part Two

Print PDF

On a former article I explained the basic principles of the MVC model that the Joomla! 1.5 framework is based on. At that time I stripped the leaves of the 'C' from the acronim MVC and we stopped at the boundaries of the 'V'.

For all those that kept looking forward for something more we are trying in this second part to clarify how does this part of the model works so that you can use the flexibility it contributes to, to your own developments.

So, what is a view

Last time we kept asking what a view was. Well, a view is, simply put, a subset of data. It's the same concept as views in SQL parlance. You deliver different sets of your data with different views. So you could have a detailed data view and a resumed data view, the later presenting a subset of the whole data collected in the former. A dalily data view and a monthly data view, etc.

As you can have multiple views in our component, Joomla! uses the 'views' folder in your component's base folder to keep things tidy. This folder is only a placeholder of your views. This means that you have to create the 'views' folder and now you could have something like this in your hard disk:

<name> base folder
'views' folder

Inside the 'views' folder other folders hold the files of each view you want to have. What does the Joomla! framework do with this?. To try to import a file named 'view.html.php' that should exist in your view directory. A bit messy?. I know. I'll try to explain...

When you built your request you included a parameter named, guess... yes, 'view' that told the MVC model what view you wanted. Or that you should have included because there is not such a thing like a default view. So your URL was something like:<myview>[&task=<mytask>]

The task parameter may or may not exist. Remember that if you omit it you are defaulting to task=display. With this URL Joomla! is importing a file located at <site root dir>/components/com_travels/views/<myview>/view.html.php. If this file or the path do not exist Joomla! will fail. By simply swapping the value of the 'view' parameter you deliver different views of your data with few or no effort. Cute, isn't it?.

Besides this make a lot of sense. Except for the specific data we need at each page load and the way to present the results all other actions related to a given page are common. Why write again and again this common code? As we see Joomla! enables, this way, code reuse; something very conveniently.

Every request for a view require that you also specify the format you are serving the view. There exist several well known formats such as html (the default one if none is specified), rss(-feed), raw, etc. but you can use your own. If no format is specfied in the request with the 'format=<myformat>' parameter a default value of 'html' is used.

The 'html' format is treated in a special way by J!: It makes the Joomla! framework wrap the response in whatever template your site is using so that you get a fully built HTML page. This way, with very few effort from you side, you get back your page fully loaded with modules or whatever you had configured and in the middle of all this, as page contents, our component output.

The specific format you are selecting is what you have written in the middle part of the name of the file in your view folder (The file we talked about a few lines before 'view.html.php'). If you use a different format like 'rss' your file should be named after it like view.raw.html, get it?.

As told before, you can have other formats than html and Joomla! will not wrap the template on them. You could have a 'pdf' format to deliver your data in pdf format or even an 'ajax' format to deliver ajax responses to the front-end easily. Just construct your URL like:

to make the Joomla! Framework look for, and load, a file named 'myview.ajax.php' located at <site root dir>/components/com_travels/views/myview/ from where you can echo anything you want. It's that easy.

Anyway, to achieve our goal we need some code inside the view.<format>.php file. At the moment we have the file, now we need the class. And fortunately you only have to extend the JView class with your own following the strict rules we said that we should follow. In this case you must build your class by concatenating the component name with the word 'View' and the view name. So our class name will be a camelised <Class name>View<Myview>. If your component class is named 'travels' and your view is named 'detail' (URL ...?option=com_travels&view=detail) your view class name should be:

class TravelsViewDetail extends JView 
function display($tpl=null)
echo 'blah, blah';

Within this class you only have to feed the data you want to display for your component under this specific case. You can do this directly by delivering the [X]HTML code directly (in this case be careful to deactivate the php code interpreter by means of a ?> secuence before issuing your html code and restart it agen by means of <?php before the closing bracket of the display function), or through calls to echo inside php tags, or be more subtle and use a layout (more on this later).

Can you have other functions besides 'display' in your view class? I don't know. This is something that the gurus must respond. Where does the display function come from? Again, I don't know. Hope that someone else can help here.


But we can go a step further. Up to this point we have a distributed framework that dissects our request in such a way that allow us to create small and very specific files to react only to specific types of requests. In this way the files that we must process can be very small and adjusted to the situation we are treating, speeding up the global response time of the system by not loading lots of code that will not ever be used with this kind of requests (as in Joomla! 1.x).

Having reached this point we can dissect a bit more and have another layer of detail: the final layout for the data we deliver.

A layout is a way to give format to the data for the active view. The same data set can be delivered under different visual aspects so that the same preparation code (inside the display function of your view class) can present the same data in different ways simply using different files. You 'inject' the view data in the layout template and use the template code to visually format it before reverting to the user (think about column distribution, data ordering and sorting, their position inthe screen and thinks alike).

As before, if you do not specify a layout you go with the 'default' layout. To use layouts you need to create a new folder under the related view folder named 'tmpl' and create a file named <mylayout>.php, nothing more nothing less. If you are using the default layout this file should be named 'default.php'.

The desired layout can be specified in the request by means of a 'layout=<mylayout>' variable or can be locally injected in the function if you manage to get the layout you want to use from other sources. To inject a layout your view class must call 'parent::display();' and pass the layout template name as a parameter. So your class should be something like:

class View extends JView
 function display($tpl=null)
  // Prepare the data
$data1 = ....
$data2 = ....
$moredata[] = array....

// Inyect data
$this->assignRef('variablename', $data1);
$this->assignRef('variablename2', $data2);
$this->assignRef('variablename3', $moredata);

// Inject the layout if not using the request
$this->setLayout('mylayout'); // Local layout inyection

// Do the display. This line exist even in the least case

This way Joomla! will look for a file named 'mylayout.php' in the 'tmpl' folder of the given view. Inside this template file you get a '$this' object that has access to the variables you have injected by means of function assignRef. Accesong object '$this' by means of the construction '$this->variablename' you access the neede variables used to construct your [X]HTML *FINAL* code and the reply to your user.

As a last note say that you can also use what we colud call 'layout templates'. The file name that Joomla! will look for in the tmpl folder is built up by the name of the layout concatenated with underscore and the contents of the $tpl variable. If $tpl is null as in the example before the final name is simply mylayout.php but, if $tpl equals template1, the final name will be mylayout_template1.php thus being able to go down to a finer grain, driving easily your output with much simpler, small and very specific files.


What now. Well there only rest the 'M' (model) part of the initial MVC model but that is another story.

Last Updated on Sunday, 07 February 2010 12:55  


0 # Joomla expert 2011-04-11 13:24
very informative post related to the joomla 1.5 as joomla deals with creating the url that proves to be search engine friendly and suuports SEO work too.
Reply | Reply with quote | Quote
0 # Garth Gerstein 2012-03-06 15:50
Hey Miguel,

I just wanted to say thank you for this tutorial. This is a tricky thing to understand how to use layouts with an ajax type response and almost every tutorial only shows how to use layouts with the html response. At first I created all the output in my view.ajax.php because I could not get the layout to load, but knew this was not a good idea as it would bypass the ability for a designer to override the layout later. This was in fact what I was trying to do initially because a developer bypassed this MVC architecture when creating their component. I ended up rewriting the front end for this component as a result, and really wanted to understand how to use this architecture correctly, but still use xml type ajax responses. This was the first tutorial that made that clear to me how to do that with a call to $this->setLayout(). Thank you so much for taking the time to share your insight. Really helpful!
Reply | Reply with quote | Quote
0 # admin 2012-03-08 09:54
Thank you for taking the time to say it has been helpful for you. We all normally are so busy that we pick what we need and forget to thank for it.
Reply | Reply with quote | Quote
0 # Bill 2013-06-22 22:09
thanks for the article. The link to the previous link leads to the spanish version. If someone else wanna change language it's top right.
Reply | Reply with quote | Quote
0 # João Theo 2018-02-13 05:29
Só para fazer a feitio para vocês verem.
Reply | Reply with quote | Quote
0 # achat arme 2018-02-15 10:49
Armurerie en ligne : Vente d'armes légales en France, vente
libre par internet et sans déclaration. Frais de port
offert à partir de 50€ pour une livraison en France.
Reply | Reply with quote | Quote
0 # Pedro Rafael 2018-02-24 16:19
Esse é melhor método de ocupar novos seguidores de modo segura e também focada
no seu escopo.
Reply | Reply with quote | Quote
0 # Mini Shocker 2018-03-05 17:04 c'est une boutique d'armurerie en ligne avec un large choix de Taser,
Shocker électrique, paralyseur en vente libre 100% légal en France.
Livraison en 48 heures.
Reply | Reply with quote | Quote
0 # matraque de police 2018-03-07 22:09
Tonfas, matraque et bâton de Police en vente sur Spécialiste dans la vente d'armes de défense sur Internet.
Acheter des armes utilisé par la Police en vente libre livré chez vous en 48 heures.
Reply | Reply with quote | Quote
0 # SuzanneChief 2018-03-10 21:50
I have checked your blog and i have found some duplicate content, that's why you don't rank high
in google, but there is a tool that can help you to create 100% unique
articles, search for: Best article rewritwer Ercannou's
essential tools
Reply | Reply with quote | Quote
0 # gel lacrimo 2018-03-20 06:57
Bombe Lacrymogène gel ou gaz, poivre ou spray comment choisir ?
Tous nos modèles et informations de bombes lacrymogène au poivre, gel, gaz ou spray pour les professionnels de la sécurité sont sur Suivez le guide de
la défense en ligne !
Reply | Reply with quote | Quote

Add comment

The owner of this site is not responsible of the opinions that users pour in their comments, and can or cannot agree with what they write.

Fair Play, Please

Please do not make offensive or insult-ant comments. Avoid publicity and Spam. Do not use the comments area to 'plug' your own site. Links you write may be erased. We pretend to create an open space for the authors and users to communicate.

Everyone will enjoy the right use of language, because not all are able to understand 'codified' messages SMS alike. Please do not write only with UPPER case because this is like yelling and you will probably not get attended faster only for yelling, probably the opposite.

Editing reserves

We reserve the right to not include comments that are offensive, unpleasant, that attack third parties (racists, homophobes, etc) or that have nothing to do with the site or the article.
Supplied data is private and owned by you and will not be used to start any commercial or other kind of action.

Security code

Archived Items

Powered by ArtTree