Jump to:

3444 Posts in 1030 Topics by 871 members

Template Questions

SilverStripe Forums » Template Questions » How to access a page variables within an if statement within a control?

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 633 Views
  • machiaveli
    Avatar
    Community Member
    1 Post

    How to access a page variables within an if statement within a control? Link to this 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.

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

    Thanks!

  • martimiz
    Avatar
    Forum Moderator
    1086 Posts

    Re: How to access a page variables within an if statement within a control? Link to this post

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

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