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

Variables in nested controls


Go to End


4 Posts   2319 Views

Avatar
Wyman

Community Member, 9 Posts

7 May 2011 at 7:14am

Edited: 07/05/2011 7:15am

I have a include template where I have a nested control and the child control is a function that takes in a parameter.
I need to pass in a parameter via a variable in the parent control, but I can not get this to work.
Here's a stripped down example of what I've done so far:

<% if GetProductSubPageOverview %>
	<% control GetProductSubPageOverview %>
	<div id="$URLSegment" class="tabPage typography">
		<div style="position:relative; width:242px; height:181px;" id="overviewImgs">
			<p>$PictureURL</p>
			<% control Top.getImagesFrom(Top.PictureURL) %>
				<p>$Parent.PictureURL</p>
				<img style="display:none;" src="$URL" alt="" width="930" height="200"/ >
			<% end_control %>
		</div>
		$Content
	</div>
	<% end_control %>
<% end_if %>

Now, I've been google searching and I found out about the $Top and $Parent variables and that's how I got as far as getting the Top.getImagesFrom function to run in the nested control (if I pass in a simple string it all works). However, if I put in the 'Top.PictureURL" you see in the code, nothing is passed to the function. Note the "<p>$PictureURL</p>" does return the right variable, so I know I'm getting my right value until I get to the control. I've tried accessing the variable inside the nested control by the "<p>$Parent.PictureURL</p>" portion of code, but this also doesn't give me any value.
Can anyone tell me how I can access these variables?

Avatar
swaiba

Forum Moderator, 1899 Posts

7 May 2011 at 7:43am

you can't put a variable as an argument to a function within <% %>

so..

  <% control Top.getImagesFrom(Top.PictureURL) %> 

..will not work. Instead you could pre-bundle the information and then parse it in the template.

Avatar
Wyman

Community Member, 9 Posts

7 May 2011 at 9:15am

It took me a while to think through your answer, and I was able to find another post of yours on the site that expanded on your reply:
http://www.silverstripe.org/template-questions/show/13619#post290481
got it working now, though it is kind of sad that the controls cannot read parent variables...

Avatar
swaiba

Forum Moderator, 1899 Posts

7 May 2011 at 9:21am

funny but I was thinking of that post! I was a bit terse - as you can see I've answered that question a couple of times. I wish I could easily see my past posts, I'd just like to them each time it comes up again.

anyway glad you've got it working