Jump to:

23478 Posts in 18941 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Method Calls from Templates Fail

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

Page: 1 2
Go to End
Author Topic: 2258 Views
  • arsenic
    Avatar
    Community Member
    9 Posts

    Re: Method Calls from Templates Fail Link to this post

    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?

  • CodeGuerrilla
    Avatar
    Community Member
    105 Posts

    Re: Method Calls from Templates Fail Link to this post

    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 %>

  • arsenic
    Avatar
    Community Member
    9 Posts

    Re: Method Calls from Templates Fail Link to this post

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

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