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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Template Questions /

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

Splitting a control at the halfway point.


Go to End


5 Posts   1729 Views

Avatar
PapaBear

Community Member, 26 Posts

15 February 2010 at 3:12pm

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.

Avatar
teejay

Community Member, 63 Posts

15 February 2010 at 6:36pm

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

Avatar
bummzack

Community Member, 904 Posts

15 February 2010 at 7:35pm

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

Avatar
PapaBear

Community Member, 26 Posts

16 February 2010 at 8:07am

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

Avatar
PapaBear

Community Member, 26 Posts

16 February 2010 at 8:12am

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