Een basis Joomla! template aanmaken

From Joomla! Documentation

Revision as of 13:53, 15 April 2016 by Meta (talk | contribs)
Split Page into Specific Joomla! Versions - J2.5 and 3.x

It has been suggested that this article or section be split into specific version Namespaces. (Discuss). If version split is not obvious, please allow split request to remain for 1 week pending discussions. Proposed since 10 years ago.

Inleiding

Met deze tutorial wordt een inleiding gegeven tot het maken van Joomla! template. We beschrijven de benodigde essentiële bestanden en code voor het maken van een basis template. Code kan gekopieerd en geplakt worden, er is weinig aanpassing meer nodig.

Het instellen van een directory-structuur

Om de meest eenvoudige template te maken, maak je een nieuwe map aan in de map 'templates'. De naam van deze map met je template is bijvoorbeeld "mynewtemplate".

Met je favoriete teksteditor maken we de bestanden index.php en templateDetails.xml aan. Voor een goed georganiseerde template maak je twee mappen aan: "afbeeldingen" en "css". In de map "css" map maak je een bestand met de naam sjabloon.css.

CSS code kan worden opgenomen in je index.php bestand, maar dat heeft nooit de voorkeur. Maak een apart CSS-bestand aan en koppel dit aan het bestand index.php, met behulp van de link - tag. Dit verkort de laadtijd van je pagina' s.

Dit is een overzicht van de mappen en bestandsstructuur:

* mynewtemplate/
** css/
*** template.css
** images/
** index.php
** templateDetails.xml


Het maken van een basic templateDetails.xml bestand

Het templateDetails.xml bestand is essentieel. Zonder dit bestand wordt de template niet gezien door Joomla!. Het bestand bevat de metadata over de template.

De syntax van het bestand verschilt per Joomla versie.

In de verouderde versie Joomla 1.5 gebruikte je het volgende:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE install PUBLIC "-//Joomla! 1.5//DTD template 1.0//EN" "http://www.joomla.org/xml/dtd/1.5/template-install.dtd">
<install version="1.5" type="template">
	<name>mynewtemplate</name>
	<creationDate>2008-05-01</creationDate>
	<author>John Doe</author>
	<authorEmail>john@example.com</authorEmail>
	<authorUrl>http://www.example.com</authorUrl>
	<copyright>John Doe 2008</copyright>
	<license>GNU/GPL</license>
	<version>1.0.2</version>
	<description>My New Template</description>
	<files>
		<filename>index.php</filename>
		<filename>templateDetails.xml</filename>
		<folder>images</folder>
		<folder>css</folder>
	</files>
	<positions>
		<position>breadcrumb</position>
		<position>left</position>
		<position>right</position>
		<position>top</position>
		<position>user1</position>
		<position>user2</position>
		<position>user3</position>
		<position>user4</position>
		<position>footer</position>
	</positions>
</install>

Voor Joomla 2.5 en later gebruikte je de volgende versie. Wijzig version="2.5" in de versie van je Joomla! installatie.

<?xml version="1.0" encoding="utf-8"?>
<extension version="2.5" type="template">
	<name>mynewtemplate</name>
	<creationDate>2008-05-01</creationDate>
	<author>John Doe</author>
	<authorEmail>john@example.com</authorEmail>
	<authorUrl>http://www.example.com</authorUrl>
	<copyright>John Doe 2008</copyright>
	<license>GNU/GPL</license>
	<version>1.0.2</version>
	<description>My New Template</description>
	<files>
		<filename>index.php</filename>
		<filename>templateDetails.xml</filename>
		<folder>images</folder>
		<folder>css</folder>
	</files>
	<positions>
		<position>breadcrumb</position>
		<position>left</position>
		<position>right</position>
		<position>top</position>
		<position>user1</position>
		<position>user2</position>
		<position>user3</position>
		<position>user4</position>
		<position>footer</position>
	</positions>
</extension>

Er staat informatie tussen markup-tags (de <element>s). De beste aanpak is om dit te kopiëren en plakken naar je eigen templateDetails.xml bestand en relevante stukjes (zoals <name> en <author>) aan te passen.

Het onderdeel <files> bevat alle bestanden die je wil gebruiken. Met het element <folder> kan een hele map in één keer worden gedefinieerd.

Laat de posities zoals ze zijn, dit is een gemeenschappelijke set waarmee je makkelijk schakelt tussen standaard templates.

Het maken van een basic index.php bestand

Het index.php bestand wordt de kern van elke pagina die Joomla! levert. In wezen, maak je een pagina (zoals een HTML-pagina), maar plaats je PHP code waar de inhoud van uw site moet komen. De template werkt door het toevoegen van Joomla code op de module posities en de component sectie van uw template. Als er iets wordt toegevoegd aan de template, wordt dit weergegeven op alle pagina's, tenzij het is toegevoegd aan een van deze secties via het Joomla CMS (of aangepaste code).

Deze pagina toont de kale code klaar om te knippen en plakken in uw eigen ontwerp.

Begin

Een Joomla template begint met de volgende regels:

<?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; ?>" >

De eerste regel stopt ondeugende mensen om naar uw code te kijken en slechte dingen te doen.

De tweede regel is de Document Type Declaration (DOCTYPE), deze vertelt de browser en web crawlers welke versie van html de pagina gebruikt. Het "doctype" zoals hierboven beschreven is HTML5, een nieuwere versie van HTML welke grotendeels backwards compatibel is, maar is voorzien van veel nieuwe mogelijkheden. Houd er rekening mee dat dit niet goed werkt met Internet Explorer 8 (en ouder) zonder een hack. Onderzoek deze situatie en de wensen van je klant alvorens je besluit welk doctype je wilt gebruiken. Dit is echter wel de versie die in Joomla Joomla 3.0 en hoger wordt gebruikt.

Op de derde regel begint ons HTML document en beschrijft welke taal voor deze website wordt gebruikt. Een html document is verdeeld in twee delen, een head en body deel. De head bevat de informatie over het document en de body zal website code bevatten waarmee u de lay-out definieert..


Head

<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>

De eerste regel geeft Joomla de juiste header-informatie. Dit is de pagina titel, meta-informatie, evenals JavaScript code. De rest maakt twee links naar systeem style sheets en naar uw eigen opmaakmodel (als het genoemde template.css en is staat in de "css" map van uw template. Dus als uw template in http://www.mysite.com/templates/my_template/ staat, dan staan de css-bestanden in http://www.mysite.com/templates/my_template/css/).

Body-Gedeelte

<body>
<jdoc:include type="modules" name="top" /> 
<jdoc:include type="component" />
<jdoc:include type="modules" name="bottom" />
</body>

Ongelooflijk, dit is voldoende! Ja, het is een heel eenvoudige lay-out, maar het werkt. Alles wordt gedaan door Joomla!. Deze regels, meestal genoemd jdoc staten, vertellen Joomla wat toe te voegen aan de uitvoer van bepaalde onderdelen van het Joomla systeem. Opmerking: u moet ervoor zorgen dat uw menu is ingesteld op de module-positie "top".

Module Posities

De regel, hierboven, die zegt name="top" voegt een module positie toe genaamd "top" en maakt het Joomla mogelijk modules te plaatsen in deze sectie van de template. De type="component" regel bevat alle artikelen en de hoofd content (eigenlijk, het component) en is erg belangrijk. Het wordt geplaatst in het midden van de template.

Opmerking:Je kunt je eigen module regels overal waar je wilt in de body plaatsen, maar je moet de corresponderende regel ook toevoegen in de templateDetails.xml die in dezelfde folder als de index.php van je template staat.

Einde

Tenslotte - het laatste stukje:

</html>

Standaard Afbeeldingen

Als je afbeeldingen wilt toevoegen aan de template kan je het op deze manier doen:

<img src="<?php echo $this->baseurl; ?>/templates/<?php echo $this->template; ?>/images/myimage.png" alt="Custom image" class="customImage" />

Hier is de template variabel vul de naam van de template in

Basis CSS

Je kunt op deze manier basis css toevoegen:

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

Ieder bestand wat toegevoegd is moet in een regel in de templatedetails.xml van de template vermeld worden,behalve als het verwijst naar een sub-folder (die kan weer opgenomen worden in een <folder> element).


This leaves a final file of:

<?php defined('_JEXEC') or die('Restricted access');?>
<!DOCTYPE html>
<html xml:lang="<?php echo $this->language; ?>" lang="<?php echo $this->language; ?>" >
<head>
<jdoc:include type="head" />
<link rel="stylesheet" href="<?php echo $this->baseurl ?>/templates/<?php echo $this->template ?>/css/template.css" type="text/css" />
</head>
<body>
<jdoc:include type="modules" name="top" /> 
<jdoc:include type="component" />
<jdoc:include type="modules" name="bottom" />
</body>
</html>

Testing the template

Find the template in the Template Manager, select it and click Default to make it the default template.

Joomla 1.5 In Joomla! 1.5, your new template will show up immediately in the Template Manager, accessible via Extensions -> Template Manager.

Joomla 2.5+ In the Joomla! 2.5 series and later, you first need to tell Joomla! that you have created a new template. This feature is called Discover Extensions and can be accessed via Extensions -> Extension Manager -> Discover (i.e. the Discover tab). Click Discover (i.e. the Discover button) to discover your template, then select it and click Install to install it. Now your template should show up in the Template Manager (Styles), accessible via Extensions -> Template Manager.

Note you can create your template outside of Joomla and simply install it like any regular extension.

HINT: there are a couple of ways you can preview your index page as you put it together, either insert the styles into the head of the index page or directly link it to the style sheet you will be using temporarily. You can remove these links before packaging the file.

Packaging the template for installation

A directory with several loose files is not a convenient package for distribution. So the final step is to make a package. This is a compressed archive containing the directory structure and all the files. The package can be in ZIP format (with a .zip extension), in TAR-gzip format (with a .tar.gz extension), or in TAR-bz2 format (with a .tar.bz2 extension).

If your template is in a directory mytemplate/ then to make the package you can connect to that directory and use commands like:

  • tar cvvzf ../mytemplate.tar.gz *
  • zip -r ..\mytemplate.zip *.*

Note to Mac OS X users

Note to template developers using Mac OS X systems: the Finder's "compress" menu item produces a usable ZIP format package, but with one catch. It stores the files in AppleDouble format, adding extra files with names beginning with "._". Thus it adds a file named "._templateDetails.xml", which Joomla 1.5.x can sometimes misinterpret. The symptom is an error message, "XML Parsing Error at 1:1. Error 4: Empty document". The workaround is to compress from the command line, and set a shell environment variable "COPYFILE_DISABLE" to "true" before using "compress" or "tar". See the AppleDouble article for more information.

To set an environment variable on a Mac, open a terminal window and type:

export COPYFILE_DISABLE=true

Then in the same terminal window, change directories into where your template files reside and issue the zip command. For instance, if your template files have been built in a folder in your personal directory called myTemplate, then you would do the following:

cd myTemplate
zip -r myTemplate.zip *


Conclusion

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.