How to use php code in Joomla template design

If we are designing our own templates in Joomla we are already using some of the php programming language. Apart from including our modules, components, scripts and css files, we can use php code to further extend the website. The following code should be placed in your template index.php file. It was tested on the new Joomla 2.5 beta and Joomla 1.7.3 system.

Without writing too much we will get our hands dirty right away, with the code bellow. Also note that the code was tested on the Joomla 1.7.3 multilingual setup, so it should also work in Joomla 2.5 multilingual.

Application object line

//With this line we include Joomla application specific objects
<?php
  $app = JFactory::getApplication();
?>

We should include the code at the beginning of our index.php file. In most of the default templates the line is already included.

Detecting the front page and executing the code

//Showing HTML code on the front page only
<?php
  $menu = JSite::getMenu();
  if ($menu->getActive() == $menu->getDefault()) {
    echo "We can put HTML code here";
  }
?>
//Displaying different information for different languages
<?php
  $menu = JSite::getMenu();
  if ($menu->getActive() == $menu->getDefault( "en-GB" )) {
    echo "This is the front page in English language";
  }
  elseif ($menu->getActive() == $menu->getDefault( "sl-SI" )) {
    echo "This is the front page in Slovenian language";
  }
?>
//Displaying information for the pages with the language setting to All
<?php
  $menu = JSite::getMenu(); ?>
  $lang = JFactory::getLanguage();
  if ($menu->getActive() == $menu->getDefault($lang->getTag())) {

  }
?>

You can read more about front-page detection from the Joomla docs.

//This will execute some code on the front page and another code on all other pages
<?php
  $menu = & JSite::getMenu();
  if ($menu->getActive() == $menu->getDefault()) {
    //This code that will be executed on the front page
  } else {
  //This code will be executed on all pages except the home page
  }
?>

Removing breadcrumbs on the front page of unilingual and multilingual web sites

<?php if ($menu->getActive() !== $menu->getDefault()) {
  if($this->countModules("breadcrumbs")) {
    <jdoc:include type="modules" name="breadcrumbs" style="none" />
  }
}
?>

This line of code removes the breadcrumbs on the front page and on sites that are unilingual only!

<?php if ($menu->getActive() !== $menu->getDefault( "en-GB" ) && $menu->getActive() !== $menu->getDefault( "sl-SI" )) {
  if($this->countModules("breadcrumbs")) {
    <jdoc:include type="modules" name="breadcrumbs" style="none" />
  }
}
?>

My site is served in two languages English and Slovenian. Change the language strings to your languages and you can remove the breadcrumbs module from multilingual joomla front page.

Inserting a link to the homepage

<?php echo $this->baseurl ?>

We most often put this code in the href parameter of a link.

Inserting the name of the site from the sitename variable

<?php echo $app->getCfg("sitename"); ?>

We can use the code to show the name of the site, that we have set in the Joomla backend.

Inserting a date

<?php echo date("d/m/Y"); ?>

Most often used in the footer section of a page for copyright.

Including a module position in the template

<?php if($this->countModules("nav")) {
  <jdoc:include type="modules" name="nav" style="xhtml"/>
}

Conditional statements help us to show modules in different position on a page. If there is no module in that position, the code is not present in the output. We have to be careful to insert the module position nav to the templateDetails.xml file, or else we cannot choose the position in the backend.

Detecting different views and components and executing the code

<?php if (JRequest::getVar("view")=="featured") { ?>
//HTML code that will show only on the featured page
<?php } ?>

Joomla! Developers recommend the use of $menu->getActive and $menu->getDefault in the above examples for detecting the front page.

<?php if (JRequest::getVar("view")=="category") { ?>
//HTML code that will show only on the category page
<?php } ?>
<?php if (JRequest::getVar("view")=="categories" && JRequest::getVar("id")==1) { ?>
//HTML code that will show only on the categories page with the id=1
<?php } ?>
<?php if (JRequest::getVar("option")=="com_content") { ?>
//HTML code that will show only on the com_content components page
<?php } ?>

You can look for the strings for view and option if you turn off Joomla SEF links. Then if you navigate through the site you can see the different components names and views in the URL address. The php conditions can be executed on the current views, e.g., article, category, categories, contact, login or even on the Id value.

Removing mootools in Joomla 2.5 and Joomla 1.7

Include before <jdoc:include type="head" />

//This code prevents the loading of the default scripts in Joomla
<?php
  $this->_scripts = array();
?>

With the above code we prevent the default Joomla scripts from loading. That means all of the default scripts, e.g., mootools.js, caption.js…! If we do not know what we are doing, it is better to not do it at all. Some of the components require the libraries. The code only removes libraries from the frontend of the site.

Removing mootools on all the pages except on the Chronoforms page

<?php if (JRequest::getVar("option")!=="com_chronoforms") { ?>
<?php $this->_scripts = array(); ?>
<?php } ?>

I had troubles on my site with jQuery and mootools conflict. I cannot disable the mootools library on all pages, because I am using the Chronoform contact component, which needs the library. So I decided to exclude the library on all the pages except on the Contact me page. Excluding or including libraries demands a lot of testing before going live. You have to be sure that the end result is what you want and do not forget to look for errors.