Jump to:

3448 Posts in 1062 Topics by 738 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Trying to extend ViewableData

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

Page: 1
Go to End
Author Topic: 1517 Views
  • sarge
    Community Member
    4 Posts

    Trying to extend ViewableData Link to this post

    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 %>
          <% end_if %>
       <% end_control %>

    Any help is much appreciated. Thanks


  • banal
    Community Member
    901 Posts

    Re: Trying to extend ViewableData Link to this post


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

  • sarge
    Community Member
    4 Posts

    Re: Trying to extend ViewableData Link to this post

    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.


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.