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.

General Questions

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Method Calls from Templates Fail


Reply

11 Posts   2290 Views

Avatar
arsenic

15 January 2010 at 2:57pm Community Member, 9 Posts

Just tried. Doesn't work.

Added:

   static function staticFunction() {
      return 'it works';
   }

Apple

<% control getApples %>
$_color // prints red and green
$staticFunction // nothing
<% end_control %>

Is this a bug?

Avatar
CodeGuerrilla

15 January 2010 at 3:06pm (Last edited: 15 January 2010 3:16pm), Community Member, 105 Posts

Try this:

class Apple {

public static $_color;

function getColor()
{
return self::$_color;
}

function setColor($c)
{
self::$_color = $c;
}

}

class Test_Controller extends Controller {
function init() {
parent::init();
}

function Apples() {
$a = array();
Apple::setColor('red');
$a[] = array('Color'=>Apple::getColor());
Apple::setColor('green');
$a[] = array('Color'=>Apple::getColor());

return new DataObjectSet(new ArrayData($a));
}
}

Edit: Actually I dont think you should name your controller function getApples either renamed to Apples

<% if Apples %>
<% control Apples %>
$Color
<% end_control %>
<% end_if %>

Avatar
arsenic

15 January 2010 at 3:36pm Community Member, 9 Posts

Well, yes, that should work. But it defeats the purpose of object-oriented programming, no?