Jump to:

23460 Posts in 18907 Topics by 2877 members

General Questions

SilverStripe Forums » General Questions » Fun with menus

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 2
Go to End
Author Topic: 1383 Views
  • Junglefish
    Avatar
    Community Member
    104 Posts

    Fun with menus Link to this post

    Okay, imagine a basic SS site layout, two-level menu down the left, some content in the middle, and a side bar on the right.

    What I'd like to do for one of the first-level menu items is prevent it appearing in the menu structure on the left, and instead build it into the side bar on the right.

    My first thought was a simple hack inside <% control Menu(1) %>, something like:

    <% if $MenuTitle.XML != 'My Headline' %>
    show the item
    <% end_if %>

    and then in the side bar on the right doing the exact opposite, so that ONLY the node I want, plus its children appear.

    But this causes a parsing error - I'm probably breaking a whole load of SS syntax rules that I know nothing of.

    So, can anybody suggest the *correct* way of doing this? Either doing it the *proper* SS way, or by cleaning up my hack?

    jf\

  • Pigeon
    Avatar
    Community Member
    243 Posts

    Re: Fun with menus Link to this post

    The 'SS' way of doing it would be to add a field to Page.php that was 'ShowOnlyInSideMenu' (or whatever) that could then be a check box in the cms. if it is checked then you can do a simple if statment in the template!

    If you don't want it showing in the CMS, just dont add the checkbox to it!

  • Bruce B
    Avatar
    Community Member
    145 Posts

    Re: Fun with menus Link to this post

    I generally don't want my home page included on the side menu because I handle it differently to other pages. I just uncheck 'show in menu' in the CMS. The right menu would then call a specific page control. You could have extra checkboxes in the CMS but if the intention is to hand the site over to a client, that sounds too much like a problem waiting to happen - better to hard code it.

  • Junglefish
    Avatar
    Community Member
    104 Posts

    Re: Fun with menus Link to this post

    @Pigeon Thanks for the suggestion. Can I get a bit more detail?

    I set up a new field in Page.php, let's call it "MoveItemFromLeftMenuToRightMenu". It might be a checkbox exposed in the CMS or I might keep it hidden away from the client.

    Once that's done, how do I test for it inside the <% control Menu(1) %> loop?

    I imagine it's something like <% if MoveItemFromLeftMenuToRightMenu = 1 %>don't show the item<% end_if %>, but what is the correct syntax? Or is there a more elegant way of testing for its presence?

    jf\

  • Junglefish
    Avatar
    Community Member
    104 Posts

    Re: Fun with menus Link to this post

    @ bruceb, Thanks also for your suggestion.

    So, I uncheck 'show in menu', no problem.

    However, I'm not sure about how I could then set up a 'specific page control' to grab that specific node and its child nodes. Could you explain a little further please?

    Thanks,

    jf\

  • Pigeon
    Avatar
    Community Member
    243 Posts

    Re: Fun with menus Link to this post

    Junglefish,

    This is how i would do it:

    class Page extends SiteTree {
    ...
    public static $db = array(
    "MoveItemFromLeftMenuToRightMenu" => "Boolean"
    );

    ...
    function getCMSFields() {
    $fields = parent::getCMSFields();
    ...
    $fields->addFieldToTab('Root.Behaviour', new CheckboxField('MoveItemFromLeftMenuToRightMenu', 'Show in right menu?'),'ShowInSearch');
    ...
    return $fields;
    }
    ...
    }

    class Page_Controller extends ContentController {
    ...
    function RightMenu() {
    return DataObject::get('Page','`MoveItemFromLeftMenuToRightMenu` = 1');
    }
    ...
    }

    Then, in the template:

    ...
    <% control RightMenu %>
    <li><a href="$Link" title="Go to the $Title.XML page" class="$LinkingMode">$MenuTitle.XML</a></li>
    <% end_control %>
    ...

    This means, if a user checks the 'show in right menu' or whatever, then it shows in that menu. To stop it appearing in both the user would have to uncheck the 'show in menus'. If you only wanted it to show in one or the other, then you can modify the Menu() function so it excludes items with MoveItemFromLeftMenuToRightMenu and then use this:

    function RightMenu() {
    return DataObject::get('Page','`MoveItemFromLeftMenuToRightMenu` = 1 AND `ShowInMenus` = 1');
    }

    PS: You don't have to give the user a check-box of course, you could code in the value, BUT then what about when they make new pages?

  • Bruce B
    Avatar
    Community Member
    145 Posts

    Re: Fun with menus Link to this post

    Pigeon's reply is much more elegant than what I was suggesting but you will definitely need to document it clearly for the client.

    General info on using controls is here:
    http://doc.silverstripe.org/doku.php?id=built-in-page-controls

  • martimiz
    Avatar
    Forum Moderator
    1078 Posts

    Re: Fun with menus Link to this post

    There's lots of good solutions suggested here but I'd like to add that you could still do something like your first try. Only: the <% if %> structure doesn't (yet?) seem to support the != operator. A workaround could be:

    <% if MenuTitle = 'My Headline' %><% else %> [your code goes here] <% end_if %>

    I think this might also be a 'Silverstripe way' as the template gets to decide what goes where in the display...

    1383 Views
Page: 1 2
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.