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

Parse Menu Titles?


Go to End


4 Posts   1242 Views

Avatar
kylehudson00

Community Member, 22 Posts

13 October 2010 at 8:09pm

Hi All!

This probably sounds like a stupid question, but I can't quite figure it out. Within a <% control Menu(1) %> control block, I need to parse each menu title, replacing any white spaces with non-breaking spaces (&nbsp;), and then return the result. I wrote a function in my page controller which uses PHP's str_replace method to do the job, BUT I can't figure out how to pass the current menu title to the function? Hope this makes sense. Any help or suggestions would be most appreciated! :)

Avatar
Sean

Forum Moderator, 922 Posts

13 October 2010 at 8:43pm

Edited: 13/10/2010 9:04pm

You don't need to create parameters to the method you just created on the controller, just create a method on the model class (Page) which parses the "Title" field from the database.

For example, in your Page model class (not Page_Controller):

<?php
class Page extends SiteTree {
...

function CleanTitle() {
   return str_replace('&nbsp;', '', $this->Title);
}

...

class Page_Controller extends ContentController {
...

Then you would simply use $CleanTitle inside the <% control Menu(1) %> block. This control loop iterates over Page instances, so you'll have access to any of the Page model fields and methods, including your newly created "CleanTitle".

Hope that explains it.

Cheers,
Sean

Avatar
kylehudson00

Community Member, 22 Posts

13 October 2010 at 9:09pm

That worked PERFECTLY, thank you SOOOOOO MUCHH! :):)

Avatar
Sean

Forum Moderator, 922 Posts

13 October 2010 at 9:13pm

Edited: 13/10/2010 9:13pm

No problem, glad to see it's working for you. :-)