Jump to:

22979 Posts in 11815 Topics by 2826 members

General Questions

SilverStripe Forums » General Questions » Alternate menu/theme based on page

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

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

Page: 1
Go to End
Author Topic: 1581 Views
  • AlaVive
    Avatar
    Community Member
    42 Posts

    Alternate menu/theme based on page Link to this post

    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?

  • zenmonkey
    Avatar
    Community Member
    524 Posts

    Re: Alternate menu/theme based on page Link to this post

    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.

  • AlaVive
    Avatar
    Community Member
    42 Posts

    Re: Alternate menu/theme based on page Link to this post

    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)!

  • anujkryadav
    Avatar
    Community Member
    30 Posts

    Re: Alternate menu/theme based on page Link to this post

    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

    1581 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.