Controlling Joomla! templates depending on menu you use

We have been working on building a multi-school Joomla! website and there have been many hurdles to overcome since Joomla! isn’t a multi-site CMS. After searching for anything that’s common across each school, I realized the menu for each school could be used as a common denominator. If I could set a variable depending on which menu is loaded on the page then I can do just about anything I want, such as change the logo, set a unique CSS class, etc. After meeting with our developers we figured out how to do this and I’ll share it with all of you in case you ever need to do the same.

To start we need to pull in the information of which menus are being used on the page. Place this code in the head of your template:

$menus = JSite::getMenu();
$m = $menus->getActive();

This checks for the active menus on the page and sets it into the variable $m so we can manipulate it. Next, you need to look at the menutype in the administrator Menu Manager (under Type). This is what we will use to determine which school is being viewed. I want to take this information and set a new variable named $school:

if($m->menutype == 'district-information') { $school = 'district-info'; }
if($m->menutype == 'cityname-elementary-school') { $school = 'cityname-elementary'; }
if($m->menutype == 'cityname-middle-school') { $school = 'cityname-middle'; }
if($m->menutype == 'cityname-high-school') { $school = 'cityname-high'; }

The first thing I did was echo the variable $school into an ID on the body tag.

body id="< ?php echo $school; ?>"

This allows me to style the CSS uniquely according to the school being viewed which gives you a lot of style control. To change the logo I can just call it by the school.

#cityname-high .logo {background: url(../images/logo-highschool.jpg);}

Another thing I did was load a unique module position according to the school so I could publish modules in all pages the menu shows up on. I did this by doing the following:

if($school == 'district-info') {
    echo '<jdoc :include type="modules" name="district_module" style="raw" />';
if($school == 'cityname-elementary') {
    echo '<jdoc :include type="modules" name="elementary_module" style="raw" />';
if($school == 'cityname-middle') {
    echo '<jdoc :include type="modules" name="middle_module" style="raw" />';
if($school == 'cityname-high') {
    echo '<jdoc :include type="modules" name="high_module" style="raw" />';

As you can see this can give you the ability to do things you may not have been able to do before in Joomla!. I know it saved me from a major headache!


Sign up today to stay informed with industry news & trends