More PHP code snippets to be used in your template

In a previous article about useful  PHP code snippets to conditionally add things to your template I allready listed couple of tricks, let's add a couple more to them!

Application object line

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

For most of tricks below to work, we should include the code at the beginning of our template's index.php file. In most of the well written templates the line is already included.

Do you want to get the active menu item?

$menu    = &JSite::getMenu();
$active  = $menu->getActive();
$menuName = $active->name;
$menuLink = $active->link;

Getting the SEF prefix in a multilanguage site

jimport('joomla.language.helper');
$languages = JLanguageHelper::getLanguages('lang_code');
$lang_code = JFactory::getLanguage()->getTag();
$sef = $languages[$lang_code]->sef;

Detecting a given component - com_component in our case

<?php if (JRequest::getVar('option')=='com_component') { ?>
    //HTML code that will show only on the com_component components pages
    <?php } ?>

Detecting a given view - for example view='category'

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

Detecting a given view AND id - for example view='categories'&id='1'

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

An evergreen: Inserting a date

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

Inserting the name of the site from the sitename variable

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

Inserting a link to the homepage

<?php echo $this->baseurl ?>

Removing default script libraries (as mootools) in Joomla 1.7+

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

<?php //This code prevents the loading of the default scripts in Joomla
    $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 and alike. If you don't know what you are doing, it is better to not do this at all. Some of the components require these libraries. The code only removes libraries from the frontend of the site. In the component specific pages the scripts might be loaded by the used component and the modules themselves! Use with care!

Removing default Joomla scripts - as mootools - from all the pages except on MyComponent pages

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