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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Template Questions /

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

Accessing variables inside nested controls


Go to End


3 Posts   2895 Views

Avatar
nicola

Community Member, 2 Posts

8 June 2010 at 10:46am

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

Avatar
Xurk

Community Member, 50 Posts

21 June 2010 at 11:52pm

Edited: 21/06/2010 11:53pm

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 :)

Avatar
BenWu

Community Member, 97 Posts

13 June 2012 at 3:17am

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