Joomla 2.5.6 Template layout control with php code

Submitted by Anonymous (not verified) on Wed, 12/12/2012 - 22:14

1. Count the modules

$this->countModules()

<?php if($this->countModules('left')) { ?>
<div id="left"><jdoc:include type="modules" name="left" /></div>
<?php } ?>

 

Operators

 

 

2. Checking for the frontpage

JRequest::getCmd('view')
<?php if(JRequest::getCmd('view') == 'article') { ?>

 

3. Checking for another component

JRequest::getCmd('option') 

<?php if(JRequest::getCmd('option') == 'com_kunena') { ?>


 

4. Both component and layout

<?php if(JRequest::getCmd('option') == 'com_content'
&& JRequest::getCmd('view') == 'frontpage') { ?>

 

 

5.Checking for the actual homepage

$menu = JSite::getMenu();
$active = $menu->getActive();
$home = (boolean)$active->home;

 

6.Checking for a specific module

jimport('joomla.application.module.helper');
$instance = JModuleHelper::getModule('mod_login');
$login_module = (is_object($instance)) ? true : false;

 

7 . front page detection:

 

<?php $menu = & JSite::getMenu();
if ($menu->getActive() == $menu->getDefault()) {
echo JHTML::script('mobile.js',' ');
} ?>

 

8. front page detection details

 

 

Front page Checking?

Basic code

<?php $menu = & JSite::getMenu(); if ($menu->getActive() == $menu->getDefault()) { echo 'This is the front page'; } ?>

 

For multi-language sites use this code

Show different content based on selected language

<?php $app = JFactory::getApplication(); $menu = $app->getMenu(); if ($menu->getActive() == $menu->getDefault( 'en-GB' )) { echo 'This is the front page'; } elseif ($menu->getActive() == $menu->getDefault( 'fr-FR' )) { echo 'Il s'agit de la premi?re page'; } ?>  

Call to a member function getActive()

You forgot to define $menu, add

$menu = & JSite::getMenu();    

For Joomla 1.5 :use these code.

 

<?php if (JRequest::getCmd('view') == 'frontpage') { // do something} ?>

 

Some templates may have defined function inside ja_templatetools.php, for example JA-purity has

<?php function isFrontPage(){ return (JRequest::getCmd( 'view' ) == 'frontpage') ; } ?>  

so in this case can know, whether it’s Frontpage, by using code:

<?php if ($tmpTools->isFrontPage()) { // do something} ?>

 

 

In Joomla 1.7, joomla 2.5, joomla 2.5.7 version, you can use the same code, except there is no view ‘frontpage’.

 

You can see, what view does Frontpage has, by inserting

 

<?php echo JRequest::getCmd( 'view'); ?>

 

after body tag in template’s index.php file. Refresh browser and somewhere in top of website you will see name of view.

If frontpage is article or last articles from some category, you better get

 

$menu->getActive()

 

to work. Because in this case getCmd(‘view’) will return same view also in articles/category (not frontpage).

This option really should be used as last resort, because it won’t allow much flexibility.

     

Examples

Add

<?php $menu = & JSite::getMenu(); if ($menu->getActive() == $menu->getDefault()) { $frontpage="yes"; } ?>

 

to beginning of template’s index.php file

Every time, you want to check if user is on frontpage, use

 

<?php if ($frontpage) {//do something} ?>

 

Show text on

<?php if ($frontpage) {echo This is frontpage} ?>

 

Show module

<?php if ($frontpage) {echo '<jdoc:include type="modules" name="modulename" style="xhtml"/>';} ?>

 

Hide module

<?php if (!$frontpage) {echo '<jdoc:include type="modules" name="modulename" style="xhtml"/>'} ?>

only difference from previous example is little exclamation mark in if statement

 

Show long HTML code

<?php if ($frontpage): ?> <div id="box1"><div>Content</div></div> <div><div>Content </div></div> <div id="box2">There can be anything</div> <?php  endif; ?>

 

Add CSS class

<body<?php if (JRequest::getCmd('view') == frontpage)  {echo 'class="classname" '} ?>

Then of course define body.classname {} in your CSS file
 

Include Javascript
Paste this between head tags

<?php if ($frontpage") {echo '<script type="text/javascript" src="pathtoscripts/jquery.js"></script>';} ?>

 

As you see, after recognizing Frontpage, usage is unlimited. You can literally turn homepage’s design upside down, including or excluding specific content and applying CSS styles. Paraphrasing post’s title, If Joomla Front page (homepage), do anything.

Related Items