3214 Posts in 848 Topics by 810 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 251 Views |
-
SS Black Magic: How does a controller function getMySomething() translate into the template control/variable $MySomething?

28 October 2011 at 4:47pm
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.
-
Re: SS Black Magic: How does a controller function getMySomething() translate into the template control/variable $MySomething?

28 October 2011 at 8:15pm
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.
-
Re: SS Black Magic: How does a controller function getMySomething() translate into the template control/variable $MySomething?

28 October 2011 at 11:01pm
@Smurkas, thank you very much for that detailed explanation.
VWD.
| 251 Views | ||
|
Page:
1
|
Go to Top |


