3375 Posts in 999 Topics by 712 members
|Go to End|
12 August 2009 at 11:08am
I'm new to Silverstripe so please bear with me if my questions are obvious.
I'm trying to create a two menu structures - one for the header and one for the footer. At a particular stage in each of these menus, I need to know which position I am at and make conditional decisions based on this. For example, if I am at Pos = 3 in a particular control block, then I want to add an extra class to the element I'm displaying. Is there a way to do this using control block code?
Another option for me is to extend the ViewableData class in order to add some custom functions to it. For example, I want to create a function similar to "Last" which is called "SecondLast" and does the same as "Last", except subtracts 2 instead of 1. Ideally I'd like to do this in a child class so I don't touch the Silverstripe core files. Is there a way I could implement this and then run the code block (see below):
<div id="footer-inner" class="clearfix">
<% control Page(footer) %>
<% if Children %>
<h5 class="acc">Footer Menu</h5>
<% control Children %>
<li <% if First || SecondLast %> class="separate" <% end_if %>><a href="$Link">$MenuTitle</a></li>
<% end_control %>
<% end_if %>
<% end_control %>
Any help is much appreciated. Thanks
12 August 2009 at 7:01pm Last edited: 12 August 2009 7:07pm
This has already been answered several times. For example here:
Or on ssbits.com: http://ssbits.com/manipulating-every-nth-item-in-a-control-loop/
You can implement the Pos = 3 or SecondLast in the same manner as the examples above suggest.
Hint The implementation of SecondLast isn't that obvious. Here's what you would do:
// this function belongs to the Page class
return $this->Pos() == $this->iteratorTotalItems -1;
12 August 2009 at 7:11pm
Thanks for your help mate.
Believe it or not I did do a search first but couldn't seem to find what I wanted (probably wasn't searching for the right keywords). I kind of figured it all out on my own anyway with regards to the functions in the class. I've only been using SilverStripe for about a day and at first it didn't seem to work, but then I realised I'd just gotten the syntax wrong. Now that I've worked that out it's all cool.
|Go to Top|