Building a splitmenu with Yireo Template Helper
Building a splitmenu with Joomla! modules is good for many cases, but it has a few downsides: For instance, if the submenu is empty, the menu-module will still display. Also, the title of the submenu-module is static, while you might want to display the title of the parent menu-item instead. With our Yireo Template Helper these things are made simple.
Using the Yireo Template Helper Yth
Yireo has created a simple PHP-class Yth (short for: Yireo Template Helper) to quickly add advanced PHP-logic to Joomla! templates. A splitmenu is part of this functionality. The Yth-class is GPL-ed, meaning you can take a peak at the source code and do the same thing in your PHP-code. Or you could use the following guidelines to ad Yth to your own template.
Downloading and including the Yth-class
The Yireo Template Helper is available for free on the product-page. Once downloaded, put the file yth.php inside your own template-directory. Within your index.php file you can include this new PHP-script with the following code:
include_once 'yth.php';
Once included you can use all the functions contained within the Yth-class.
Getting the splitmenu content
To get the menu-structure you need two calls to the Yth-class. The systemname of the menu is important here, as example we are using "mainmenu":
$topmenu = Yth::getSplitMenu( 'mainmenu', 0, 1 ); $submenu = Yth::getSplitMenu( 'mainmenu', 1, 9 );
Now we also want to fetch the title of the parent of the current submenu:
$submenu_title = Yth::getActiveParent();
Filling in the HTML
Now that the variables contain all the menu-content, we can use these variables to print the menus. Instead of using JDoc-tags in our HTML-code, we just add the following PHP-code:
<div id="topmenu"><?php echo $topmenu; ?></div>
And for the submenu:
<div id="left"> <div class="moduletable"> <h3><?php echo $submenu_title; ?></h3> <?php echo $submenu; ?> </div> </div>
Checking if the menu is empty
Now that we have everything in place, there's one more issue to solve: If the submenu is empty, we actually do not want to display either the containing div or the submenu-title. We use an extra if-statement for this:
<div id="left"> <?php if(!empty($submenu)) { ?> <div class="moduletable"> <h3><?php echo $submenu_title; ?></h3> <?php echo $submenu; ?> </div> <?php } ?> </div>
That's it.
Created on Saturday, 27 February 2010Modified on Friday, 11 November 2011
More tutorials in this section
- Force IE8 to use IE7 Compatibility Mode
- Browser detection in PHP
- Custom Joomla! template layouts with PHP-code
- Building a splitmenu with Yireo Template Helper
- Crunching Joomla! CSS with the Yireo Template Helper
- Disabling CSS in Joomla! extensions
- Modules on the Joomla! offline page
- Writing your own Joomla! splitmenu
- Joomla! output override of mod_mainmenu
