Home > Articles > Web Development > Content Management Systems

  • Print
  • + Share This
This chapter is from the book

Creating a Simple Template

To understand the contents of a template, we will start by looking at a blank Joomla template.

The Template File Components

The template contains the various files and folders that make up a Joomla template. These files must be placed in the /templates/ directory of a Joomla installation in their own folder. So if we had two templates installed, our directory would look something like the following:

/templates/element
/templates/voodoo

Note that the directory names for the templates must be the same as the name of the template, in this case element and voodoo. Obviously they are case sensitive and shouldn't contain spaces.

Within the directory of a template, there are a number of key files:

/element/templateDetails.xml
/element/index.php

These two filenames and locations must match exactly because this is what they are called by the Joomla core script.

The first of these is the template XML file.

templateDetails.xml

This is an XML format metadata file that tells Joomla what other files are needed when loading a web page that uses this template. Note the uppercase "D." It also details the author, copyright, and what files make up the template (including any images used). The last use of this file is for installing a template when using the admin backend.

Second, we have the engine of the template, the index.php:

index.php

This file is the most important. It lays out the site and tells the Joomla CMS where to put the different components and modules. It is a combination of PHP and (X)HTML.

In almost all templates, additional files are used. It is conventional (although not required by the core) to name and locate them as shown here:

/element/template_thumbnail.png
/element/css/template.css
/element/images/logo.png

These are just examples. Table 9.1 examines each line.

Table 9.1. Core Files Needed for a Template

/templatename/folder/filename

Description

/element/template_thumbnail.png

A web browser screenshot of the template (usually reduced to around 140 pixels wide and 90 pixels high). After the template has been installed, this functions as a "Preview Image" visible in the Joomla administration Template Manager and also the template selector module in the frontend (if used).

/element/css/template.css

The CSS of the template. The folder location is optional, but you have to specify where it is in the index.php file. You can call it what you like. Usually the name shown is used, but we will see later that there are advantages in having other CSS files too.

/element/images/logo.png

Any images that go with the template. Again for organization reasons, most designers put this in an images folder. Here we have an image file called logo.png as an example.

templateDetails.xml

The templateDetails.xml must include all the files that are part of the template. It also includes information such as the author and copyright. Some of these are shown in the admin backend in the Template Manager. An example XML file is shown here:

<?xml version="1.0" encoding="utf-8"?>
<install version="1.5" type="template">
      <name>TemplateTutorial15</name>
      <creationDate>August 2007</creationDate>
      <author>Barrie North</author>
      <copyright>GPL</copyright>
      <authorEmail> compassdesigns@gmail.com </authorEmail>
      <authorUrl>www.compassdesigns.net</authorUrl>
      <version>1.0</version>
      <description>First example template for Chapter 9 of the Joomla Book
      </description>
      <files>
            <filename>index.php</filename>
            <filename>templateDetails.xml</filename>
            <filename>js/somejsfile.js</filename>
            <filename>images/threecol-l.gif</filename>
            <filename>images/threecol-r.gif</filename>
            <filename>css/customize.css</filename>
            <filename>css/layout.css</filename>
            <filename>css/template_css.css</filename>
       </files>
      <positions>
            <position>user1</position>
            <position>top</position>
            <position>left</position>
            <position>banner</position>
            <position>right</position>
            <position>footer</position>
      </positions>
         <params>
            <param name="colorVariation" type="list" default="white" label="Color
Variation" description="Color variation to use">
                  <option value="blue">Blue</option>
                  <option value="red">Red</option>
            </param>
         </params>
</install>

Let's explain what some of these lines mean:

  • <install version="1.5" type="template">. The contents of the XML document are instructions for the backend installer. The option type="template" tells the installer that we are installing a template and that it is for Joomla 1.5.
  • <name>TemplateTutorial15</name>. Defines the name of your template. The name you enter here will also be used to create the directory within the templates directory. Therefore it should not contain any characters that the file system cannot handle, for example spaces. If installing manually, you need to create a directory that is identical to the template name.
  • <creationDate>August 2007</creationDate>. The date the template was created. It is a free form field and can be anything such as May 2005, 08-June-1978, 01/01/2004, and so on.
  • <author>Barrie North</author>. The name of the author of this template—most likely your name.
  • <copyright>GPL</copyright>. Any copyright information goes into this element. A Licensing Primer for Developers and Designers can be found in the Joomla forums.
  • <authorEmail>compassdesigns@gmail.com</authorEmail>. Email address where the author of this template can be reached.
  • <authorUrl>www.compassdesigns.net</authorUrl>. The URL of the author's website.
  • <version>1.0</version>. The version of this template.
  • <files></files>. Various files used in the template.

The files used in the template are laid out with <filename> tags:

       <files>
            <filename>index.php</filename>
            <filename>templateDetails.xml</filename>
            <filename>js/somejsfile.js</filename>
            <filename>images/threecol-l.gif</filename>
            <filename>images/threecol-r.gif</filename>
            <filename>css/customize.css</filename>
            <filename>css/layout.css</filename>
            <filename>css/template_css.css</filename>
       </files>
  • The "files" sections contain all generic files like the PHP source for the template or the thumbnail image for the template preview. Each file listed in this section is enclosed by <filename> </filename>. Also included would be any additional files; here the example of a JavaScript file that is required by the template is used.
  • All image files that the template uses are also listed in the <files> section. Again, each file listed is enclosed by <filename> </filename>. Path information for the files is relative to the root of the template. For example, if the template is in the directory called 'YourTemplate', and all images are in a directory 'images' that is inside 'YourTemplate', the correct path is: <filename>images/my_image.jpg</filename>.
  • Last, any stylesheets are listed in the files section. Again, the filename is enclosed by <filename> </filename>, and it's path is relative to the template root.
  • <positions></positions>.The module positions available in the template.
  • <params></params>. These describe parameters that can be passed to allow advanced template functions such as changing the color of the template.

index.php

What actually is in an index.php file? It is a combination of (X)HTML and PHP that determines everything about the layout and presentation of the pages.

First, let's look at a critical part of achieving valid templates, the DOCTYPE at the top of the index.php file. This is the bit of code that goes at the very top of every web page. At the top of our page, we have this in our template:

<?php
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/
TR/xhtml1/DTD/xhtml1-transitional.dtd">

The first PHP statement simply makes sure that the file is not accessed directly for security.

A web page DOCTYPE is one of the fundamental components of how a web page is shown by a browser, specifically, how that browser interprets CSS. To give you further understanding, an observation from alistapart.com says

  • [Information on W3C's site about DOCTYPEs is] written by geeks for geeks. And when I say geeks, I don't mean ordinary web professionals like you and me. I mean geeks who make the rest of us look like Grandma on the first day of She's Got Mail.

Anyway, you can use several DOCTYPEs. Basically, the DOCTYPE tells the browser how to interpret the page. Here the words "strict" and "transitional" start getting floated around (float:left and float:right usually). Essentially, ever since the Web started, different browsers have had different levels of support for CSS. This means for example, that Internet Explorer won't understand the "min-width" command to set a minimum page width. To duplicate the effect, you have to use "hacks" in the CSS.

Strict means the HTML (or (X)HTML) will be interpreted exactly as dictated by standards. A transitional DOCTYPE means that the page will be allowed a few agreed upon differences to the standards.

To complicate things, there is something called "quirks" mode. If the DOCTYPE is wrong, outdated, or not there, the browser goes into quirks mode. This is an attempt to be backwards-compatible, so Internet Explorer 6 for example, will render the page pretending as if it were IE4.

Unfortunately, people sometimes end up in quirks mode accidentally. It usually happens in two ways:

  • They use the DOCTYPE declaration straight from the WC3 web page, and the link ends up as DTD/xhtml1-strict.dtd, except this is a relative link on the WC3 server. You need the full path as shown earlier.
  • Microsoft set up IE6 so you could have valid pages but be in quirks mode. This happens by having an "xml declaration" put before the DOCTYPE.

Next is an XML statement (after the DOCTYPE):

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<?php echo $this->language;
?>" lang="<?php echo $this->language; ?>" >

The part about IE6 quirks mode is important. In this chapter we only design for IE6+, so we will make sure that it's running in standards mode. This will minimize the hacks we have to do later on.

What Else Is in index.php?

Let's look at the structure of the header first; we want to be as minimal as possible but still have enough for a production site. The header information we will use is as follows:

<?php
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<?php echo $this->language; ?>" lang="<?php echo $this->language; ?>" >

<head>

<jdoc:include type="head" />

<link rel="stylesheet" href="templates/system/css/system.css" type="text/css" />
<link rel="stylesheet" href="templates/system/css/general.css" type="text/css" />
<link rel="stylesheet" href="templates/<?php echo $this->template ?>/css/template.css" type="text/css" />

</head>

What does all that mean?

We have already discussed the implications of the DOCTYPE statement in the index.php file. The <?php echo $this->language; ?> is pulling the language from the site Global Configuration.

The next line is to include more header information:

<jdoc:include type="head" />

This is all header information that is set in the Global Configuration again. It includes the following tags (in a default installation):

<title>Welcome to the Frontpage</title>
  <meta name="description" content="Joomla! - the dynamic portal engine and
  content management system" />
  <meta name="generator" content="Joomla! 1.5 - Open Source Content Management" />
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta name="robots" content="index, follow" />
  <meta name="keywords" content="joomla, Joomla" />

  <link href="index.php?option=com_content&amp;view=frontpage&amp;format=feed&amp;
Itemid=1&amp;type=rss" rel="alternate" type="application/rss+xml" title="RSS 2.0"
/>
  <link href="index.php?option=com_content&amp;view=frontpage&amp;format=feed&amp;
Itemid=1&amp;type=atom" rel="alternate" type="application/atom+xml" title="Atom
1.0" />
  <script type="text/javascript" src="https://localhost/Joomla-1.5RC2/media/system/
js/mootools.js"></script>
  <script type="text/javascript" src="https://localhost/Joomla-1.5RC2/media/system/
js/caption.js"></script>

Much of this header information is created on the fly specific to the page (article) that someone is on. It includes a number of metatags—the favicon, RSS feed URLs, and some standard JavaScipt files.

The last lines in the header provide links to CSS files for the template:

<link rel="stylesheet" href="templates/system/css/system.css" type="text/css" />
<link rel="stylesheet" href="templates/system/css/general.css" type="text/css" />
<link rel="stylesheet" href="templates/<?php echo $this->template ?>/css/template.
css" type="text/css" />

The first two files, system.css and general.css contain some generic Joomla styles. The last one is all the CSS for the template, here called template.css. The PHP code <?php echo $this->template ?> will return the name of the current template. Writing it in this way rather than the actual real path makes the code more generic. When you create a new template you can just copy it (along with the whole header code) and not worry about editing anything.

The template CSS files can have any number of files, for example conditional ones for different browsers. This one targets IE6:

<!--[if lte IE 6]>
<link href="templates/<?php echo $this->template ?>/css/ieonly.css"
rel="stylesheet" type="text/css" />
<![endif]-->

This example is part of a technique to use a template parameter:

<link rel="stylesheet" href="templates/<?php echo $this->template ?>/css/<?php
echo $this->params->get('colorVariation'); ?>.css" type="text/css" />

Blank Joomla Template Body

Creating our first template will be very, very easy! Ready?

All we need to do is use Joomla statements that insert the contents of any modules and the mainbody.

<body>
<?php echo $mainframe->getCfg('sitename');?><br />
<jdoc:include type="module" name="breadcrumbs" />
<jdoc:include type="modules" name="top" />
<jdoc:include type="modules" name="left" />
<jdoc:include type="component" />
<jdoc:include type="modules" name="right" />
</body>

At this point (if you preview it), our site does not look very awe inspiring. The output is shown in Figure 9.3.

Figure 9.3

Figure 9.3 An unstyled template

The template contains the following in reasonably logical order:

  • name of the site
  • top module
  • left modules
  • main content
  • right modules

The goal is to try and come as close to semantic markup as possible. From a Web point of view, it means a page can be read by anyone—a browser, a spider, or a screen reader. Semantic layout is the cornerstone of accessibility.

You will notice that we have used the first of a number of commands specific to Joomla to create this output:

<?php echo $mainframe->getCfg('sitename');?><br />
<jdoc:include type="module" name="breadcrumbs" />
<jdoc:include type="modules" name="top" />
<jdoc:include type="modules" name="left" />
<jdoc:include type="component" />
<jdoc:include type="modules" name="right" />

The PHP echo statement simply outputs a string from the configuration.php file. Here, we are using the site name; we could have as easily had the following:

The name of this site is <?php echo $mainframe->getCfg('sitename');?><br />
The administrator email is <?php echo $mainframe->getCfg('mailfrom');?><br />
This template is in the <?php echo $this->template?> directory<br />
The URL is <?php echo JURI::base();;?>

The jdoc statement inserts various types of (X)HTML output from modules of components.

This line inserts the output from a component. What component it will be is determined by the menu link:

<jdoc:include type="component" />

This line inserts the output for a module location:

<jdoc:include type="modules" name="right" />

The full syntax is actually

<jdoc:include type="modules" name="LOCATION" style="OPTION" />

We look at the various options for styles in the section about modules later in this chapter.

CSSTemplateTutorialStep1

At this point, we have a very bare template. I have created an installable template that is available from www.joomlabook.com: CSSTemplateTutorialStep1.zip.

This will install a template that has only two files, the index.php and template Details.xml. I removed references to other files to give a bare bones output with no CSS. This is actually a useful diagnostic template; you can install it and track errors that are occurring with a component or module.

  • + Share This
  • 🔖 Save To Your Account