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.

Template Questions /

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

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

Go to End

3 Posts   792 Views


Community Member, 166 Posts

28 October 2011 at 4:47pm


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:


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?




Community Member, 288 Posts

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.


Community Member, 166 Posts

28 October 2011 at 11:01pm

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