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.

Archive /

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

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

Extending the menu - need some help with the API


Go to End


5 Posts   2729 Views

Avatar
siulun

14 Posts

11 October 2007 at 9:42am

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!

Avatar
Willr

Forum Moderator, 5523 Posts

11 October 2007 at 2:42pm

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

Avatar
Sean

Forum Moderator, 922 Posts

11 October 2007 at 6:37pm

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

Avatar
siulun

14 Posts

12 October 2007 at 7:33am

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!

Avatar
Willr

Forum Moderator, 5523 Posts

12 October 2007 at 3:42pm

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 :P you would then need to create the page and change ID manually editing it in the database and who knows what that could break :D

Glad $ID fixed what you are trying to do!