Jump to:

23370 Posts in 18145 Topics by 2864 members

General Questions

SilverStripe Forums » General Questions » Parse Menu Titles?

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: 793 Views
  • kylehudson00
    Avatar
    Community Member
    22 Posts

    Parse Menu Titles? Link to this post

    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!

  • Sean
    Avatar
    Forum Moderator
    921 Posts

    Re: Parse Menu Titles? Link to this post

    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

  • kylehudson00
    Avatar
    Community Member
    22 Posts

    Re: Parse Menu Titles? Link to this post

    That worked PERFECTLY, thank you SOOOOOO MUCHH!

  • Sean
    Avatar
    Forum Moderator
    921 Posts

    Re: Parse Menu Titles? Link to this post

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

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