Jump to:

23002 Posts in 11855 Topics by 2828 members

General Questions

SilverStripe Forums » General Questions » page attributes/methods from inside a control

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
Go to End
Author Topic: 893 Views
  • gregsaunders
    Avatar
    Community Member
    12 Posts

    page attributes/methods from inside a control Link to this post

    if I have a function in the Page_Controller

       public function getMemberType() {
          $family_member = $this->getFamilyMember();
          return $family_member->MemberType()->Name;
       }

    why does this work

    <% if getMemberType = Pathfinder %>

    and this also works

    <p>$CurrentPage.getMemberType</p>

    but this does not

    <% if CurrentPage.getMemberType = Pathfinder %>

    at this point I'm inside a contol of some other object but I want to reach back up to the CurrentPage and get the "getMemberType" method.

    Help!

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: page attributes/methods from inside a control Link to this post

    try

    <% if Top.getMemberType = Pathfinder %>

  • gregsaunders
    Avatar
    Community Member
    12 Posts

    Re: page attributes/methods from inside a control Link to this post

    I get a php parse error ... same as if I use "CurrentPage" instead of "Top" ...

    Thanks though

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: page attributes/methods from inside a control Link to this post

    well "Top" is the way to do it, not CurrentPage (http://doc.silverstripe.org/sapphire/en/reference/built-in-page-controls) and without seeing the parse error it's hard to help...

    how about

    <% control Top %>
    <% if getMemberType = Pathfinder %>
    stuff
    <% end_if %>
    <% end_control %>

  • gregsaunders
    Avatar
    Community Member
    12 Posts

    Re: page attributes/methods from inside a control Link to this post

    your code works but that is not the problem. I'm not talking about a control loop. If "getMemberType" is a method of the Page_Controller, why does

    <% if getMemberType = Pathfinder %>

    work but

    <% if Top.getMemberType = Pathfinder %>

    does not. However

    <p>$Top.getMemberType</p>

    works just fine. The reason I'm trying to "if Top.getMemberType" is that I'm in a control loop of an object and the Page is no longer accessible to me.

    So maybe the question I need answered is "how do I set a variable on a page and access if from anywhere ... even several control loops deep".

    BTW, the parse error reported by Apache is "PHP Parse error: syntax error, unexpected '}' in" ...

    Thanks!

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: page attributes/methods from inside a control Link to this post

    Just to let you know control is *both* a scope changing operation and loop iterator. So using (in the way I advised) changes the current scope to the "top" context of the page.

    I believe they are moving in a less confusing direction for SS3 on this issue.

    And I could guess the start of the error! the line number and file and then the contents of the file at that line number would be of benefit - it is possible to debug these "compiled" ss files as they are just php and they do make sense once you go to the line number - the smaller the file the easier it is - best of luck!

  • Willr
    Avatar
    Forum Moderator
    5464 Posts

    Re: page attributes/methods from inside a control Link to this post

    I believe they are moving in a less confusing direction for SS3 on this issue.

    You're correct. SS3 is separating it out to <% with %> and <% loop %>, but thats not until 3.0.

    Perhaps try

    <% control Top %>
    <% if getMemberType = Pathfinder %>
    ..

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