Jump to:

3401 Posts in 979 Topics by 855 members

Template Questions

SilverStripe Forums » Template Questions » Getting properties and calling methods is pure magic

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

Page: 1
Go to End
Author Topic: 367 Views
  • Urug
    Avatar
    Community Member
    4 Posts

    Getting properties and calling methods is pure magic Link to this post

    Consider following DataObject:

    class News extends DataObject {
    public static $db = Array(
    'Title' => 'Text',
    'Date' => 'Date',
    )

    public function getTitle() {
    return 'MyTitle';
    }

    public function getDate() {
    return date('j.n.',strtotime($this->getField('Date')));
    }
    }

    now in the template:

    <% loop $News %>
    <div class="news">
    <span class="date">$Date</span><a href="">$Title</a>
    </div>
    <% end_loop%>

    The overloaded method for field 'Title' is called corretly and string "MyTitle" is returned. On the other hand overloaded method for field 'Date' isn't called and the default getter is used instead, so the 'Date' si returned in DD-MM-YYYY format. Fortunately it can be fixed by changing $Date to $getDate in template.

    Another similar problem is calling methods with parameters from template. If method is defined:

    public function getSomething($param) {
    return 'Something with'.$param;
    }

    and $Something(Param) is used in template then [Warning] Missing argument 1 for SomeController::getSomething() is thrown. It's quite interesting to me, because it means that the right method is called, but the arguments are not passed. Again it can be fixed by changing $Something(Param) to $getSomething(Param).

    I find this behavior inconsistent and confusing, is there any logic into it or am I missing something?

    367 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.