Doymer Consultores

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

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
controller.php
<component_name>.php
'views' folder
view1
view2
...

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:

http://my.site.com/index.php?option=com_travels&view=<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:

http://my.site.com/index.php?option=com_travels&view=myview&format=ajax

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.

Layouts

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
parent:display($tpl);
}
}

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.

Share
Last Updated on Sunday, 07 February 2010 12:55  

Comments  

 
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 # شرکت فنی مهندسی 2017-02-16 17:20
Wow, that's what I was searching for, what a material!
existing here at this blog, thanks admin of this web page.
Reply | Reply with quote | Quote
 
 
0 # دانلود آهنگ جدید 2017-02-21 14:24
Have you ever considered publishing an e-book or guest authoring on other websites?
I have a blog based on the same information you discuss and would
really like to have you share some stories/information. I know my readers would appreciate your work.
If you're even remotely interested, feel free to shoot me an e mail.
Reply | Reply with quote | Quote
 
 
0 # rio x marketing 2017-05-15 19:24
Haѵe you ever consiɗered about inclսding a little bit more than just yоur articles?
I mean, what you say is valuаble and everything. Nevertheless think of if you added some great graphics or video cliрs to give your pоsts more,
"pop"! Your content is excellent but with pics аnd clips,
this site could certainly be one оf the νerу best in its fiеld.
Great blog!

Also visit mу page rio x marketing: https://www.rioxmarketing.com/agencia-de-marketing-digital-sao-paulo/
Reply | Reply with quote | Quote
 
 
0 # backlinks 2017-05-20 23:19
Thгeе quаrters of adult Internet users utilise social media and time іnvested in the Internet expires two-tһirds from 2009.
One of the intеrnet markеting tools wһich is now increasingly concentrating on is
sеo (SEO). In order to best exploit the opportunities thrown ready to aсcept your company by this concept would be to understand and appreciate the changing dynamics of online searches and after thɑt adaptіng
your ways to better satisfy the end result.



Review my website; bаcklinks: http://www.cldisplay.com/2016/02/10/verizon-3g-rdsl/
Reply | Reply with quote | Quote
 
 
0 # LOL builder 2017-05-30 13:15
I am in fact pleased to glance at this weblog posts which consists of plenty of valuable data,
thanks for providing such data.
Reply | Reply with quote | Quote
 
 
0 # Anemonalove 2017-07-07 05:01
Hello fellas! Who wants to see me live? I'm live at HotBabesCams.com, we
can chat, you can watch me live for free, my nickname is Anemonalove ,
here is my pic:

https://3.bp.blogspot.com/-u5pGYuGNsSo/WVixiO8RBUI/AAAAAAAAAFA/JWa2LHHFI2AkHParQa3fwwHhVijolmq8QCLcBGAs/s1600/hottest%2Bwebcam%2Bgirl%2B-%2BAnemonalove.jpg
Reply | Reply with quote | Quote
 
 
0 # KristinX 2017-07-07 17:00
I see your website needs some fresh posts. Writing manually takes a
lot of time, but there is tool for this boring task,
type in google; murgrabia's tools unlimited content
Reply | Reply with quote | Quote
 
 
0 # http://corta.co 2017-07-19 14:26
Os Impostos são cobrados adrede na Amazon.
Reply | Reply with quote | Quote
 
 
0 # Cassinodigital.com 2017-07-24 15:37
Se vc irá all in, nao irá agitar pos-flop.
Reply | Reply with quote | Quote
 
 
0 # pleadingexchange.com 2017-07-25 13:10
AGREMIAÇÃO DE GEOGRAFIA E ESTATÍSTICA - IBGE.
Reply | Reply with quote | Quote
 
 
0 # Cassino Digital 2017-07-27 00:02
É natural bancar no flop e também foldar no turn.
Reply | Reply with quote | Quote
 
 
0 # rivotram para dormir 2017-09-07 14:18
tenho surfado on-line cerca de três horas por noite , e eu de
nenhuma maneira tinha encontrado algo tão legal como o seu blog.
parabéns, parabenizo pelo seu esforço na criação do seu conteúdo
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
Refresh


Archived Items

Powered by ArtTree