Jump to:

3439 Posts in 1028 Topics by 869 members

Template Questions

SilverStripe Forums » Template Questions » SS Black Magic: How does a controller function getMySomething() translate into the template control/variable $MySomething?

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

Page: 1
Go to End
Author Topic: 382 Views
  • vwd
    Avatar
    Community Member
    158 Posts

    SS Black Magic: How does a controller function getMySomething() translate into the template control/variable $MySomething? Link to this post

    Hi,

    I guess I'm just curious about what goes on in the background. I'm continually amazed at what you can do and how you can do things with SilverStripe...

    One of those things is when a 'getter' function is declared such as:

    <?php

    class MySomethingController extends Controller{

    function index($url) {

    return array();
    }

       function getMySomething() {
          return "Some Random Text";
       }

       function GetMySomethings() {
          return "Some More Random Text";
       }
    }
    ?>

    ... it translates to a template variable/control which can be used like...

    Here is the output of getMySomething: $MySomething...

    And a little bit more of GetMySomethings: $MySomethings...

    And even these work: $getMySomething and $GetMySomethings...

    So what exactly gets exposed to templates? Every public function in a controller? And are getters ("getXXXX()" and "GetXXXX()") the only functions that can be used with the prefixes (ie "get" | "Get") stripped?

    Thanks...

    VWD.

  • MarcusDalgren
    Avatar
    Community Member
    288 Posts

    Re: SS Black Magic: How does a controller function getMySomething() translate into the template control/variable $MySomething? Link to this post

    If you go up the chain of inheritance you'll find the __call() method which does all this magic stuff. As you said you can use getMySomething and get is stripped. However I really don't recommend using that since you lose the ability to use arguments in your template functions.

    function getMySomething($var) {
    ...
    }

    The code above will never work in a template but if you remove get then it will. Some of the image functions also get prefixes stripped I think but it was some time since I read the code so i don't remember it all.

    As you've figured out every public method in a controller gets exposed to the template and also all public methods in the model. The controller uses the model as a failover so whenever a method isn't found in the controller it gets sent over to the model.

  • vwd
    Avatar
    Community Member
    158 Posts

    Re: SS Black Magic: How does a controller function getMySomething() translate into the template control/variable $MySomething? Link to this post

    @Smurkas, thank you very much for that detailed explanation.

    VWD.

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