Jump to:

3439 Posts in 1028 Topics by 869 members

Template Questions

SilverStripe Forums » Template Questions » can I use Top in a control?

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

Page: 1
Go to End
Author Topic: 2213 Views
  • DsX
    Avatar
    Community Member
    178 Posts

    can I use Top in a control? Link to this post

    I have code as follows:

    <% control ImageResources %>
    <% if TopActive == ID %>
    <ul>
    <li>not showing the image with ID= $Top.Active</li>
    </ul>
    <% else %>
    <ul>
    <li>DATA= $Top.getActive ... $ID - test this style call</li>
    <li>DATA= $Top.Active ... $ID - test this style call</li>
    <li><a href="{$Top.Link}Display/{$ID}" >$Title</a></li>
    <li>caption: $Caption</li>
    <li>$AttachedImage.SetRatioSize(50,50)</li>
    </ul>
    <% end_if %>
    <% end_control %>

    I can't figure how I can use an if statement to compare the local ID and the ID from a level up.
    You can see I have a couple test li, that show me that I am getting the Top.Active and ID, and I can see when they match.
    I just can't get the template to like my conditional.

    Please help.. this should be easy I would think.

  • simon_w
    Avatar
    Forum Moderator
    471 Posts

    Re: can I use Top in a control? Link to this post

    You can only compare against literals in ifs.

  • DsX
    Avatar
    Community Member
    178 Posts

    Re: can I use Top in a control? Link to this post

    OK, so does that mean I can pass the ID along to the method?
    and can I use Top?

    I found that the top. syntax causes an error.

    I'm trying to get a sense of the limitations here..

  • Willr
    Avatar
    Forum Moderator
    5497 Posts

    Re: can I use Top in a control? Link to this post

    No you cannot pass any variables along to it. You need to contain any logic like that in the php class.

  • DusX
    Avatar
    Community Member
    12 Posts

    Re: can I use Top in a control? Link to this post

    thanks for the tip.. took me a little to get my head wrapped around it but I'm there.

    on a related note:
    I am trying to use 'Top' to break out of a control, and am finding that I don't really get the data that I expect.
    Is there a clear definition of what using 'top' really does?
    I have tested it in a number of cases, and it doesn't seem to give you access to the data outside the control but rather to the base of the template.
    Is there another way to reference data outside a control other than writing custom methods to return the data?

  • DsX
    Avatar
    Community Member
    178 Posts

    Re: can I use Top in a control? Link to this post

    Where might I find 'top' in the api docs?

    Is there any other similar command that will breakout of a number of controls?

    I mean it would be handy to be able to say $break(1).title if you wanted to output the title of a dataobject that you are using in a control while you are also controlling image (so that you can use the image methods).
    Being able to break out 1 level would be great, but if you could break out a defined number that would be outstanding.
    Currently the only similar feature seems to be top, but that doesn't help if I want data related to the dataobject that I'm in.

  • Willr
    Avatar
    Forum Moderator
    5497 Posts

    Re: can I use Top in a control? Link to this post

    Top will always go to the Top most scope. Its a function on every ViewableData object. You can see the method (but no docs) here

    http://api.silverstripe.org/2.4/sapphire/view/ViewableData.html#methodTop

    You cannot currently go $Parent or $Back to break out of just 1 loop. This was a recent discussion on the core mailing list. Check that out for all the discussion and possible solutions.

  • DsX
    Avatar
    Community Member
    178 Posts

    Re: can I use Top in a control? Link to this post

    Thanks, I was browsing the api and found toplevel() but not viewable data, still working on getting a full understanding of the structure of the system, I guess it will only come with time, however I am getting a sense of how I might be able to write a short tutorial to show beginners how they can use the api docs to locate additional methods. Do you think that this would be well received?

    I found this post that has helped me with my problem somewhat:
    http://www.silverstripe.org/template-questions/show/8518

    Where would I find the core mail-list to read up on the discussion you mentioned?

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