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.
I was reading this page here: http://doc.silverstripe.com/doku.php?id=datamodel#customizing which said that you can make your own virtual properties.
What I want to do is very similar to what they have listed. They take the firstname and surname and then return it as a title.
My question is, how do you then refer to that virtual property in the .ss template (which I am assuming you can)? $getTitle?
Does the code getTitle() go in the Page or in the Controller?
OK I worked it out. It works well in the Controller, and I just use the function name in the template. eg. $getTitle.
However, say you want to do a small string manipulation within that last function, eg, strip all whitespace, where should this new function exist? In the controller alongside getTitle()?
It is as though once I have worked out the exact wording to communicate what I want to ask, the answer comes to me in about half an hour.
I placed the new function in the controller and referred to it like $this->functionName() and it worked a treat.
Thank you again! :)
It works equally well in the 'model' or 'page' or 'object' and I think most of the time is even better there.
Functions you define in the controller also get called as an action as part of an url.
( urlsegment/action/id/ ).
When you have something like firstName and surname, and you want to return it as a title,
I think it might be better to define it on the object model, rather than in the controller.
just put something like
return $this->FirstName ." ". $this->Surname;
on the eg. Page class (not controller) , and it will work as $FullName in the template.
So functions defined in the controller can refer to functions defined in the page class, and referenced as though they were defined in the controller? So for example: $this->function() ?
So far so good.
Euhm.. :-) probably yes, but most of the time you'll probably won't be needing them in the controller.. Just define them on your 'Page' (or whatever) class and access them in the template.
Hard to see without knowing what you're doing. :-)
I wouldn't put getTitle() like in yr example in the controller class, but in the 'Page' class.
Have shifted it all over to the Page class*. So far, so good. :)
So then what does belong in the controller class?
Also, if a child has a defined virtual property, can it be referred to by a parent, in the template?
<% control Children %> $child.subproperty <% end_control %>
*Not the actual page class but the one I have extended. eg. ArticlePage.
My bad. The virtual property is referred to in the template, as though it were a normal property, and it works all good.