| Joomla 1.5 Template Tutorial - Creating a Simple Blank Template |
|
Page 3 of 7
Creating a Simple Blank TemplateTo understand the contents of a template, we will start by looking at a blank Joomla template. The Template File ComponentsThe 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. /element/templateDetails.xml
/element/index.php These two filenames and location must match exactly because this is how they are called by the Joomla core script. The first of these is the template XML file. templateDetails.xmlThis 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.phpThis 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
templateDetails.xmlThe 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> This e-mail address is being protected from spambots. You need JavaScript enabled to view it </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:
<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>
index.phpWhat 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 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. Some say that serving XHTML as text/html is considered harmful. If you actually understand that statement you are well ahead of the game and beyond this guide. You can read more at hixie.ch/advocacy/xhtml. 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:
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. NOTE Designing your site to standards can on one level be reduced to saying what you do and then doing what you say. Here are some useful links, which will help you understand DOCTYPE and quirks mode:
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="/feed/rss" rel="alternate" type="application/rss+xml" title="RSS 2.0" /> <link href="/feed/atom" rel="alternate" type="application/atom+xml" title="Atom 1.0" /> <script type="text/javascript" src="http://localhost/Joomla-1.5RC2/media/system/js/mootools.js"></script> <script type="text/javascript" src="http://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[md]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 1.5 Template BodyCreating 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.
The template contains the following in reasonably logical order:
The Least You Need to Know 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[md]a browser, a spider, or a screen reader. Semantic layout is the cornerstone of accessibility. NOTE 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 as easily have 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 XHTML output, either from modules of components. <jdoc:include type="component" />
NOTE 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. CSSTemplateTutorialStep1At this point we have a very bare template. I have created an installable template that is available from the Compass library: CSSTemplateTutorialStep1.zip. This will install a template that has only two files, the index.php and templateDetails.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.
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. |
||||||||
| Last Updated on Monday, 07 December 2009 14:01 |