Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Extending the menu - need some help with the API

Our old forums are still available as a read-only archive.

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

Page: 1
Go to End
Author Topic: 2050 Views
  • siulun
    Avatar
    14 Posts

    Extending the menu - need some help with the API Link to this post

    Hi,

    Trying to extend the menu control output a little by grabbing the ID of the page link from the database and using it as a style class.

    The reason for this is I need a unique style class identifier for each of the links so I can style each one up individually different.

    I've been through all the tutorials, read all the APIs, but I still don't even know whether there is an ID assigned for each page at the moment and if there is, how it could be accessed and extended!

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Extending the menu - need some help with the API Link to this post

    yes each page has an ID attached to it. If you need a special ID for each menu item I use id="$URLSegment" so that will come up with an id of "home" or "contact-us". You can also you id="$ID" in your template and that will generate id="3" for whatever link but 3 is not as semantic and easy to understand as 'home'. The only problem with this method is that if you can the page url you will need to update the id name in the css

  • Sean
    Avatar
    Forum Moderator
    921 Posts

    Re: Extending the menu - need some help with the API Link to this post

    You could also use something like <li class="item-$Pos"> which outputs <li class="item-1"> etc, dependent on what position it's in from the DataObjectSet you generated it from, typically using <% control Menu(1) %> or Menu(2).

    $URLSegment is a good semantic way to use it. However, it's volatile as users could change the page name, which in turn updates the URL segment, as Will said.

    Cheers,
    Sean

  • siulun
    Avatar
    14 Posts

    Re: Extending the menu - need some help with the API Link to this post

    Odd, I swear I tried $ID yesterday but it didn't work (I put it outside all the tags but within the control section...) , today I put it in properly as id="m$ID" within the tag and it worked like a charm! Great. Thanks guys.

    I would advice though not to use URL segment for future reference because
    a) id will become too long, for large sites this means a couple of kb more per view
    b) as you said, url segment could change where as the ID won't

    Thanks again!

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Extending the menu - need some help with the API Link to this post

    b) as you said, url segment could change where as the ID won't

    as long as you don't delete the page it will be fine you would then need to create the page and change ID manually editing it in the database and who knows what that could break

    Glad $ID fixed what you are trying to do!

    2050 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.