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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Archive /

Our old forums are still available as a read-only archive.

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

Virtual Properties


Go to End


8 Posts   1962 Views

Avatar
KatB

Community Member, 105 Posts

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?

Avatar
KatB

Community Member, 105 Posts

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()?

Avatar
KatB

Community Member, 105 Posts

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.

Thank you!!

I placed the new function in the controller and referred to it like $this->functionName() and it worked a treat.

Thank you again! :)

Avatar
dio5

Community Member, 501 Posts

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

function FullName()
{
return $this->FirstName ." ". $this->Surname;
}

on the eg. Page class (not controller) , and it will work as $FullName in the template.

Avatar
KatB

Community Member, 105 Posts

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.

Avatar
dio5

Community Member, 501 Posts

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.

Avatar
KatB

Community Member, 105 Posts

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?

eg.
<% control Children %> $child.subproperty <% end_control %>

?

*Not the actual page class but the one I have extended. eg. ArticlePage.

Avatar
KatB

Community Member, 105 Posts

22 January 2008 at 1:45pm

SORRY!

My bad. The virtual property is referred to in the template, as though it were a normal property, and it works all good.

Thank you!:)