Jump to:

3393 Posts in 976 Topics by 852 members

Template Questions

SilverStripe Forums » Template Questions » Splitting a control at the halfway point.

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

Page: 1
Go to End
Author Topic: 988 Views
  • PapaBear
    Avatar
    Community Member
    26 Posts

    Splitting a control at the halfway point. Link to this post

    Hi all,

    On a client website I need to display the nav bar on two rows, splitting evenly at the halfway point.

    Eg:
    Item1 - Item2 - Item3 - Item4
    Item5 - Item6 - Item7 - Item8

    or

    Item1 - Item2 - Item3 - Item4
    Item5 - Item6 - Item7

    I tried to use

    <% if Pos = TotalItems / 2 %> and variations of that but kept getting PHP errors. The generated PHP code showed that the <% if ... %> had not been interpreted (still showed as the <% %> tag) whilst the <% end_if %> tag had been interpreted - causing the error.

    below is the code I have been playing with from my Navigation.ss file

    <ul>
       <% control Menu(1) %>
          <% if Pos = TotalItems / 2 %>
       </ul>
       <ul>
          <% end_if %>
          <li><a href="$Link" title="Go to the $Title.XML page" class="$LinkingMode">
          <% if Pos + 1 = TotalItems / 2 %>
          <span class="last">$MenuTitle.XML</span>
          <% else_if Last %>
          <span class="last">$MenuTitle.XML</span>
          <% else %>
          <span>$MenuTitle.XML</span>
          <% end_if %>
          </a></li>
       <% end_control %>
    </ul>

    Can anyone offer some suggestions? I have good PHP skills but my knowledge of Silverstripe is still growing

    Cheers
    James.

  • teejay
    Avatar
    Community Member
    63 Posts

    Re: Splitting a control at the halfway point. Link to this post

    Try to create a controller function HalfTotalItems do your db query stuff their again and return the the count / 2.

  • banal
    Avatar
    Community Member
    901 Posts

    Re: Splitting a control at the halfway point. Link to this post

    If your navigation items have a fixed with, you can do this in CSS only. Just set the width of the container element to 4 * the width of a navigation item and set float: left on the navigation items...

  • PapaBear
    Avatar
    Community Member
    26 Posts

    Re: Splitting a control at the halfway point. Link to this post

    Good suggestion about the fixed width. I've used that in the past.

    However, in this case the number of elements in the nav will be variable and the width of the items has a min-width but not a maximum. I didn't state that in the original question. Mea Culpa I'm afraid.

    Thank you for the prompt reply though!

    J

  • PapaBear
    Avatar
    Community Member
    26 Posts

    Re: Splitting a control at the halfway point. Link to this post

    Creating the custom control is what I'm sure I have to do but it doesn't seem to be working. I'm not sure I'm using the variables of $Pos and $TotalItems correctly:

       function NavSplit () {
          return ($Pos == $item->XML_val("TotalItems",null,true) % 2 ) ? true : false;
       }
       
       function NavBeforeSplit () {
          return ($Pos == ($item->XML_val("TotalItems",null,true) % 2) - 1 ) ? true : false;
       }

    NavSplit should return true if the current item is the middle item (ie the first item of the second row). NavBeforeSplit should return true if the current item is the last item of the first row.

    I'm not sure these functions will work correctly because of the cacheing.

    J

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