How to Build a Joomla Template: Start to Finish

To begin, I am not even going to touch the inevitable flame war here. Joomla vs WordPress vs... who cares? And frankly, neither should you. You wouldn't hammer a nail with a saw, would you? So let's bear in mind that every job requires the correct tool.

That being said, this tutorial will demonstrate how to proceed from design to deployment of a complete Joomla template theme, including core override and template option parameters that you can use and abuse to your hearts content.

Getting Around

My goal is to provide something useful -- not ammunition. There are so many wonderful WordPress resources and so few for Joomla. This is my attempt to aid in the effort; I hope you find it useful. So let's begin!


The PSD

Back Up

Here is the design I am starting with. I found a free PSD file online here, and modified it for this tut. The easiest and fastest way I have found to get from PSD to HTML is how I will demonstrate here. Begin by making sure auto select layers is enabled in Photoshop, and then make note of which images you need to grab. Concentrate on parts that either you cannot, or do not want to duplicate, via CSS. This includes things, such as actual imagery, logo, and buttons (possibly). Shadows and gradients are your call, considering the new CSS3 properties, and should be decided on a case by case basis. For this tut, we will achieve those effects with CSS.

Old browsers get solid colors...fine with me!

Start by clicking on the logo to highlight its corresponding layer in the layers panel. Now, move the cursor to the thumbnail and option+click it to auto select the entire layer; you should should see ants dancing around it now.

Next, hit copy, create a new file, (cmd+c, cmd+n if you are rocking a Mac) and you will notice that the height and width are automatically set; so simply hit enter, then paste (cmd+v) and you have the logo. You may not see it as the background blends with the text, so the next step, for all images you copy that have transparency, is to turn off the background layer.

Next, save it as a .png file to preserve the pretty see-through-ness and name it something obvious like "logo" or such. BAM! Image one is done. Rinse and repeat for all the images you need to grab.

A tricky one can be the button dots used for the slider navigation. The easiest way that I've found to do this is to copy the layer or group that makes up one of the dots, and then put it into a new file, bigger than its actual size. Then turn off the background layer, go to trim in the edit menu,

and get rid of the transparent pixels so that the size is precise.

Continuing on, edit the image canvas size to be 200% height, and simply copy, (option+click) the layer or group and drag it down to duplicate it. Make a change to this one and you now have an active state! Easy as pie right? Save this new image and our buttons are now ready!

Once you have gathered the pics, drop them into a folder, called images, and now it is time to begin building the HTML structure; let's dig into some HTML.


The HTML

Back Up

So we have our design and images; now it's time to turn them into something usable. You can skip this step, and very well may learn to as you become more adept at templating a site; however, I still find it helpful to construct the static version before full conversion into a theme. Create a simple folder structure like so, and then in index.html, build a standard HTML skeleton with a structure.

This is the base HTML5 we are going to use to create this site. It's based on the HTML5 Boilerplate. You will notice the inclusion of the Moderizr script, so be sure to download it there if you need it. Starting from the top, let's go ahead and add in our logo and navigation sections, so inside the header_container, add the following chunk:

This covers the logo link and a simple menu system, complete with dropdown goodness. I used the new tag, nav, for the semantic HTML5 markup. Up next is our featured content slider area, so, inside of our wrapper section, add the following to create an image list:

Feel free, obviously, to change the text and image links to suit your own setup, or simply follow along here. Be sure that you are not hopping into a browser to check this out yet, because it will look dreadful!

The list of elements inside the slider ul will all start out hidden, except for the first one, which is why it has a class assigned to it: first. This way, we keep all of the images from briefly displaying on page load, bypassing that annoying flicker.

Next, we add some main content stuff; drop in these two sections. I am simply duplicating what was in the PSD when we started.

I gave the first chunk an aside tag as I feel it tangentially relates to the rest of the content, and the main content gets the article tag as it would be the main area.

Now to incorporate our tri-column section:

I aptly named the section with an ID of columns, and, inside, have three articles with a class to style them properly later. I added in a horizontal rule to break it up a bit as well.

Finally, we arrive at our images section:

Worth noting here is the inclusion of the rel attribute on the image links inside of the figure tags. I did this to incorporate the PrettyPhoto plugin we will be using to spice these pics up a bit. The image displayed is the smaller, thumbnail size one, and the actual href points to the full-sized image.

Now, down to the bottom, inside of the footer_container, drop in these sections to give us our three columns and associated content:

And finally, just outside of the closing section tag for the footer_container, paste in our lower copyright area that includes the link to jump back to top.

Notice how the href points to the header ID; this will allow a click here to jump the page right back up to the top-most section. Later, we will enhance this with some scrolling fun.

Onto some styling now; if you take a look in a browser, you will find something similar to this -- but don't worry, we are about to rectify the situation!


The CSS

Back Up

Let's work on the styling a bit. Inside your style.css file, begin by dropping in this base here. It contains a reset and basic setup of the elements, and of course, the ubiquitous clearfix.

Ok, that part is done... yay; now let's add in our own styling!

Here we have some basic typography setup, colors and sizes.

Now, give some styling to the top region.

Note the image in the logo styling; if you did not name yours the same, be sure to change it here.

The menu comes next. Here, we take care of positioning and text sizing, as well as handling multi-level lists!

There's some scary CSS3 in there, but it adds a pretty little curve to the bottom part of the drop down list, so take it or leave it

Also, note how initially we start with the submenu ul as hidden, and only display it when we hover over its parent list item. This ensures that the menu is still accessible for folks without JavaScript enabled, because we will enhance this later on.

We'll next add in more structure styles to handle the main wrapper and the uber fancy slider component we are putting to use.

Continuing, we'll configure the footer section.

Finally, let's style some of the main text.

Take a note of the final classes in the code above, zoom_out and zoom_in. This is another enhancement for the popup images, to add a little flare -- you will see soon enough!

At this point you should have something along the lines of this:

Hey, that isn't too bad, but note that some parts may not work as awesomly yet, because the scripts to make them function we have not yet been created! So, time to get on that.


The JavaScript

Back Up

This part should be a snap! Mostly, because we are going to use a couple of awesome plugins. First up is the Cycle plugin, by @malsup. We will use this to build our slider, and also the PrettyPhoto plugin, by @scaron will be used for our lightboxing. Enhancing this will be the easing plugin for some more spice, and localscroll to handle our ups and downs. Each of the links above should link to the author's page where you can find and download the script you wish. Once we have the plugins downloaded, we need to include them within our code, like so:

Note that, at the time of production, try to group these scripts into one file, in order to limit the number of HTTP requests.

Be sure to put this in the footer "after" we include the jQuery library, in order for this to all function. Bear in mind we will be combining all of these shortly. Now to make these take effect, we have to write a small bit of scripting.

Firstly, drop in this line of code to handle the hash tag scrolling:

This ensures that our "back to top" button will smoothly glide back up. Next, let's work with the slider core.

This will add the bullet navigation for the slides, and setup the duration and easing parameters. Next, we handle the lightbox with the prettyphoto code, like so:

This instructs each image, that we assign the prettyPhoto rel to, to behave in the proper manner. In order to spruce up the images, add in the following,

This will add a nice hover in, fade out effect when we mouse over the images. Pretty!

And finally the menu; add the following snippet, still in between the document ready tags.

This simply makes sure that the submenu is hidden on page load, and then sets up the animations to take place when we hover over the corresponding menu items. Simple and fun!

Okay, now check the page and make sure that all the functions and JavaScripts are working as expected. We are now going to combine these multiple files. Go ahead and open the file named plugins.js and inside paste the following:

We will pass all of the plugin source code in between this function. Granted, these plugins are by good developers and have been created the right anyway, but this is a good habit to prevent potential conflicts with the jQuery namespace. Grab the source for cycle, easing, localscroll, scrollto and prettyphoto and paste each one in, one at a time, in between the remap function in plugins.js. Once that is done, save the file and open up script.js. Here, we will take all the code in between the script tags in our footer and paste it in, inside one document ready function.

Now, we only need to import these two files and are good to go. Once done, be sure to recheck and make sure that everything still works correctly on the page.

So we installed the PrettyPhoto plugin and its script, but we also need to grab its default images and styling. So grab the folder named prettyPhoto from the plugin file you downloaded; it will be in the images folder. Simply copy that folder in its entirety to your templates images folder. While you are there, copy the prettyPhoto.css into your template's css folder as well. At this point, your file structure should look something close to:

Now, hey, guess what? Refresh your html page to see some pretty sweet stuff going on! Hopefully, you have a functional menu, slider, and scrollable and lightbox imagery. Take a moment to revel in the fact that you just made some awesomeness happen.

Back? Okay, now it's time to tear this boy apart and templatize it for the homestretch of our Joomla Template tutorial!


Joomla Construct

Back Up

We now have a fully functional static HTML site, and it is time to templatize this monster. Joomla has a standard file structure that we need to adhere to. Create a folder structure like so, and be sure to include all the files -- I will explain what each of them do shortly.

Starting with component.php; this file is a structure template that Joomla uses to display content called with the component parameter. Essentially, when you want to see part of the site that is just its content, and not the header/footer etc. For now, you will simply be copying mine for the default usage. The CSS folder will hold, you know, CSS and the same with the images. Index.html is simply a generic page that display nothing, and it is a good practice to keep one of these empty index files in the root of each folder you create to keep prying eyes away.

Index.php will be the main file; nearly everything will take place in here. Template_thumbnail.png is, you guessed it, an image of the template itself that will show in the admin section of Joomla, and template_details.xml is the main control file that Joomla reads to install your template. It contains basic information about the file structure of your theme, as well as template parameters and module position names.

The Params.ini file needs to be here and can remain blank for now. This will be a writeable file that stores the template parameter data that Joomla can access later on.

Finally, the HTML folder is where we will store files to override the default Joomla behavior for certain display types, such as the frontpage. So inside, you will find another folder named frontpage, which itself holds a few more files. Each one will take precedence over the default core files that would be used to display the frontpage content. The benefit of this is that we can alter the structure here completely, or simply remove a line we don't want or need. Be sure to copy over your CSS and Images folders into this directory for your template as well.

Sidestep

To gain an idea of where the files and folders inside the HTML folder come from, head to your root installation of Joomla and look in the administrator folder, then in the components folder and finally open the com_content folder. You will find a nice list of core components -- components being the way Joomla displays information in an abstract way from logic. This is only an overview; you can override just about every aspect of Joomla output by adding a folder to the HTML folder in your theme root and creating a new view file. For example, to override the main menu output, you would create a path such as [YOUR_TEMPLATE_NAME]/html/mod_mainmenu/default.php where mod_mainmenu is the name of the core module you wish to override.

Breaking Into Pieces

So, easiest thing first; take a snapshot of your HTML page and save it as template_thumbnail.png. This should be placed in the root of the template folder. For index.html, simply open it up and paste the following inside:

Make a few copies of this file and put them in the root of each folder.

Wow, two files down already!

Now, for component.php, I suggest you use the one from the accompanying download, as it is too much to paste in here, and it is just default behavior we are not going to touch, but is helpful to have in your theme, depending on usage. Again, for the HTML folder, use the provided download and drop the entire folder and contents into your template root.

I like to break things up a bit more, so create a folder called includes, and, inside two more folders, one named sections, and the other named scripts. You guessed correctly: inside the scripts folder, copy all of the contents of your HTML folder version's js folder. Now, head into the sections folder and create a few new files:

  • head.php
  • header.php
  • footer.php
  • scripts.php
  • setup.php

It seems like a lot, but it is really not. This is simply a good method to separate content and keep things as modularized as possible. We'll start with setup.php, so open that one up and paste the following in:

This is really a personal preference, but, for the sake of ease, I suggest you follow along with it. First, we instruct Joomla to include the hook for its head, thus enabling core functions access. Following that, I manually unset the mootools that is loaded by default.

Save and done! For now, we are finished with the set up, so open head.php and add:

All we do here is point to the base stylesheet we will use, and load up the Modernizr script. This is the only Javascript that gets loaded into the head section. The href attributes may look odd, and that is to keep them as dynamic as possible. The PHP statement,

simply prints out the absolute base URL of the site as Joomla knows it, and then concatenates it with the templates folder and points it to our own template. This way, we can easily access our own files and folders.That's it! Save head.php and move along to header.php.

Inside header paste in the following,

This code opens the body tag in an awesome way that allows us to target individual versions of Internet Explorer. Then, it creates our top header section and navigation area. Finally, we open the main wrapper.

The new code here is the statement,

This is a native to Joomla that runs a check before the page renders on the specific module position. It determines if there is anything setup to go there. So, if we have not created a menu and assigned it to the position "main_nav" then this statement will not execute. Be sure to close the if statement.

Ok save it; we're done for now. While we are here, open footer.php, and add:

You already know what's happening in here now. Again, we simply create our sections and close out the site. We also do a few more checks for module positions so we can append their contents when needed.

And here, we come to index.php, the main attraction. Add the following code:

Yup, it is everything that was left over! Now top to bottom: we start by declaring the new Doctype in HTML5 fashion. Next, we've added the no-js class to the html tag if you are using Modernizr. We immediately include two of the files we already setup: our head.php and setup.php. This time, note how we use a new variable, jPATH_ROOT to concatenate our way into our template. Once again, we're keeping things as dynamic as possible so that we can transport this code across multiple templates. After we include the top section, we close out the head and start building our page. Including the header.php kicks that off and brings in the top section of our soon to be awesome website.

Now, open the main section and drop in another Joomla call:

This call to component essentially tells Joomla to output right here exactly what is put into the WYSIWYG editor for the page currently being viewed.

Next we include the footer.php file to end things up, and then the last file we created in the sections folder, scripts.php.

What? Yes I know that file is empty still! So, let's go fill it up. Open scripts.php and paste in the following:

Again, this is reading in our script files, and keeping them segmented away from other code. Now, we add in the functions code like so:

And we are good to go. Moving on!


The Frontpage

Back Up

You may have noticed that index.php seems to be quite lacking in, stuff -- like everything that we built into index.html for the first version. Why is that? Well, because that is for our homepage. Now, we are going to override Joomla's frontpage to show our custom homepage. Head into the html folder, and then into com_content and the frontpage directory. Inside should be two PHP files; make one called default.php and one called default_item.php. We are going to rip 95% of the guts from these to make our own, so open up default_item.php. If anything is there, delete it and replace it with:

Nice, right? This is setting up the output for whatever text was entered into the homepage component. Now open default.php and again, delete all that may be there and paste in:

There's lots of new stuff here, and this is a pretty complicated page override; so let's go through it all.

Because the frontpage is actually displayed via a component, the override does not have access to the standard Joomla directives; we have to instantiate them. So we create an object and use it to grab the params list that is set when we update the template setting parameters. Once we have that, we can grab the chosen parameters as we normally would. We then use the variables to output the info into the slider list markup.

The modules class is not included by default either. We need to create an object for it and load its renderer that will allow us to output our custom modules where they need to go. Two variables are created after that: columns and images, which are then set equal to their respective module positions, so that, later down the page, they can be called, using:

Finally, the ugly part in the middle,

This is the only part kept from the original default.php for the frontpage, as it is how Joomla communicates with default_item.php, and decides if it is okay to output whatever we had in the page. I know there is a lot to this file, but hopefully it makes some level of sense.

If you have been working along, you should noe have a file hierarchy that looks like so:

All that is left appears to be the XML file. Let's handle that now.

Obviously, feel free to change the name and URLs to fit your needs. This file sets up the install process to get our template into the database, so that Joomla will recognize it. After you have this info in, we need to tell Joomla what to expect from our template folder. Before the closing </install> tag, paste in:

Remember the module positions we were querying earlier? Here is where we inform Joomla what positions to expect by default:

Dont worry if you miss a position, because you can enter new ones later from the administration area if needed. Finally, add:

Wait, what's that? Glad you asked. Wouldn't it be cool if we could change stuff in our template on the fly from the backend? Yes, it would! This is where we get the opportunity to setup some template parameters. In all honesty, you can go nuts here, but for the purposes of this tutorial, I will only add some basic parameters.

Let's start with a simple text parameter to set the copyright in our footer. I know this looks like backtracking, but, this way, we learn a second way of doing things, and a better way to integrate it! Add this in between the params tags:

The first param type is the spacer in which we can put a label. Make note of the ASCII, however, if you wish to bold or italicize the text in there.

How about a second stylesheet? If so, add this parameter:

Once again, note how the name of the parameter is style, and the radio button allows one stylesheet to be chosen at a time. Now before we jump into the big one, let's first finish setting up these parameters. So open up setup.php and add:

This creates two variables we can use that hold the input data for copyright and style, respectively. Now, open head.php and replace the call to the style.css with this line:

Here, we use the variable we just created to concatenate into the string to our second CSS file. Don't forget to create one! It will use the actual value from the parameter, so the default will output style.css, and dark will output dark.css. Create a second CSS file, named dark.css and change anything in it for the time being -- only for the sake of reviewing difference. I made the header and footer all black.

Now open footer.php and replace

...with:

Now we have one less module to make, and we allow the copyright data to be set inside a parameter. Sweet! Head back into tempate_details.xml, and remove the copyright position. Then, back into the parameter area, let's setup our slider.

Here, we create another spacer to segment our slider and then a few parameter fields. One will be a drop down list for choosing the image to display, and the second one, the caption for each slide. The drop down image list works as a path; so inside your template images folder, create a new folder, named slides, and insert the images that you want to be made available to the slider section. Be sure to change where it says "tutorialtemplate" to the name of your template folder.


Dynamic Parameters

Back Up

I debated including this part, but it is just too cool not to! You noticed that PrettyPhoto has multiple folders and lots of CSS right? That is because it has a few built in themes, which makes it ideal for a situation where you can change stylesheets. We can make some dynamic parameters to change the theme that PrettyPhoto will use!

Within template_details.xml, add the following section for parameters:

So, we create a new section label and a dropdown list to choose the available themes. The value returned will be a string that corresponds to the parameter PrettyPhoto expects. To handle this, we need to make two more edits. First open up setup.php and add the following to grab the parameters that we can now set:

The first line above sets a variable to the parameter we will choose, and then we add that parameter to an array with a key of theme -- you will learn why in a minute. Save and close this, then open scripts.php.

Find the code:

And add this above it:

This is why we put the parameter into an array. We can use the magic json_encode function to create a parameter list that we can send into a jQuery function. Awesome! So replace the prettyPhoto function parameter with this new variable, like so:

We can now dynamically change the theme from the template parameter section! Awesome indeed.

Idea!

Feel free to take this a step further and add multiple transition options to the slideshow.


Install Template

Back Up

We should be all done! Wow, how exciting. Now, zip up this folder and install it to your Joomla installation. With any luck, there will be no errors!

Jump into template manager, and set your new template as the default.

Be sure to create a new page, called home and assign it as frontpage. In it, you can paste the following, which will be output from the Joomla "component" module that we reviewed earlier in this tutorial. Please note: be sure to enter this into the HTML view, not the WYSIWYG view, to preserve the tags.

Speaking of tag preservation, we need to set it up so that the TinyMCE editor does not eat our code! Navigate to the plugins section and click on TinyMCE.

On the right, you will find an option to instruct Joomla to never perform "code cleanup on save." Check it, and save.

Phew, code is safe now! Okay, into the article manager and back to edit your frontpage:

Sweet. You may notice that your image did not appear. That is because the paths have changed and /images no longer goes to where it used to. Be sure to alter your image paths to account for this; simply add to the front of the path, templates/YOUR_TEMPLATE_NAME/, and voila, images shall appear once more!

Inside the modules section, create a new custom html module, and in the HTML view, paste in the code and text from the columns section:

Set the position on it to columns and save. Do the same thing for a new images module, and be sure to update the image paths!

We are so close... homestretch!

Create one more custom html module, named footer, assign it to the footer position, and drop in:

Before we leave the module section, click on the main menu module that was there by default, and reassign its position to main_nav. Now, jump into template admin and click on our template. You should be greeted by the following screen:

Go ahead and pick a style and add some copyright text. Then, pick your images and set some captions for the slider. Hit save, and guess what, time to check this bad boy out! Point to the upper right and hit preview.

With any luck, you'll see a sweet frontpage with some great functionality. I hope this has been a useful read and resource for you. Please leave a comment below, if you need any help. Thanks for reading.

Tags:

Comments

Related Articles