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.

Data Model Questions

Trying to extend ViewableData


Reply

3 Posts   1533 Views

Avatar
sarge

12 August 2009 at 11:08am Community Member, 4 Posts

Hi guys,

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>
      <ul class="clearfix">
         <% control Children %>
            <li <% if First || SecondLast %> class="separate" <% end_if %>><a href="$Link">$MenuTitle</a></li>
         <% end_control %>
      </ul>
      <% end_if %>
   <% end_control %>
</div>

Any help is much appreciated. Thanks :)

Christian

Avatar
banal

12 August 2009 at 7:01pm (Last edited: 12 August 2009 7:07pm), Community Member, 901 Posts

Hi

This has already been answered several times. For example here:
http://silverstripe.org/general-questions/show/266511?start=0#post266513

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
function SecondLast(){
   return $this->Pos() == $this->iteratorTotalItems -1;
}

Avatar
sarge

12 August 2009 at 7:11pm Community Member, 4 Posts

Hi there,

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.

Thanks though. :)

Christian