Joomla 1.5 Template Tutorial - Using CSS to Create a Tableless Layout
Article Index
Joomla 1.5 Template Tutorial
What is a Joomla 1.5 Template?
Creating a Simple Blank Template
Using CSS to Create a Tableless Layout
Making a Real Joomla 1.5 Template
Advanced Joomla 1.5 Template Features
Summary and Comments
All Pages

Using CSS to Create a Tableless Layout

We will be using pure CSS to make a 3-column layout for the Joomla template. We will also be making it a fluid layout. There are two main types of web page layout[md]fixed and fluid[md]and they both refer to how the width of the page is controlled.

The width of the page is an issue because of the many browser resolutions at which people surf the Web. Although the percentage is dropping, about 17% of surfers are using an 800x600 resolution. The majority, 79%, are using 1024x768 and higher2. Making a fluid layout means that your valuable web page won't be a narrow column in the 1024 resolution and will be visible in full on smaller monitors.

A typical design might use tables to layout the page. They are useful in that you just have to set the width of the columns as percentages, but they have several drawbacks. For example, tables have lots of extra code compared to CSS layouts. This leads to longer load times (which surfers don't like) and poorer performance in search engines. The code can roughly double in size, not just with markup but also with something called "spacer gifs."

Even big companies sometimes fall into the table trap, as seen by a recent controversy about the new disney.co.uk website3:

There are a couple of major problems with a site that uses tables for layout.

  • They are difficult to maintain. To change something you have to figure out what all the table tags like td/tr are doing. With CSS there are just a few lines to inspect.
  • The content cannot be source-ordered. Many Web surfers do not see web pages on a browser. Those viewing with a text browser or screen reader will read the page from the top left corner to the bottom right. This means that they first view everything in the header and left column (for a 3-column layout) before they get to the middle column, the important stuff. A CSS layout, on the other hand, allows for "source-ordered" content, which means the content can be rearranged in the code/source. Perhaps your most important site visitor is Google, and it uses a screen reader for all intents and purposes.

Let's look at our layout using CSS. You can position elements (stuff) in several ways using CSS. For a quick introduction, a good source is Brainjar's CSS Positioning.

If you are new to CSS, you might read at least one beginner's guide to CSS. Here are a few suggestions:

The Least You Need to Know
Modern web design uses CSS rather than tables to position elements. It's difficult to learn but worth the investment. There are many (non-Joomla) resources available to help you.

We will be using float to position our content. At its most basic, the template might look like Figure 9.4.
Still not very exciting, but let's see what the different parts are all about.

The CSS styles are defined here in the head of the file to show what is going on, but normally they would be in the template.css file.

Everything is contained in an element called #wrap. This has a fluid width that ranges between 760px and 960px.

Joomla 1.5 Template Tutorial
Figure 9.4
Basic template layout

In Figure 9.4, the left, middle, and right columns are each given their own element. Each is floated left and given a percent width that together add up to 100%. The clear:both style on the footer tells the browser to "stop floating" and makes the footer stretch across all three columns. When we build our second template in this chapter, we will have to use a more advanced clearing technique.

To improve the layout and to add some breathing room to the content, we need to add some column spacing, commonly called "gutter." Unfortunately, there is a problem here. You might know that Internet Explorer does not interpret CSS correctly. One problem is that it calculates width differently. We can solve this problem by not using any padding or borders on something that has a width. To get our gutter, we add another <div> element inside the columns.

To the CSS we add

.inside {padding:10px;}

Our resulting <body> code for index.php is:

<body>
<div id="wrap">
<div id="header">
<div class="inside">
<?php echo $mainframe->getCfg('sitename');?>
<jdoc:include type="modules" name="top" />
</div>
</div>
<div id="sidebar">
<div class="inside">
<jdoc:include type="modules" name="left" />
</div>
</div>
<div id="content">     <div class="inside">
<jdoc:include type="component" />
</div>
</div>
<div id="sidebar-2">
<div class="inside">
<jdoc:include type="modules" name="right" />
</div>
</div>
<div id="footer">
<div class="inside"> Powered by <a href="http://joomla.org">Joomla!</a>.
Valid <a href="http://validator.w3.org/check/referer">XHTML</a> and
<a href="http://jigsaw.w3.org/css-validator/check/referer">CSS</a>. </div>
</div>
</div>
<!--end of wrap-->
</body>

Our template.css file looks like this:

#wrap {
min-width:760px;
max-width:960px;
}

#content {
float:left;
width:60%;
overflow:hidden;
}

#footer {
clear:both;
}

.inside {
padding:10px;
}

#sidebar,#sidebar-2 {
float:left;
width:20%;
overflow:hidden;
}

TIP
CSS Shorthand
It's possible to reduce the amount of CSS code by using "shorthand." One example of this is padding and margin styles applied to an element, where:

margin-top:5px; margin-bottom:5px; margin-left:10px; margin-right:10px;

can be replaced by:

margin: 5px 10px;

There are "shorthand" styles at the beginning of each style definition. After you have figured out the styles, fill the shorthand versions in and delete the long versions. The syntax is:

font: font-size |font-style | font-variant | font-weight | line-height | font-family

Here is an example. Rather than using this

font-size:1em;
font-family:Arial,Helvetica,sans-serif;
font-style:italic;
font-weight:bold;
line-height:1.3em;

use this:

font:bold 1em/1.3em Arial,Helvetica,sans-serif italic;

Read more about this syntax at An Introduction to CSS shorthand properties
http://home.no.net/junjun/html/shorthand.html.

This simple layout is a good one to use for learning about how to use CSS with Joomla because it shows two of the advantages of CSS over table-based layouts, it is less code, and it is easier to maintain.

owever, it is not source-ordered. For that we must use a more advanced layout known as a nested float.
Source-ordered layouts perform better for SEO than ones where the important content occurs late in the code. From a Joomla site perspective, the important content is that which is coming from the component.

Default CSS

So far, all of our CSS has been only about layout, which will make a plain page. So let's add some formatting:

body {
text-align:center;
}

#wrap {
min-width:760px;
max-width:960px;
width:auto !important;
text-align:left;
margin:0 auto;
}

#content {
float:left;
width:60%;
overflow:hidden;
}

#footer {
clear:both;
}

.inside {
padding:10px;
}

#sidebar,#sidebar-2 {
float:left;
width:20%;
overflow:hidden;
}

We have centered the page by using a small hack. This has to be done because Internet Explorer does not read CSS accurately. With a standards-compliant browser, we could just say margin:0 10%; to center the page, but IE does not recognize that, so we center the "text" of the whole page and then align it back left in the columns.

In celebration of IE7's support of min/max width (which IE6 does not), we can add in a minimum and maximum width. Note we have to add a tiny hack for IE6 as it does not understand these. It will ignore the !important statement and have a plain, old 960px width.

NOTE
It might seem strange to define our columns in percentage widths and then have a containing div that is fixed. Well, a few things are going on here:

  • Having fluid columns inside a fixed width container makes the template very flexible. If we add width changer buttons, we only need to change one value.
  • We still have a max-width so why not go all fluid? Many viewers on the Web now have enormous screens. Usability research tells us that lines of text over 900px wide are hard to read because the eyes have to go a long way to go to the next line. Limiting the fluidity makes the site more useable/accessible.

We have also added a new style to the columns: overflow:hidden. This will make the page "break" more consistently as we reduce its width.

At the beginning of the typography, with CSS we will set some overall styles and have what is known as a global reset:

* {
margin:0;
padding:0;
}

h1,h2,h3,h4,h5,h6,p,blockquote, form,label,ul,ol,dl,fieldset,address {
margin:0.5em 0;
}

li,dd {
margin-left:1em;
}

fieldset {
padding:.5em;
}

body {
font-size:76%;
font-family:Verdana, Arial, Helvetica, sans-serif;
line-height:1.3;
}

Everything is given a zero margin and padding, and then all block level elements are given a bottom margin. This helps achieve browser consistency. You can read more about the global reset at clagnut5 and left-justified.

The font size is set to 76%. The reason for this is to try and get more consistent font sizes across browsers. All font sizes are then set in em. Having line-height:1.3 helps readability. This means that the pages will be more accessible because the viewer will be able to resize the fonts to their own preferences. This is discussed more at "An experiment in typography" at The Noodle Incident (Owen Briggs)

If we were to add some background colors to the header, sidebars, and content containers, we would see something like what is shown in Figure 9.5.

oomla 1.5 Template Tutorial
Figure 9.5
Basic template with typography

Notice that the side columns do not reach their footer. This is because they only extend as far as their content; where the space is white on the left and on the right, they don't exist.

If we have a template that has a white background for all three columns, this is no problem. We will use this approach and will have boxes around the modules. If we want equal height columns that are colored or have boxes, we have to use a background image that will tile vertically. This technique is called Faux Columns and is described by Douglas Bowman8 and Eric Meyer.

Joomla-Specific CSS

Although Joomla 1.5 has the functionality to override the core output in a template, its default rendering still uses significant tables to output content in the main body. Along with these tables, CSS output is available for a designer to style pages. Based on some research by various community members, Table 9.2 shows the current list. Note it does not include generic web page styles like H1, H2, p, ul, a, form, and so on.

Table 9.2
Legacy Default CSS Styles from 1.0 in 1.5

article_separator
adminform
article_separator
author
bannerfooter
bannergroup
bannerheader
banneritem
blog
blog_more
blogsection
breadcrumbs
button
buttonheading
clr
componentheading
content_email
content_rating
content_vote
contentdescription
contentheading
contentpagetitlw

contentpane
contentpaneopen
contenttoc
createdate
created-date
date
input
inputbox
intro
latestnews
loclink
mainlevel
message
metadata
modifydate
module
moduletable
mosimage
mosimage_caption
mostread
newsfeed

outline
pagenav
pagenav_next
pagenav_prev
pagenavbar
pagenavcounter
pathway
pollstableborder
read
search
searchintro
sections
sectiontable_footer
sectiontableentry
sectiontablefooter
sectiontableheader
small
smalldark
sublevel
title
wrapper

Many designs you might see in Table 9.2 actually have given CSS styles that are more specific in their definitions. Basically, a more specific rule overrides a less specific rule.

For example

a {color:blue;} a:link {color:red;}
.contentheading {color:blue;}
div.contentheading {color:red;}

The color on a link and the color of the .contentheading will be red, as that rule is more specific (as .contentheading is contained within a <div>)

In the case of Joomla templates, you will often see more specific rules used. This often occurs when the class is on a table. Here are more examples:

.moduletable
table.moduletable

.moduletable is the name of the <div> that wraps a module. table.moduletable will only apply the style to a table with class="moduletable" on it.

.moduletable will apply the style regardless of what element the class is on.

a.contentpagetitle:link
.contentpagetitle a:link

a.contentpagetitle:link will apply the style to any a tags with a .contentpagetitle class on them that is a link.
.contentpagetitle a:link will apply the style to any elements inside .contentpagetitle that are links.

Specificity is not easy to understand; its often easier to start by using the most general style possible and then getting more specific if the results are not what you expect.

Here are some links to websites that discuss specificity in detail:

  • www.htmldog.com/guides/cssadvanced/specificity/
  • www.meyerweb.com/eric/css/link-specificity.html
  • www.stuffandnonsense.co.uk/archives/css_specificity_wars.html

At the moment, our template is using several tables. As mentioned earlier, this slows the pages down and makes them harder to update. To reduce the number of tables, when we call the modules, we need to use style parameters in the jdoc:include.

The Least You Need to Know
Joomla will output specific elements, ids, and classes in the code of a webpage. These can be predicted and used to style the design using CSS.

Modules in Templates

When a module is called in the index.php, it has several options on how it is displayed.
The syntax is:

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

The style is optional and is defined in templates/system/html/modules.php. Currently, the default modules.php file contains the following layouts.

OPTION="table" (default display) modules are displayed in a column. The following shows an example of the output:

<table cellpadding="0" cellspacing="0" class="moduletable<?php echo $params->get('moduleclass_sfx'); ?>">
<?php if ($module->showtitle != 0) : ?>
<tr>
<th valign="top">
<?php echo $module->title; ?>
</th>
</tr>
<?php endif; ?>
<tr>
<td>
<?php echo $module->content; ?>
</td>
</tr>
</table>

OPTION="horz" makes the modules appear horizontally. Each module is output in the cell of a wrapper table. The following shows an example of the output:

<table cellspacing="1" cellpadding="0" border="0" width="100%">
<tr>
<td valign="top">
<?php modChrome_table($module, $params, $attribs); ?>
</td>
</tr>
</table>

OPTION="xhtml" makes modules appear as a simple div element. The following shows an example of the output:

<div class="moduletable<?php echo $params->get('moduleclass_sfx'); ?>">
<?php if ($module->showtitle != 0) : ?>
<h3><?php echo $module->title; ?></h3>
<?php endif; ?>
<?php echo $module->content; ?>
</div>

OPTION="rounded" makes modules appear in a format that allows, for example, stretchable rounded corners. If this $style is used, the name of the <div> changes from moduletable to module. The following shows an example of the output:

<div class="module<?php echo $params->get('moduleclass_sfx'); ?>">
<div>
<div>
<div>
<?php if ($module->showtitle != 0) : ?>
<h3><?php echo $module->title; ?></h3>
<?php endif; ?>
<?php echo $module->content; ?>
</div>
</div>
</div>
</div>

OPTION="none" makes modules appear as raw output containing no element and no title. Here is an example:

echo $module->content;

As you can see, the CSS options (xhtml and rounded) are much leaner in code, which makes it easier to style the web pages. I don't recommend using suffixes of table (default) or horz unless absolutely needed.

Here's the really good bit!

If you examine the modules.php file, you will see all the options that exist for modules. It's easy to add your own; this is part of the new templating power that is in 1.5.We will look at this in more details in our section on template overrides.

To develop our template, we will put a module style of "xhtml" on all of our modules:

<body>
<div id="wrap">
<div id="header">
<div class="inside">
<h1><?php echo $mainframe->getCfg('sitename');?></h1>
<jdoc:include type="modules" name="top" style="xhtml" />
</div>
</div>
<div id="sidebar">
<div class="inside">
<jdoc:include type="modules" name="left" style="xhtml" />
</div>
</div>
<div id="content">
<div class="inside">
<jdoc:include type="module" name="breadcrumbs" style="none" />
<jdoc:include type="component" />     </div>
</div>
<div id="sidebar-2">
<div class="inside">
<jdoc:include type="modules" name="right" style="xhtml" />
</div>
</div>
<div id="footer">
<div class="inside">
<jdoc:include type="modules" name="footer" style="xhtml" />
</div>
</div>
<!--end of wrap-->
</body>

Note that we cannot put these module styles on the <jdoc:include type="component" /> because it is not a module.

The Least You Need to Know
In 1.5, the output of modules can be completely customized, or you can use the pre-built output. All of these options are called module chrome.

We have also placed the site title inside an <H1> tag. It's more semantically correct and will also help in SEO. Let's also remove the background from the layout divs.

We will also add some CSS to style the modules with a border and a background for the module titles.
Our CSS now looks like this:

* {
margin:0;
padding:0;
}

h1,h2,h3,h4,h5,h6,p,blockquote, form,label,ul,ol,dl,fieldset,address {
margin:0.5em 0;
}

li,dd {
margin-left:1em;
}

fieldset {
padding:.5em;
}

body {
font-size:76%;
font-family:Verdana, Arial, Helvetica, sans-serif;
line-height:1.3;
margin:1em 0;
}

#wrap {
border:1px solid #999;
}

#header {
border-bottom:1px solid #999;
}

#footer {
border-top:1px solid #999;
}

a {
text-decoration:none;
}

a:hover {
text-decoration:underline;
}

h1,.componentheading {
font-size:1.7em;
}

h2,.contentheading {
font-size:1.5em;
}

h3 {
font-size:1.3em;
}

h4 {
font-size:1.2em;
}

h5 {
font-size:1.1em;
}

h6 {
font-size:1em;
font-weight:700;
}

#footer,.small,.createdate,.modifydate,.mosimage_caption {
font:0.8em Arial,Helvetica,sans-serif;
color:#999;
}

.moduletable {
margin-bottom:1em;
border:1px #CCC solid;
padding:0 10px;
}

.moduletable h3 {
background:#666;
color:#fff;
text-align:center;
font-size:1.1em;
margin:0 -10px 0.5em;
padding:0.25em 0;
}

NOTE
Several of the menus in the default installation have a menu suffix in the module properties of _menu. To get everything behaving properly, I deleted that parameter.

This typography CSS now produces the result shown in Figure 9.6.

oomla 1.5 Template Tutorial
Figure 9.6
Basic template with module title styling

Menus in Templates

We saw in Chapter 5, "Creating Menus and Navigation," that there are a number of settings for how a menu will be rendered.

Again, using CSS lists rather than tables results in reduced code and easier markup. After setting all our menus to lists we have only 12 tables (we'll see how to remove the rest using the new version 1.5 override feature). Remember, the list setting is the new 1.5 version; flat list is from 1.0 and will be depreciated.

ists are also better than tables because text-based browsers, screen readers, non-CSS supporting browsers, browsers with CSS turned off, and search bots will be able to access your content more easily.
One of the other advantages of using CSS for menus is that there is a lot of example code on various CSS developer sites. Let's look at one of them and see how it can be used.

A web page at maxdesign.com10 has a selection of over 30 menus, all using the same underlying code. It's called the Listamatic. There is a slight difference in the code that we have to change in order to adapt these menus to Joomla.

These lists use the following code:

<div id="navcontainer">
<ul id="navlist">
<li id="active"><a href="/ #" id="current">Item one</a></li>
<li><a href="#">Item two</a></li>
<li><a xhref="#">Item three</a></li>
<li><a href="#">Item four</a></li>
<li><a href="#">Item five</a></li>
</ul>
</div>

This means that there is an enclosing <div> called navcontainer, and the <ul> has an id of navlist. To duplicate this effect in Joomla, we need have some sort of enclosing <div>.

We can achieve this by using module suffixes. If you recall, the output of an XHTML style option module is:

<div class="moduletable">
<h3>modChrome_xhtml</h3>
modChrome_xhtml
</div>

If we add a module suffix, that will get added to the moduletable class, like this:

<div class="moduletablesuffix">
<h3>modChrome_xhtml</h3>
modChrome_xhtml
</div>

So when picking a menu from Listamatic, you would need to replace the navcontainer class style in the CSS by moduletablesuffix.

NOTE
Module suffixes to a certain extent blur the line between site design and site administration. One of the goals of further development of the Joomla core is to clearly separate these roles. The implication is that it is likely that they might get depreciated in future versions beyond 1.5.

This use of a module class suffix is useful. It allows different colored boxes with just a simple change of the module class suffix.

The Least You Need to Know
It's best to always use the bulleted or flat list for menu output. You can then make use of many free resources for the CSS that are available on the Web.

For our site we will use List 10 by Mark Newhouse.11 Our CSS will be:

.moduletablemenu {
color:#333;
margin-bottom:1em;
padding:0;
}

.moduletablemenu h3 {
background:#666;
color:#fff;
text-align:center;
font-size:1.1em;
border-bottom:1px solid #fff;
margin:0;
padding:0.25em 0;
}

.moduletablemenu ul {
list-style:none;
margin:0;
padding:0;
}

.moduletablemenu li {
border-bottom:1px solid #ccc;
margin:0;
}

.moduletablemenu li a {
display:block;
border-left:10px solid #333;
border-right:10px solid #9D9D9D;
background-color:#666;
color:#fff;
text-decoration:none;
padding:3px 5px 3px 0.5em;
}

html>body .moduletablemenu li a {
width:auto;
}

.moduletablemenu li a:hover,a#active_menu:link,a#active_menu:visited {
border-left:10px solid #1c64d1;
border-right:10px solid #5ba3e0;
background-color:#2586d7;
color:#fff;
}

We then need to add the module suffix of menu (no underscore in this case) to any modules of menus we want to be styled. This will produce a menu like what's shown in Figure 9.7.

For any menu we want to be styled this way, we have to add "menu" as a module suffix.

oomla 1.5 Template Tutorial
Figure 9.7
Basic template with menu styling

TIP
When trying to get a particular menu to work, here is a useful tip: Create a default Joomla installation and then look at the code that makes up the mainmenu. Copy and paste this code into an HTML editor (like Dreamweaver). Replace all the links by "#," and then you can add CSS rules until the effect you want is achieved. The code for the menu to create the style is as follows:

<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<style type="text/css">
<!-- .astyle { } -->
</style>
</head>
<body>
<div class="moduletable">
<h3>Main Menu</h3>
<ul class="mainmenu">
<li id="current" class="item1 active"><a href="#">Home</a></li>
<li class="item2"><a href="#">Joomla! Overview</a></li>
<li class="item3"><a href="#">What's New in 1.5?</a></li>
<li class="item4"><a href="#">Joomla! License</a></li>
<li class="item5"><a href="#">More about Joomla!</a></li>
<li class="item6"><a href="#">FAQ</a></li>
<li class="item7"><a href="#">The News</a></li>
<li class="item8"><a href="#">Web Links</a></li>
<li class="item9"><a href="#">News Feeds</a></li>
</ul>
</div>
</body>
</html>

The CSS is embedded instead of linked to make editing easier.

Hiding Columns

So far, we have our layout such that we always have three columns, regardless of whether there is any content included. From the perspective of a CMS template, this is not very useful. In a static site the content would never change, but we want to give our site administrators the ability to put their content anywhere they want to without having to worry about editing CSS layouts. We want to be able to "turn off" a column automatically or "collapse" it if there is no content there.

During the development of the Joomla 1.5 templating engine, there were a number of changes and improvements. Quoting directly from the Joomla development blog:

The changes to the template system in Joomla 1.5 can be divided into two categories. First of all there are changes to the way things where done in Joomla 1.0, for example the new way modules are loaded, and second there are also a bunch of extra features, like template parameters[el]a quick overview:

mosCountMoules

The mosCountModules function has been replaced by the $this->countModules function and support for conditions has been added. This allows designers to easily count the total number of modules in multiple template positions in just one line of code, for example $this->countModules('user1 + user2'); which will return the total number of modules in position user1 and user2.

NOTE
More information is also available in the Joomla forum.

So the general use of mosCountModules would be:

<?php if($this->countModules('condition')) : ?>
do something
<?php else : ?>
do something else
<?php endif; ?>

There are four possible conditions. As an example let's count the number of modules in Figure 9.7. We could insert this code somewhere in the index.php:

left=<?php echo $this->countModules('left');?><br />
left and right=<?php echo $this->countModules('left and right');?><br />
left or right=<?php echo $this->countModules('left or right');?><br />
left + right=<?php echo $this->countModules('left + right');?>
  • countModules('left'). Will return 4; there are 4 modules on the left.
  • countModules('left and right'). Will return 1; there is a module in left and right position.
  • countModules('left or right'). Will return 1; there is a module in left or right position.
  • countModules('left + right'). Will return 7; counting the modules in left and right. position

In this situation, we need to use the function that allows us to count the modules present in a specific location. So for example, if there is no content published in the right column, we can adjust the column sizes to fill that space.

There are several ways to do this. We could put the conditional statement in the body to not show the content and then have a different style for the content based on what columns were there. To make it as easy as possible, I have a series of conditional statements in the head tag that (re)define some CSS styles:

<?php
if($this->countModules('left and right') == 0) $contentwidth = "100";
if($this->countModules('left or right') == 1) $contentwidth = "80";
if($this->countModules('left and right') == 1) $contentwidth = "60";
?>

So we count:

  • If there is nothing in left OR right, we are 100%.
  • If there is something in left OR right, we are 80%.
  • If there is something in left AND something in right, we are 60%.

We then need to change the index.php file in the content div to:

<div id="content<?php echo $contentwidth; ?>">

Change the layout css to:

#content60 {float:left;width:60%;overflow:hidden;}
#content80 {float:left;width:80%;overflow:hidden;}
#content100 {float:left;width:100%;overflow:hidden;}

The PHP conditional statements in the head must appear after the line that links to the template.css file. This is because if there are two identical CSS style rules; the one that is last will overwrite all the others.
This can also be done in a similar fashion by having the if statement import a sub CSS file.

TIP
While you try to troubleshoot your conditional statements, you can add a line of code into your index.php, like this, to show what the value is:

This content column is <?php echo $contentwidth; ?>% wide

So we are half-way there, but now we have empty div containers where the columns are.

Hiding Module Code

When creating collapsible columns, it is good practice to set up the modules not to be generated if there is no content there. If this is not done, the pages will have empty <div>s in them, which can lead to cross browser issues.

To hide the empty <div>, the following if statement is used:

<?php if($this->countModules('left')) : ?>
<div id="sidebar">
<div class="inside">
<jdoc:include type="modules" name="left" style="xhtml" />
</div>
</div>
<?php endif; ?>

Using this code, if there is nothing published in left, then <div id="sidebar"> will not be outputted.

Using these techniques for our left and right columns, our index.php file now looks like the following code. We will also add an include for the breadcrumbs module, the module that shows the current page and pathway. Note that this now needs to be included in the index.php file and also published as a module.

<?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" />
<?php if($this->countModules('left and right') == 0) $contentwidth = "100"; if($this->countModules('left or right') == 1) $contentwidth = "80"; if($this->countModules('left and right') == 1) $contentwidth = "60"; ?>
</head>
<body>
<div id="wrap">
<div id="header">     <div class="inside">         <h1><?php echo $mainframe->getCfg('sitename');?></h1>
<jdoc:include type="modules" name="top" style="xhtml" />     </div>
</div>
<?php if($this->countModules('left')) : ?>
<div id="sidebar">     <div class="inside">
<jdoc:include type="modules" name="left" style="xhtml" />     </div>
</div>
<?php endif; ?>
<div id="content<?php echo $contentwidth; ?>">     <div class="inside">
<jdoc:include type="module" name="breadcrumbs" style="none" />
<jdoc:include type="component" />     </div>
</div>
<?php if($this->countModules('right')) : ?>
<div id="sidebar-2">     <div class="inside">
<jdoc:include type="modules" name="right" style="xhtml" />     </div>
</div>
<?php endif; ?>
<?php if($this->countModules('footer')) : ?>
<div id="footer">     <div class="inside">
<jdoc:include type="modules" name="footer" style="xhtml" />     </div>
</div>
<?php endif; ?>
<!--end of wrap-->
</body>
</html>

The Least You Need to Know
Elements such as columns or module locations can be hidden (or collapsed) when there is no content in them. This is done using conditional PHP statements that are linked to different CSS styles.

I would recommend a slightly different way of producing the footer. In the manner shown here, it is hard coded into the index.php file, which makes it hard to change. Right now the "footer" module in the administrative backend shows the Joomla copyright and can't be easily edited. It would make much more sense to have a custom (X)HTML module placed in the footer location so the site administrator can more easily change it. If you wanted to create your own footer, you would simply unpublish that module and create a custom html module with whatever language you wanted.

In this case we would replace

<jdoc:include type="modules" name="footer" style="xhtml" />

with

<jdoc:include type="modules" name="bottom" style="xhtml" />

We must also remember to add this position to the templateDetails.xml file.

TIP
There are several names associated with modules in Joomla: banner, left, right, user1, footer, and so on. One important thing to realize is that the names do not correspond to any particular location. The location of a module is completely controlled by the template designer, as we have seen. It's customary to place them in a location that is connected to the name, but it is not required.

This basic template shows some of the fundamental principles of creating a Joomla template.

CSSTemplateTutorialStep2

We now have a basic, but functional template. Some simple typography has been added, but more importantly, we have created a pure CSS layout that has dynamic collapsible columns. I have created an installable template that is available from the Compass library:

CSSTemplateTutorialStep2.zip

Now that we have the basics done, let's create a slightly more attractive template using the techniques we have learned.

 

This sample content is excerpted from the forthcoming book, titled Joomla! A User's Guide: Building a Joomla! Powered Website, to publish in 2007, by Prentice Hall Professional.

The author, Barrie North creates Joomla Templates and Tutorials at  www.compassdesigns.net. This work is licensed under a Creative Commons Attribution NonCommercial ShareAlike 2.5 License. This attribution must be reproduced in full.



Last Updated on Monday, 07 December 2009 14:01
 

Get Joomla Tips by RSS or Email

 

Joomla 1.5 BookGet the Joomla 1.5 Book

Get the #1 best-selling about Joomla 1.5 from Amazon and free access to the companion site, joomlabook.com.