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.

Template Questions

can I use Top in a control?


Reply

8 Posts   2240 Views

Avatar
DsX

22 September 2010 at 1:34pm Community Member, 178 Posts

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.

Avatar
simon_w

22 September 2010 at 2:08pm Forum Moderator, 474 Posts

You can only compare against literals in ifs.

Avatar
DsX

22 September 2010 at 2:42pm (Last edited: 22 September 2010 2:42pm), Community Member, 178 Posts

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

Avatar
Willr

22 September 2010 at 6:58pm Forum Moderator, 5511 Posts

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

Avatar
DusX

1 October 2010 at 5:56am Community Member, 12 Posts

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?

Avatar
DsX

18 November 2010 at 7:50am Community Member, 178 Posts

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.

Avatar
Willr

18 November 2010 at 3:29pm Forum Moderator, 5511 Posts

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.

Avatar
DsX

19 November 2010 at 7:04am Community Member, 178 Posts

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?