How to create right structure of the index.php file in your template
1. How to create right structure of the
index.php file in your template
Some practical pieces of advice
2. Today’s publication is devoted to the index.php file.
We will suggest how to organize its structure.
3. Index.php is a main file of your template. It contains of basic
HTML markup and some PHP code.
Here we will try to describe main things that you have to know
about how to do good foundation of your new template.
First of all you have to forbid direct access to this file:
// no direct access
defined( ‘_JEXEC’ ) or die( ‘Restricted access’ );
It is always good to set some variables before HTML code.
Define path to your template so you could use it further:
$tpath = $this->baseurl.’/templates/’.$this->template;
Init application object:
$app = JFactory::getApplication();
And get template parameters:
$params = $app->getParams();
4. Usually you want to include some CSS and JS files for your
template. To do this you have to initialise Document object and
call addStyleSheet and/or addScript methods:
$doc = JFactory::getDocument();
$doc->addStyleSheet($tpath .
‘your_css_folder/your_cool_styles.css’);
$doc->addScrypt($tpath . ‘your_js_folder/megascript.js’);
Next we have to insert common head for HTML files with
DOCTYPE definition:
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0
Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml” xml:lang=”<?php
echo $this->language; ?>” lang=”<?php echo $this->language; ?
>” >
5. After that we can include Joomla head content.
<head>
<jdoc:include type=”head” />
</head>
<jdoc:include />
Main structure makes possible for you to show modules,
component, messages etc. on pages of your template.
Inside <body> tags you can use <jdoc: include/> structure to
show modules, component, messages etc. on pages of your
template.
6. System messages:
<jdoc: include type=”message“/>
Modules, for example search module:
<jdoc:include type=”module” name=”search” />
Search in this case is a position in your template described in
templateDetails.xml file.
For modules you can also define “style” attribute.
<jdoc:include type=”module” name=”leftmenu” style=”xhtml”/>
7. Style may be one of these:
xhtml – each module as separate div element
table – modules will be shown as a table with one column
horz - modules will be also shown as a table but horizontally
each module in own cell
rounded – modules are shown in div elements with a border
and rounded corners
none – module content is shown without title and container
8. Good coding style is to hide markup of inserting modules:
<?php if($this->countModules(‘search’)) : ?>
<div id=”search”>
<jdoc:include type=”modules” name=”search” style=”xhtml” />
</div>
<?php endif; ?>
And of course don’t forget to include component output:
<jdoc:include type=”component” />
Source of the article
Editor's Notes
This presentation was made on the base of the blog post - http://tonytemplates.com/blog/?p=364