Jump to:

3435 Posts in 1026 Topics by 866 members

Template Questions

SilverStripe Forums » Template Questions » Variables in nested controls

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

Page: 1
Go to End
Author Topic: 1330 Views
  • Wyman
    Avatar
    Community Member
    9 Posts

    Variables in nested controls Link to this post

    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?

  • swaiba
    Avatar
    Forum Moderator
    1785 Posts

    Re: Variables in nested controls Link to this post

    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.

  • Wyman
    Avatar
    Community Member
    9 Posts

    Re: Variables in nested controls Link to this post

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

  • swaiba
    Avatar
    Forum Moderator
    1785 Posts

    Re: Variables in nested controls Link to this post

    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

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