It's Sunday - what a day to visit jfoobar!

Jfoobar Labs

At jfoobar labs you will find "stuff" that is either cool, useful or for learning purposes. We hope to create a pretty nice selection over time. Remember, we need it to be fun.

Snippet - layout switching

  • Type: Code Snippet
  • Compatible: Joomla! 1.5+
  • Last update: 2009-06-19

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');
	}

There are 1 comments posted.

# 1 - Posted by: unleash.it on 2009-11-07 02:59:48

Very cool, I learned a couple new things. But I think it could help a lot of people if you can break it down into a little more detail with some basic descriptions. I know what most of these do (but not sure what default is vs. active... ), but others might not. Also.. if you throw in some user related stuff that would really be handy!

Thx

Help for creating beautiful comments.

Enter Your Details:
Enter Your Comments:
I'm finished with the form Your form will be checked and you'll get a preview.
moovur promo

JFoobar friends on Twitter

Follow JFoobar on twitter

Sponsored Links

Latest Comments

Aaron wrote:
2009-12-23 13:19:22 - Genius! Thanks, Wilco. I've been dying to take .
Posted in How to downlo .
Amy Stephen wrote:
2009-12-22 18:39:37 - Happy Birthday to one of Joomla!'s most noble - .
Posted in Mister Joomla .
Antonie de Wilde wrote:
2009-12-22 09:30:26 - Congrats Robin. Have a good day and watch out w .
Posted in Mister Joomla .
Robert wrote:
2009-12-22 08:51:02 - Happy Birthday Robin .
Posted in Mister Joomla .
Arno wrote:
2009-12-22 08:43:28 - Happy Birthday Robin, love your suit, you wife .
Posted in Mister Joomla .
Brian Teeman wrote:
2009-12-22 00:17:41 - Happy Birthday Robin, Welcome to the big four oh .
Posted in Mister Joomla .