Jump to:

22977 Posts in 11742 Topics by 2826 members

General Questions

SilverStripe Forums » General Questions » Passing variables from controller to template

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: 2610 Views
  • stinkytofu
    Avatar
    Community Member
    17 Posts

    Passing variables from controller to template Link to this post

    I feel really stupid asking this question, but if I want to make some changes to an existing variable in the controller before it is rendered in the template, such as $Content or $Title or a custom field such as $AuthorName for example, how can I do this? I just can't seem to find instructions on how to do this in the tutorials or the forums. Can someone point me to the right page that explains how to accomplish this?

    If someone tells me to RTFM, I'm really sorry that I'm such an idiot, but I just cannot find any information about this anywhere on Google or the Silverstripe tutorials/forums, and I tried really really hard. So would very much appreciate it if you can point me to the right URL.

    Thanks!

  • simon_w
    Avatar
    Forum Moderator
    467 Posts

    Re: Passing variables from controller to template Link to this post

    Just create a method with the same name as the variable that you want to use, ie:

    function AuthorName() {
    return $this->Author()->Name;
    }

  • Kleinforstkoenig
    Avatar
    Community Member
    3 Posts

    Re: Passing variables from controller to template Link to this post

    Hi,

    i think i have the same problem as stinkytofu,
    and im not that familiar with silverstripe and the mvc framework.

    I want to check $MenuTitle string-length to define another Menu CSS class before its been replaced by its Content.
    I want to try it by creating a new php-method into my pagetype controller, but i i dont know how to assign $MenuTitle to this funcion

    does anybody got a solution for this problem?

    greetings from germany.
    Sebastian

  • simon_w
    Avatar
    Forum Moderator
    467 Posts

    Re: Passing variables from controller to template Link to this post

    function MenuTitleLength() {
    return strlen($this->MenuTitle) > length;
    }

    Then you can use <% if MenuTitleLength %>...<% end_if %> in your template.

    The comparison must be done in the controller as the template language doesn't allow inequalities.

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