Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Virtual Properties

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

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

Page: 1
Go to End
Author Topic: 1480 Views
  • KatB
    Avatar
    Community Member
    94 Posts

    Virtual Properties Link to this post

    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?

  • KatB
    Avatar
    Community Member
    94 Posts

    Re: Virtual Properties Link to this post

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

  • KatB
    Avatar
    Community Member
    94 Posts

    Re: Virtual Properties Link to this post

    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!

  • dio5
    Avatar
    Community Member
    501 Posts

    Re: Virtual Properties Link to this post

    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.

  • KatB
    Avatar
    Community Member
    94 Posts

    Re: Virtual Properties Link to this post

    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.

  • dio5
    Avatar
    Community Member
    501 Posts

    Re: Virtual Properties Link to this post

    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.

  • KatB
    Avatar
    Community Member
    94 Posts

    Re: Virtual Properties Link to this post

    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.

  • KatB
    Avatar
    Community Member
    94 Posts

    Re: Virtual Properties Link to this post

    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!

    1480 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.