Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Custom Function but parameter is always NULL

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

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

Page: 1 2
Go to End
Author Topic: 3029 Views
  • ElBarto
    Avatar
    Community Member
    3 Posts

    Custom Function but parameter is always NULL Link to this post

    I want to have a function that generates me a copyright year range.
    I call it in the footer like this: $CopyrightRange(2007)
    Then it should print "2007-2008"
    But the parameter is always NULL. I checked this with the var_dump you can see below.
    If have tested this function in the Page class and in the Page_Controller class but it doesn't work at all.
    What goes wrong here??

    public function getCopyrightRange($from) {
    $to = date('Y');
    var_dump($from);
    if(!empty($from) && $from < $to) {
    return $from . '&ndash;' . $to;
    }
    return $to;
    }

  • dio5
    Avatar
    Community Member
    501 Posts

    Re: Custom Function but parameter is always NULL Link to this post

    You can also do just

    2007 - $Now.Year

    in the template

  • ElBarto
    Avatar
    Community Member
    3 Posts

    Re: Custom Function but parameter is always NULL Link to this post

    I could but that's not what I want.
    Ich want do write $CopyrightRange(2008)
    and in 2009 in should then automatically print 2008-2009
    but this year it should print only 2008

  • dio5
    Avatar
    Community Member
    501 Posts

    Re: Custom Function but parameter is always NULL Link to this post

    the problem is the 'get' in getCopyrightRange($from)

    Either add the 'get' in the template, or get rid of it in the controller/model.. whereever you've put it.

  • Sean
    Avatar
    Forum Moderator
    921 Posts

    Re: Custom Function but parameter is always NULL Link to this post

    This is caused by a deficiency in the templating language.

    You either have to:

    1. Change getCopyrightRange() to CopyrightRange() in your PHP code (removing the "get" prefix)
    2. OR, you can call $getCopyrightRange(2007)

    Sean

  • Sean
    Avatar
    Forum Moderator
    921 Posts

    Re: Custom Function but parameter is always NULL Link to this post

    dio5: d'oh! Posted same time as you! ;-)

  • ElBarto
    Avatar
    Community Member
    3 Posts

    Re: Custom Function but parameter is always NULL Link to this post

    Ok thanks.
    Can you explain why sometimes I have to use getFooBar and sometime only FooBar is enough?

  • Sean
    Avatar
    Forum Moderator
    921 Posts

    Re: Custom Function but parameter is always NULL Link to this post

    It only happens when there's an argument to a function call.

    For example:

    function getPage($id) {
    return DataObject::get_by_id('Page', (int)$id);
    }

    Using $Page(5) in the template will call ->getPage() and not pass in the parameter, which will cause a problem if you're assumption is a parameter that should always be there.

    However, calling $getPage(5) in the template will call ->getPage(5) passing in the parameter correctly.

    Perhaps it is indeed a bug, but it doesn't appear to be a major one, as it's easily worked around in the templates.

    3029 Views
Page: 1 2
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.