tags:

views:

157

answers:

2

Folks, I am trying to implement a rudimentary feature in Joomla but having no luck getting my head around it.

My client has setup Joomla with several sections; each section having its own categories and eventually content underneath.

I need each section to have a slightly different color component (e.g. Section A and its all subsequent child pages red, Section B - blue, etc); certain borders and backgrounds need to be unique according to each section.

I have one theme which is used by all sections. Somewhere in the theme file, I need to detect which section I am on, and based on that set a css variable accordingly:

<html>
    <body class="cars-section">

    </body>
</html>

All I need is to set my body's class to the right section, and all my coloring has been setup to work magically.

Any ideas how this can be done in the Joomla world? Is there another way of doing such a thing.

A: 

There are a couple of ways to achieve body css-classing:

  1. Utilize Joomla's menu page class suffix system.
  2. Output the class based on the selected menu link from within the template. Of course, if you plan to do this, you'll need to modify your template a bit.

    $menu = &JSite::getMenu();
    $active = $menu->getActive();
    <body <?php if($active->alias) echo 'class="' .$active->alias .'"' ?>>

Eddy
This will get me the alias of the current page. This will work fine if my section had only one page.However, a section can have a hierarchy of child pages. What if someone clicks on the deepest child page. Alias will return the alias of that child page, rather than inform me the name of the root section.
Hady
+1  A: 

You need to pick the section ID up from the request.

Use this to get the relevant request variables:

<?php
$option = JRequest::getWord('option', null);
$view = JRequest::getWord('view', null);
$idalias = JRequest::getVar('id', null);
if (strpos($idalias, ":") != false) {
    $idandalias = explode(":", $idalias);
    $id = $idandalias[0];
} else {
    $id = JRequest::getInt ('id' , 0);
}

Then use something like this to see what section you are in, if you are on a section page:

if ( $option=="com_content" && $view=="section" ) {
  $sectid = $id;
}

In section pages you can just use the request, but in other pages you need to do a database query as well:

else {
  $database =& JFactory::getDBO();
  if ( $option=="com_content" && $view=="category" ) {
    $query = "SELECT section FROM jos_categories WHERE id=$id";
  }
  if ( $option=="com_content" && $view=="article" } {
    $query = "SELECT sectionid FROM jos_content WHERE id=$id";
  }
  $database->setQuery($query);
  $sectid = $database->loadResult();
}

When you have the section ID you can use it to set and insert the right class.

if ( $sectid == '3' ) {
  $my_cars_section_class = 'three';
}
?>
<body class="<?php echo $my_cars_section_class; ?>">

Something like that should do it.

E Wierda