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

page attributes/methods from inside a control


Reply

7 Posts   981 Views

Avatar
gregsaunders

21 June 2011 at 11:34am (Last edited: 21 June 2011 11:36am), Community Member, 12 Posts

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!

Avatar
swaiba

21 June 2011 at 11:49am Forum Moderator, 1796 Posts

try

<% if Top.getMemberType = Pathfinder %>

Avatar
gregsaunders

21 June 2011 at 11:52am (Last edited: 21 June 2011 11:53am), Community Member, 12 Posts

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

Thanks though

Avatar
swaiba

21 June 2011 at 12:13pm (Last edited: 21 June 2011 12:24pm), Forum Moderator, 1796 Posts

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

Avatar
gregsaunders

21 June 2011 at 1:24pm (Last edited: 21 June 2011 2:13pm), Community Member, 12 Posts

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!

Avatar
swaiba

21 June 2011 at 8:16pm Forum Moderator, 1796 Posts

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!

Avatar
Willr

22 June 2011 at 5:35pm Forum Moderator, 5511 Posts

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