Monday, October 18, 2010

Joomla Snippet - layout switching

Here's some code you can use in your template index file to switch between different layouts based on the menu tree -> menu item ID. You can do all kinds of funky stuff with this.


You can put layout files in templates/your_template/layouts. The code you can paste in your templates index.php file.
// Get menu vars for switching between layouts
$menus  =& JMenu::getInstance('site'); // Full menu object
$active  = $menus->getActive();   // Active id
$default  = $menus->getDefault();  // Default id
$mitem   = $menus->getItem($active->id); // Active menu item object
$mtree  = $mitem->tree[0];   // Tree start id
$mparent = $mitem->parent;   // Parent id

// Requests
$option  = JRequest::getCmd('option');
$view   = JRequest::getCmd('view');
$layout  = JRequest::getCmd('layout');
$secid   = JRequest::getCmd('secid');
$catid   = JRequest::getCmd('catid');
$itemid  = JRequest::getCmd('Itemid');

 /*
 * Check if the page requires a special layout
 */
 if ($active->id == $default->id) {
 /*
 * Include home layout if the default ID == The active ID
 */
 require( dirname( __FILE__ ).DS.'layouts'.DS.'home.php');
 } else {
 /*
 * Include the default layout if the default ID != The active ID
 */
 require( dirname( __FILE__ ).DS.'layouts'.DS.'page_default.php');
 }


No comments:

Post a Comment