17487 Posts in 4473 Topics by 1978 members
|Go to End|
21 January 2008 at 3:10pm
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?
21 January 2008 at 4:26pm
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()?
21 January 2008 at 5:26pm
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!
21 January 2008 at 10:16pm
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.
22 January 2008 at 12:50pm
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.
22 January 2008 at 1:06pm
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.
22 January 2008 at 1:38pm
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.
22 January 2008 at 1:45pm
My bad. The virtual property is referred to in the template, as though it were a normal property, and it works all good.
|Go to Top|