Jump to:

3401 Posts in 979 Topics by 855 members

Template Questions

SilverStripe Forums » Template Questions » Accessing variables inside nested controls

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

Page: 1
Go to End
Author Topic: 1953 Views
  • nicola
    Avatar
    Community Member
    2 Posts

    Accessing variables inside nested controls Link to this post

    Hey Everyone,
    Is there anyway I can access variables from inside nested Controls? Its probably easier to explain it using a few code snippets. The below code works and does pretty much what you'd expect. It prints out a list of venues then a list of campaigns for each venue and then a list of products for each campaign.

    <% control Entities %>
                <h3>$EntityName</h3>
          
                <% control Campaigns %>
                   <h4><a href="/browse-campaigns/view/$ID">$Name</a></h4>
                   <% control Products %>
                      <h5>$Name - $Count</h5>
                   <% end_control %>
                <% end_control %>
             <% end_control %>

    This works, but I don't really want it to be structured hierarchically like this instead I would just like a plain list of products, but I also want to output the variables for each entity and campaign. Basically I am trying to do something like the below. Is there any way to achieve this or am I better of changing the structure of the array data that I am using?

    <% control Entities %>
                <% control Campaigns %>   
                   <% control Products %>
    <h3>$Entities.Name</h3>
    <h4><a href="/browse-campaigns/view/$Campaigns.ID">$Campaigns.Name</a></h4>
                      <h5>$Name - $Count</h5>
                   <% end_control %>
                <% end_control %>
             <% end_control %>

  • Xurk
    Avatar
    Community Member
    50 Posts

    Re: Accessing variables inside nested controls Link to this post

    Have you tried $Parent.Name and / or $Top.Name ? The prior should be able to access the variable from the control one level up in your nested architecture. The latter should retrieve the variable from the (you guessed it) top-level of the template. But that would probably be looking outside all of the controls for the variable, where it won't be found.

    So I think $Parent would be your best bet, although I'm not sure how that behaves when there are more than just two levels, seeing as you have three controls nested inside each other in your "ideal situation" example.

    Good luck

  • BenWu
    Avatar
    Community Member
    87 Posts

    Re: Accessing variables inside nested controls Link to this post

    No, $Top or $Parent doesn't work for me. Any other way??

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