| Understanding Output Overrides in Joomla! 1.5 |
|
|
|
| Tutorials |
| Written by Andrew Eddie |
IntroductionThere are many competing requirements for web designers ranging from accessibility to legislative to personal preferences. Rather than trying to over-parameterise views, or trying to aim for some sort of line of best fit, or worse, sticking its head in the sand, Joomla! has added the potential for the designer to take over control of virtually all of the output that is generated. Joomla! has generally been labeled by some for not giving due attention to accessibility or being archaic in their approach to web standards. However with 1.5, the responsibility, but more importantly the power is back in the designer's hands.
In addition, except for files that are provided in the Joomla! distribution itself, these methods for customisation eliminate the need for designers and developers to "hack" core files that could change when the site is updated to a new version. Because they are contained within the template, they can be deployed to the Web site without having to worry about changes being accidentally overwritten when your System Administrator upgrades the site. The aim of this tutorial is to introduce the how four areas of the output of Joomla! are able to be customised by the template designer. Not interested in all the theory? Jump straight to the cheat sheet. MVC 101MVC can be a scary acronym. It stands for Model-View-Controller and the concepts behind MVC are responsible for the extra flexibility that is now afforded to the designer. While parts of the theory can be rather involved and complicated, the only part that the designer need worry about is the V for View. This is the part that is concerned with output. Different extensions display output in different ways. Components, as you would already know, are fairly complex and have the ability to display different information in different ways. For example, the Articles Component (com_content) is able to display a single article, or articles in a category, or categories in a section. Each of the ways of representing the different types of data (an article, or a category, or a section) is called a "view" (this comes from our MVC terminology). Most components will have many views. However, the view doesn't actually display the output. This is left up to what we call a "layout" and it is possible for a view to have a variety of layouts. The main thing to remember here is that components can have multiple views, and each view can have one or more layouts. Each view assembles a fixed set of information, but each layout can display that information in different ways. For example, the Category view in the Articles component assembles a number of articles. These articles could be displayed in a list or in a table (and probably other ways as well). So this view may have a "list" layout and a "table" layout to choose from. Modules, on the other hand, are very simple. They generally display one thing one way. Modules don't really have views but they do support a layout. Some developers might even support a choice of layout through module parameters. Template versus LayoutIt is very important to distinguish between the role of template and the role of layouts. The template sets up a structural framework for the page of the Web site. Within this framework are positions for modules and a component to display. What actually gets displayed is governed by the module layout, or the combination of view and layout in the case of the component. The following image shows the structural layout of a typical Joomla! template (rhuk_milkyway, the default for 1.5). The module positions are displayed by adding tp=1 to the URL (eg, index.php?tp=1). You can clearly see where the module output is contained within the overall template, as well as the main component output starting in the lower-centre region. However, what is actually output in those regions, is controlled by the layouts.
Ancillary CustomisationWhile not strictly related to the MVC, there are two other important areas to consider when looking at customising the output of Joomla!. In addition to layouts, modules have what we call "chrome". Chrome is the style with which a module is to display. Most developers, designers and probably some end-users will be familiar with the different built-in styles for modules (raw, xhtml, etc). It is also possible to define your own chrome styles for modules depending on the designer result. For example, you could design a chrome to display all the modules in a particular position in a fancy javascript collapsing blind arrangement. In the screenshot above, you can just make out the names of some of the built-in module chrome used (rounded, none and xhtml). The second area has to do with controlling the pagination controls when viewing lists of data. We will look at that in more detail later. Component Output Types and Layout OverridesTo understand layout overrides we must first understand the file structure of a component. While there are many parts to a component, all fulfilling different roles and responsibilities, we want to look just in the /components /com_content /views /articles /tmpl default.php (this is a layout) form.php (this is a layout) view.html.php (this is the view that outputs the HTML) view.pdf.php (this is the view that outputs the PDF) /category /tmpl blog.php (layout) blog_items.php (a sub-layout default.php (layout) view.html.php (HTML view) view.feed.php (RSS feed) So what you see here is that under the Output TypesUnder the The affect of these different output types is most apparent when the Global Configuration setting for Search Engine Friendly URLs is set to Yes, Use Apache mod_rewrite is set to Yes, and Add suffix to URLs is also set to Yes. When this is done, the site URLs will look something like this: http://domain/sports.html The exact URL will vary depending on how you set up your site but the point here is to show that LayoutsUnder the view directory there is a The relationship between component views and layout is most plainly seen when adding a new menu item. The next screenshot represents the typical display of the New Menu Item page. Having clicked on Articles (which represents com_content), the tree expands to show the list of views and each layout within the view.
You will notice that while there are extra files in some of the Armed with all that knowledge of how all the parts relate to each other, we are now ready to actually create our layout overrides. Copying or Creating Layout FilesLayout overrides only work within the active template and are located under the It is important to understand that if you create overrides in one template, they will not be available in other templates. For example, rhuk_milkyway has no component layout overrides at all. When you use this template you are seeing the raw output from all components. When you use the Beez template, almost every piece of component output is being controlled by the layout overrides in the template. JA Purity is in between having overrides for some components and only some views of those components. The layout overrides must be placed in particular way. Using Beez as an example you will see the following structure: /templates /beez /html /com_content (this directory matches the component directory name) /articles (this directory matches the view directory name) default.php (this file matches the layout file name) form.php The structure for component overrides is quite simple: The rhuk_milkyway template does not have any layout overrides for any components. If we want to override the default layout for an article, first we need to copy this file:
to this location, creating the appropriate directories in the event they don't already exist:
If we wanted to override the blog layout in the category view, we would copy this file:
to:
Once the files are copied, you are free to customise these files as much or as little as required or desired. You do not have to honour parameter settings if you don't want to. Overriding Sub-layoutsIn some views you will see that some of the layouts have a group of files that start with the same name. The category and frontpage views have examples of this. The blog layout in the category view actually has three parts: the main layout file
echo $this->loadTemplate('item');
// or
echo $this->loadTemplate('links');
When loading sub-layouts, the view already knows what layout you are in, so you don't have to provide the prefix (that is, you load just What is important to note here is that it is possible to override just a sub-layout without copying the whole set of files. For example, if you were happy with the Joomla! default output for the blog layout, but just wanted to customise the item sub-layout, you could just copy:
to:
When Joomla! is parsing the view, it will automatically know to load Module Layout OverridesModules, like components, are set up in a particular directory structure. /modules /mod_latest_news /tmpl default.php (the layout) helper.php (a helper file containing data logic) mod_latest_news.php (the main module file) mod_latest_news.xml (the installation XML file) Similar to components, under the main module directory (in the example, As for components, the layout override for a module must be placed in particular way. Using Beez as an example again, you will see the following structure: /templates /beez /html /mod_latest_news (this directory matches the module directory name) default.php (this file matches the layout file name) The structure for module overrides is again quite simple: Copying or Creating Layout FilesThe rhuk_milkyway template does not have any layout overrides for any modules. If we want to override the default layout for Latest News module, we need to copy this file:
to this location, creating the approriate directories in the event they don't already exist:
You need to take a little care with overriding module layout because there are a number of different ways that modules can or have been designed so you need to treat each one individually. Module ChromeJoomla! 1.0 had a number of fixed styles that could display a list of modules in a particular position. These where represented by numbers:
It was a great system except for two things:
Well, in 1.5, the numbers are still recognised, but more commonly the style is represented as a word. As well as that, the syntax for displaying a module position was changed. For example, this snippet displays each module in the left position in the xhtml style:
<jdoc:include type="modules" name="left" style="xhtml" />
The built-in styles that are now available are:
In the source code, these styles are actually referred to as "chrome". The default chrome can actually be found in the system template provided in the default Joomla! install:
This file is maintained by the project so you should never modify it directly otherwise there is a risk that you will loose your changes if and when you upgrade your Joomla! installation. To create your own chrome, or module styles, all you need to do is create or edit The rhuk_milkyway template actually does provide some extra chrome as an example (it provides and new example style called "slider"). This can be found in the following file:
To create your own chrome is really easy. Let's look at ficticious example that displays the module in a Definition List (a set of DL's, DT's and DD's). Just add the following function to the /html/modules.php file in your default template directory (create it if it does not exist):
/*
* Module chrome that wraps the module in a definition list
*/
function modChrome_dlist($module, &$params, &$attribs)
{ ?>
<dl class="<?php echo $params->get('moduleclass_sfx'); ?>">
<?php if ($module->showtitle != 0) : ?>
<dt>
<?php echo $module->title; ?>
</dt>
<?php endif; ?>
<dd>
<?php echo $module->content; ?>
</dd>
</dl>
<?php
}
We will be calling the style "dlist", so the name of the function needs to be The function must take the three arguments as shown for the module object, the module parameters, and lastly the There are three main properties in the module object to be concerned with:
This is a very simple case and you can of course design more complex styles, possibly using custom atrributes in the XML tag. Pagination Links OverridesThe final override we will look at is the pagination override. This override can control the display of items-per-page and the pagination links that are used with lists of information, as shown in the following screenshot.
The rhuk_milkyway template provides a well commented example for this override. The file is found here:
When the pagination list is required, Joomla! will look for this file in the default templates. If it is found it will be loaded and the display functions it contains will be used. There are four functions that can be used:
This function is responsible for showing the select list for the number of items to display per page.
This function is responsible for showing the list of page number links as well at the Start, End, Previous and Next links.
This function displays the links to other page numbers other than the "current" page.
This function displays the current page number, usually not hyperlinked. Cheat SheetUsing the rhuk_milkyway template as an example, here is a brief summary of the priniciples we've looked at. Customise the Component OutputTo override a component layout (for example the default layout in the article view), copy:
to:
Read more about component output. Customise the Module OutputTo override a module layout (for example the Latest News module using the rhuk_milkyway template), copy:
to:
Read more about module output. Add New Module StylesTo add new module styles (chrome), add them to the following file:
Read more about module styles. Customise the Pagination LinksTo customise the way the items-per-page selector and pagination links display, edit the following file:
Read more about pagination. ConclusionJoomla! 1.5, through the use of an MVC paradigm has greatly improved the flexibility that is afforded to Web site designers. By way of a few simple principles, like copying certain files to certain places in your template, the designer is able to override almost all of the output generated by Joomla!. If you have any questions relating to output overrides please let us know so that we can improve this tutorial. If you have translated this article into another language, please let us know.
For more information on Andrew, visit his profile page at community.joomla.org. |










Mon 07 Jul 2008 00:13:06 EDT
Mon 07 Jul 2008 05:50:40 EDT
Mon 07 Jul 2008 09:22:42 EDT
Mon 07 Jul 2008 10:19:35 EDT
Sun 13 Jul 2008 22:12:03 EDT
Mon 14 Jul 2008 03:22:09 EDT
Fri 01 Aug 2008 05:59:38 EDT
Fri 01 Aug 2008 06:46:28 EDT
Tue 05 Aug 2008 05:25:45 EDT
Thu 07 Aug 2008 00:02:34 EDT
Mon 25 Aug 2008 09:35:16 EDT
Fri 12 Sep 2008 16:07:52 EDT
Mon 15 Sep 2008 00:05:45 EDT
Mon 22 Sep 2008 08:26:30 EDT