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.

Template Questions

How to access a page variables within an if statement within a control?


2 Posts   644 Views


24 August 2012 at 2:48pm Community Member, 1 Post

I have the follow code in my page.

I have the variable $GiftID in my page.
I want to cycle through all of my gift objects using my function getGifts().
When the $ID of the gift is equal to the $GiftID of the page then I want something to happen.

<% control getGifts %>
   <% if CurrentPage.GiftID = ID %>This is it!<% end_if %>
<% end_control %>

I can't figure out how to access the variables from outside of the control within the if statement... Am I missing something very simple? I feel there should be an easy way to do this.

I am using SS 2.9



2 September 2012 at 2:23am Forum Moderator, 1091 Posts

Hi machiaveli

I realize this is a late reply, sorry for that. You can use $Top to access Page variables from within a control/loop/with structure. However using

<% if Top.GiftID = ID %>This is it!<% end_if %>

would check on the litteral value 'ID', and not on the value of the variable $ID. I know in SilverStripe 3 you should be able to use the following:

<% if Top.GiftID = $ID %>This is it!<% end_if %>

But I don't think this is possible in 2.x. In that case you could maybe (re)build the function getGifts() so that an extra property $ValidID (true/false) is returned for every gift object. Or some other php logic...