Difference between revisions of "Creating a basic index file"

From Joomla! Documentation

m (Adjusted link after page move)
(Several markup changes. URL corrections.)
 
(16 intermediate revisions by 9 users not shown)
Line 1: Line 1:
The <tt>index.php</tt> 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. Here is the bare-bones code ready for you to cut and paste.
+
<noinclude><languages /></noinclude>
 +
<noinclude>{{Joomla version|version=3.x}}{{Joomla version|version=2.5|status=eos}}</noinclude>
 +
<translate><!--T:1-->
 +
The ''index.php'' file becomes the core of every page that Joomla! delivers. Essentially, you make a page (like any HTML page) but place [[S:MyLanguage/PHP|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>
  
Let's start at the top:
+
<translate><!--T:2-->
 +
This page will show the bare-bones code ready for you to copy and paste into your own design.</translate>
  
<source lang="php">
+
<translate>
 +
===Begin=== <!--T:3-->
 +
</translate>
 +
 
 +
<translate><!--T:4-->
 +
A Joomla template begins with the following lines:</translate>
 +
 
 +
<syntaxhighlight lang="php">
 
<?php defined( '_JEXEC' ) or die( 'Restricted access' );?>
 
<?php defined( '_JEXEC' ) or die( 'Restricted access' );?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+
<!DOCTYPE html>
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+
<html xmlns="https://www.w3.org/1999/xhtml/"
<html xmlns="http://www.w3.org/1999/xhtml"  
 
 
   xml:lang="<?php echo $this->language; ?>" lang="<?php echo $this->language; ?>" >
 
   xml:lang="<?php echo $this->language; ?>" lang="<?php echo $this->language; ?>" >
</source>
+
</syntaxhighlight>
The first line stops naughty people looking at your coding and getting up to bad things. The second tells the browser (and [[wikipedia:Web crawler|web crawlers]]) what sort of page it is. The third line says what language the site is in.
+
<translate><!--T:5-->
 +
The first line stops people looking at your coding and getting up to bad things.</translate>
 +
 
 +
<translate><!--T:6-->
 +
The second line is the [[wikipedia:Document Type Declaration|Document Type Declaration (DOCTYPE)]], which tells the browser (and [[wikipedia:Web_crawler|web crawlers]]) which flavor of HTML the page is using. The ''doctype'' used above is [[wikipedia:HTML5|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 old browsers 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 {{JVer|3.0}} and higher.</translate>
 +
 
 +
<translate><!--T:7-->
 +
The third line begins our HTML document and describes which language the website is in. An 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>
  
Now the header for real:
+
<translate>
 +
===Head=== <!--T:8-->
 +
</translate>
  
<source lang="php">
+
<syntaxhighlight lang="php">
 
<head>
 
<head>
 
<jdoc:include type="head" />
 
<jdoc:include type="head" />
Line 21: Line 40:
 
<link rel="stylesheet" href="<?php echo $this->baseurl ?>/templates/<?php echo $this->template; ?>/css/template.css" type="text/css" />
 
<link rel="stylesheet" href="<?php echo $this->baseurl ?>/templates/<?php echo $this->template; ?>/css/template.css" type="text/css" />
 
</head>
 
</head>
</source>
+
</syntaxhighlight>
 +
 
 +
<translate><!--T:9-->
 +
The first line gets Joomla to put the correct header information in. This includes the page title, meta information as well as system JavaScript.</translate>
 +
<translate><!--T:10-->
 +
The rest creates links to two system style sheets and to your own style sheet (if it's named ''template.css'' and is located in the ''css'' folder of your template directory. So if your template is in ''<nowiki>https://www.mysite.com/templates/my_template/</nowiki>'' then the css files will go in ''<nowiki>https://www.mysite.com/templates/my_template/css/</nowiki>'').</translate>
  
The first line gets Joomla to put the correct header information in. This includes the page title, meta information as well as system JavaScript.
+
<translate>
The rest creates links to two system style sheets and to your own style sheet (if it's named <tt>template.css</tt> and is located in the <tt>''css''</tt> folder).
+
===Body Section=== <!--T:11-->
 +
</translate>
  
Now for the main body:
+
<syntaxhighlight lang="php">
<source lang="php">
 
 
<body>
 
<body>
<jdoc:include type="modules" name="top" />  
+
<jdoc:include type="modules" name="top" />
 
<jdoc:include type="component" />
 
<jdoc:include type="component" />
 
<jdoc:include type="modules" name="bottom" />
 
<jdoc:include type="modules" name="bottom" />
 
</body>
 
</body>
</source>
+
</syntaxhighlight>
 +
 
 +
<translate><!--T:12-->
 +
Amazingly, this will suffice! Yes, it's a basic layout, but it will do the job. Everything else will be done by Joomla! These lines, usually called [[S:MyLanguage/jdoc statements|''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>
 +
====Module Positions==== <!--T:13-->
 +
</translate>
  
Amazingly, this will suffice! Yes, its a very basic layout, but it will do the job. Everything else will be done by Joomla!. Note: you will need to ensure your menu is set to go into the "top" [[Screen.modules.edit.15#Details|module position]].
+
<translate><!--T:14-->
 +
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 [[S:MyLanguage/component|component]]) and is very important. It goes in the centre of the template.</translate>
  
Finish it off - one last bit:
+
<translate><!--T:15-->
<source lang="php">
+
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>
 +
=== End === <!--T:16-->
 +
</translate>
 +
<translate><!--T:17-->
 +
Finish it off - one last bit:</translate>
 +
<syntaxhighlight lang="php">
 
</html>
 
</html>
</source>
+
</syntaxhighlight>
<noinclude>View the [[Basic template index file|full source code]] of this template.</noinclude>
+
 
[[Category:Tutorials]][[Category:Template Development]]
+
<translate>
 +
===Custom Images=== <!--T:18-->
 +
</translate>
 +
<translate><!--T:19-->
 +
If you want to add any images to the template you can do so like this:</translate>
 +
<syntaxhighlight lang="php">
 +
<img src="<?php echo $this->baseurl; ?>/templates/<?php echo $this->template; ?>/images/myimage.png" alt="Custom image" class="customImage" />
 +
</syntaxhighlight>
 +
 
 +
<translate><!--T:20-->
 +
Here the template variable will fill in the name of your template.</translate>
 +
 
 +
<translate>
 +
===Custom CSS=== <!--T:21-->
 +
</translate>
 +
 
 +
<translate><!--T:22-->
 +
You can add custom CSS like this:</translate>
 +
 
 +
<syntaxhighlight lang="php">
 +
<link rel="stylesheet" href="<?php echo $this->baseurl ?>/templates/<?php echo $this->template;?>/css/styles.css" type="text/css" />
 +
</syntaxhighlight>
 +
 
 +
<translate><!--T:23-->
 +
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>
 +
 
 +
<noinclude>
 +
<translate>
 +
<!--T:24-->
 +
[[Category:Tutorials]]
 +
[[Category:Template Development]]
 +
</translate>
 +
</noinclude>

Latest revision as of 10:42, 9 October 2022

Other languages:
Deutsch • ‎English • ‎Nederlands • ‎español • ‎français • ‎русский • ‎中文(台灣)‎
Joomla! 
3.x
Joomla! 
2.5

The 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).

This page will show the bare-bones code ready for you to copy and paste into your own design.

Begin[edit]

A Joomla template begins with the following lines:

<?php defined( '_JEXEC' ) or die( 'Restricted access' );?>
<!DOCTYPE html>
<html xmlns="https://www.w3.org/1999/xhtml/"
   xml:lang="<?php echo $this->language; ?>" lang="<?php echo $this->language; ?>" >

The first line stops people looking at your coding and getting up to bad things.

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 old browsers 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 Joomla 3.0 and higher.

The third line begins our HTML document and describes which language the website is in. An 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.

Head[edit]

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

The first line gets Joomla to put the correct header information in. This includes the page title, meta information as well as system JavaScript. The rest creates links to two system style sheets and to your own style sheet (if it's named template.css and is located in the css folder of your template directory. So if your template is in https://www.mysite.com/templates/my_template/ then the css files will go in https://www.mysite.com/templates/my_template/css/).

Body Section[edit]

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

Amazingly, this will suffice! Yes, it's a 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.

Module Positions[edit]

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.

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.

End[edit]

Finish it off - one last bit:

</html>

Custom Images[edit]

If you want to add any images to the template you can do so like this:

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

Here the template variable will fill in the name of your template.

Custom CSS[edit]

You can add custom CSS like this:

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

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