Creating a basic Joomla! template
From Joomla! Documentation
Revision as of 21:51, 17 November 2008 by Dragmen
The purpose of this tutorial is to serve as an introduction to creating Joomla! templates. It will cover the essential files and code needed to create a basic template. The code is presented so it can be cut and pasted with very little modification needed.
Setting up a directory structure
To make the most basic template, create a new folder in the templates folder. Name this folder after your template i.e. mynewtemplate.
Using a text editor create the files index.php and templateDetails.xml.
To keep things organized, make 2 new folders called images and css. Inside the css folder create a file called template.css.
Although it is fine to place all your CSS code directly in your index.php file to start, many web developers prefer to place their CSS code in a separate file that can be linked from multiple pages using the
link tag. This may also shorten the loading time of your pages, since the separate file can be cached.
This is the most basic practical setup.
Outline of folder and file structure:
Het creeren van een basis templateDetails.xml file
Creating a basic index.php file
index.php file becomes the core of every page that Joomla! delivers. Essentially, you make a page (like any HTML page) but place PHP code where the content of your site should go. The template works by adding Joomla code into module positions and the component section in your template. Anything added to the template will appear on all pages unless it is added to one of these sections via the Joomla CMS (or customised code).</translate>
<translate> This page will show the bare-bones code ready for you to cut and paste into your own design.</translate>
<translate> A Joomla template begins with the following lines:</translate>
<?php defined( '_JEXEC' ) or die( 'Restricted access' );?> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<?php echo $this->language; ?>" lang="<?php echo $this->language; ?>" >
<translate> The first line stops naughty people looking at your coding and getting up to bad things.</translate>
<translate> The second line is the Document Type Declaration (DOCTYPE), which tells the browser (and web crawlers) which flavor of HTML the page is using. The doctype used above is HTML5, a newer version of HTML that is largely backwards compatible, but contains many new features. You should be aware that this will not work well in Internet Explorer 8 or earlier without a hack. You might want to investigate this situation and your clients' wishes before deciding on which doctype you want to use. However this is used in Joomla and higher.</translate>
<translate> The third line begins our HTML document and describes what language the website is in. A html document is divided into two parts, head and body. The head will contain the information about the document and the body will contain the website code which controls the layout.</translate>
<head> <jdoc:include type="head" /> <link rel="stylesheet" href="<?php echo $this->baseurl ?>/templates/system/css/system.css" type="text/css" /> <link rel="stylesheet" href="<?php echo $this->baseurl ?>/templates/system/css/general.css" type="text/css" /> <link rel="stylesheet" href="<?php echo $this->baseurl ?>/templates/<?php echo $this->template; ?>/css/template.css" type="text/css" /> </head>
template.css and is located in the
css folder of your template directory. So if your template is in
http://www.mysite.com/templates/my_template/ then the css files will go in
<body> <jdoc:include type="modules" name="top" /> <jdoc:include type="component" /> <jdoc:include type="modules" name="bottom" /> </body>
<translate> Amazingly, this will suffice! Yes, it's a very basic layout, but it will do the job. Everything else will be done by Joomla!. These lines, usually called jdoc statements, tell Joomla to include output from certain parts of the Joomla system. Note: you will need to ensure your menu is set to go into the "top" module position.</translate>
<translate> Above, the line which says
name="top" adds a module position called top and allows Joomla to place modules into this section of the template. The
type="component" line contains all articles and main content (actually, the component) and is very important. It goes in the centre of the template.</translate>
<translate> Note: You can add your own module lines anywhere you want in the body, but you have to add a corresponding line to the templateDetails.xml file which sits alongside the index.php of your template.</translate>
</translate> <translate> Finish it off - one last bit:</translate>
</translate> <translate> If you want to add any images to the template you can do so like this:</translate>
<img src="<?php echo $this->baseurl; ?>/templates/<?php echo $this->template; ?>/images/myimage.png" alt="Custom image" class="customImage" />
<translate> Here the template variable will fill in the name of your template.</translate>
<translate> You can add custom css like this:</translate>
<link rel="stylesheet" href="<?php echo $this->baseurl ?>/templates/<?php echo $this->template;?>/css/styles.css" type="text/css" />
<translate> Every file which is added must have a line in the templateDetails.xml file for the template, unless it resides in a sub-folder (which can be included in a <folder> element).</translate>
You should now have created a template that works. It won't look like much yet. The best thing to do now is start experimenting with the layout.