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

Method Calls from Templates Fail


Go to End
Reply


11 Posts   2985 Views

Avatar
arsenic

Community Member, 9 Posts

15 January 2010 at 2:57pm

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

Community Member, 105 Posts

15 January 2010 at 3:06pm

Edited: 15/01/2010 3:16pm

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

Community Member, 9 Posts

15 January 2010 at 3:36pm

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

Go to Top