Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Alternate menu/theme based on page


Go to End


4 Posts   2603 Views

Avatar
AlaVive

Community Member, 42 Posts

28 September 2009 at 5:38am

I'm working on a large site that has three different menus and three different themes to be displayed based on the section of the site being visited. Ideally, I wouldn't have to maintain three different templates (especially as I continue to receive changes to the layout while I'm trying to build the CMS for it).

Provided I can solve the issue of switching menus based on the site section, the themes are similar enough that I can allow the user to select the proper CSS for a page within the CMS (according to http://www.silverstripe.org/archive/show/60032#post60032).

I've created the different menus and have them displaying correctly when hard-coded into the template, and I've managed to get them to display according to the following test:

  <% if URLSegment == chickens %> 
	<% include ChickenNavigation %>
	<% else %> 
	<% include Navigation %>
   <% end_if %>

The obvious problem is the utter lack of flexibility for creating new pages that display the proper menu.

It would be fine to say, "If you want to display the chickens menu, you have to add 'chickens' somewhere in the page title," or "Select the menu you want to display from the CMS." But, I can't figure out how to modify any effort I've yet put forth to offer either of those capabilities ;-).

What's the better way to handle this?

Avatar
zenmonkey

Community Member, 545 Posts

28 September 2009 at 5:57am

What about adding a drop down field to the CMS with your different Menu Types then create functions that check against that selection in your Page_Controller.

function CheckChickens() {
  if ($this->MenuType == "Chickens") {
    return true;
  }
  else { return false; }
}

That way in you template you would do:
<% if CheckChickens %>
   <% include ChickenNavigation %>
<% elseif CheckOther %>
  <% include OtherNavigation %>
<% elseif etc %>
<% end_if %>

You could also create these sort of Check controllers against other parameters including Parent pages etc. I use the same sort of thing to display images certain class of image on my website. If the page itself doesn't specify this particular type of image it uses the image from the parent page, and if the parent page doesn't have this sort of image it reverts to a default.

Avatar
AlaVive

Community Member, 42 Posts

28 September 2009 at 6:58am

Genius.

It works like a dream. Furthermore, I actually set the function to check against the PageStyle drop down, which combines both the CSS and menu modifications in one.

Thank you (again)!

Avatar
anujkryadav

Community Member, 30 Posts

8 January 2010 at 2:21am

hello AlaVive
i too have the same requirement,can you please explain me the way to create different menu ...if possible do post the code and the pages where i need towrite the code

thank you in advance