17488 Posts in 4473 Topics by 1978 members
| Go to End | Next > | |
| Author | Topic: | 2802 Views |
-
Custom Function but parameter is always NULL

12 June 2008 at 7:29pm
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 . '–' . $to;
}
return $to;
} -
Re: Custom Function but parameter is always NULL

12 June 2008 at 7:57pm
You can also do just
2007 - $Now.Year
in the template
-
Re: Custom Function but parameter is always NULL

12 June 2008 at 8:04pm
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 -
Re: Custom Function but parameter is always NULL

12 June 2008 at 8:13pm
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.
-
Re: Custom Function but parameter is always NULL

12 June 2008 at 8:15pm
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
-
Re: Custom Function but parameter is always NULL

12 June 2008 at 8:16pm
dio5: d'oh! Posted same time as you! ;-)
-
Re: Custom Function but parameter is always NULL

12 June 2008 at 8:43pm
Ok thanks.
Can you explain why sometimes I have to use getFooBar and sometime only FooBar is enough? -
Re: Custom Function but parameter is always NULL

12 June 2008 at 10:56pm
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.
| 2802 Views | ||
| Go to Top | Next > |



