Creating Custom Joomla Error and Offline pages.

While creating a template for Joomla! we can define not only the main page and subpages look but also the look of standard error messages or information about storefront’s maintenance works. If needed, we can also define our own templates which fulfill certain conditions, e.g. which are needed to exchange data with AJAX use. It can be done thanks to variable tmpl which is in address.

Joomla Error

Generally, templates have only index.php file in the main catalog or sometimes component.php file; they are used in order to generate site’s content which depend on value of tmpl variable in address. (index is its default value). We define in component.php site’s look which includes present component only. If we look at templates/system/ catalog content, we will see that there are two more files this type, namely error.php and offline.php. The first one is responsible for messages displayed; e.g. when entered address does not exist and the second one is responsible for the site’s look informing about maintenance works. Both files can be overwritten in main catalog of our template and we can get coherent styling with our project’s look.

There is wide range of possibilities while creating own templates of this type. If you thought how to implement scripts with AJAX use in Joomla!, it is: an appropriate component + an appropriate template.

In this case, component will generate suitable data (let’s assume that they will be in XML format) and a template will generate component content without redundant structures which is in this case document header.

It is enough to create in our template’s main catalog ajax.php file with the following content:

<?php
 $document =&amp; JFactory::getDocument(); 
 $document->setMimeEncoding('application/xml'); 

 defined( '_JEXEC' ) or die( 'Restricted access' );
 ?>
<jdoc:include type="component" />

First two lines cause the change MIME type of a document (if we want to receive data in JSON format, it is enough to change setMimeEncoding attribute method), the next line blocks remote development of our template and our template’s content is component content which depends on parameters included in site’s address.

Now, every site’s where tmpl variable with ajax value will be present, it will cause our new template use, e.g.:

index.php?option=com_ajax&amp;tmpl=ajax

It will cause generation of disclosed data at com_ajax component site with ajax.php template use.

If we remove tmpl variable from address, we will display component content in a traditional way.

In this way, we can create templates very easily specifying format of data output from component. Theoretically, we can use format variable in the address but it requires to be defined appropriate views responsible for output format given in a component and thanks to this method presented, we are doing everything once, instead of doing so for each component separately.

Besides, it is worth mentioning that we can change output formats to a template given quite easily, e.g. for ajax.php template, JSON and XML formats would be required and there is no sense in creating two separate templates; it is enough to define an additional variable, e.g. output and depending on its value, generate an appropriate MIME type for a document.

To top it off, a sample code of a template with JRequest class use:

<?php
 $format = JRequest::getCmd('output') == 'json' ? 'application/json' : 'application/xml';
 $document =&amp; JFactory::getDocument();
 $document->setMimeEncoding($format);

 defined( '_JEXEC' ) or die( 'Restricted access' );
 ?>
<jdoc:include type="component" />

As you can see, if we set some value (or we don’t set it) other than json and then we will get data in XML format.

And one more thing; it is worth considering using templates mentioned, e.g. to create mobile template versions or various templates forms deprived of some module positions.

Share
This article was first published April 24th, 2010