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.

General Questions

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

navigation class


Go to End
Reply

9 Posts   1444 Views

Avatar
mhull

19 July 2009 at 9:54am Community Member, 79 Posts

I have the following code in my navigation to add a class to my current page:

<li <% if LinkOrSection = section %>class="current"<% end_if %>><a href="$Link" >$MenuTitle</a>

How would I add an alternate class onto this if within a certain link or section? for example:

<li <% if LinkOrSection = whats-new %>class="hidden"<% end_if %>><a href="$Link" >$MenuTitle</a>

How would I add these together?

My full navigation code is:

   <ul id="topmenu" class="sf-menu sf-navbar">
<% control Menu(1) %>

<li <% if LinkOrSection = section %>class="current"<% end_if %>><a href="$Link" >$MenuTitle</a>

<% if Children %>
<% if URLSegment != whats-new %>
<% if URLSegment != work-with-us %>

<ul>
<% control Children %>
<li class="$LinkingMode"><a href="$Link" >$MenuTitle</a></li>
<% end_control %>
</ul>
<% end_if %>
<% end_if %>
<% end_if %>
</li>
<% end_control %>
</ul>

Avatar
mhull

20 July 2009 at 1:17am Community Member, 79 Posts

Okay, So I am trying the following, but it isnt reading the else. Am I writing it wrong? Any help would be much appreciated.

   <ul id="topmenu" class="sf-menu sf-navbar">
<% control Menu(1) %>

<li <% if LinkOrSection = section %>class="current"<% end_if %>><a href="$Link" >$MenuTitle</a>

<% if Children %>
<ul>
<% control Children %>
<li class="$LinkingMode"><a href="$Link" >$MenuTitle</a></li>
<% end_control %>
</ul>
<% end_if %>
</li>

<% else %>
<% if URLSegment = work-with-us %>
<li class="work"><a href="$Link">$MenuTitle</a></li>
<% if Children %>
<ul>
<% control Children %>
<li class="$LinkingMode"><a href="$Link" >$MenuTitle</a></li>
<% end_control %>
</ul>
<% end_if %>

<% end_if %>
<% end_control %>
</ul>

Avatar
mhull

20 July 2009 at 9:28am Community Member, 79 Posts

Why is the following not working?
If whats new use add this class to children, otherwise use other code. What am I doing wrong?

   <ul id="topmenu" class="sf-menu sf-navbar">
<% control Menu(1) %>

<li <% if LinkOrSection = section %>class="current"<% end_if %>><a href="$Link" >$MenuTitle</a>

<% if Children %>

<ul>
<% control Children %>

<% if URLSegment != what-we-ve-done %>
<% control Children %>
<li class="plop"><a href="$Link" >$MenuTitle</a></li>
<% end_control %>
<% end_if %>
<% else %>
<li class="$LinkingMode"><a href="$Link" >$MenuTitle</a></li>
<% end_control %>
</ul>

<% end_if %>
</li>
<% end_control %>
</ul>

Any help? please anyone?

Avatar
AdamJ

20 July 2009 at 2:16pm (Last edited: 20 July 2009 2:18pm), Community Member, 145 Posts

Can you post your site structure, eg:

- index
- about
- - staff
- - location
- services
- contact

Avatar
Willr

20 July 2009 at 3:37pm Forum Moderator, 5511 Posts

mhull - you have used URLSegment != ... negation (!=) is not supported in ss templates. You have to do <% if URLSegment = blah %><% else %>.....

Avatar
mhull

20 July 2009 at 9:13pm Community Member, 79 Posts

Many Thanks, with your comment and trial and error I finally got it to do what I wanted! Cheers

My final code, hiding certain children

<ul id="topmenu" class="sf-menu sf-navbar">
<% control Menu(1) %>

<li <% if LinkOrSection = section %>class="current"<% end_if %>><a href="$Link" >$MenuTitle</a>

<% if Children %>

<% if URLSegment = whats-new %>
<ul>
<% control Children %>
<li class="hidden"><a href="$Link" >$MenuTitle</a></li>
<% end_control %>
</ul>

<% else %>

<% if URLSegment = work-with-us %>
<ul>
<% control Children %>
<li class="hidden"><a href="$Link" >$MenuTitle</a></li>
<% end_control %>
</ul>

<% else %>

<ul>
<% control Children %>
<li class="$LinkingMode"><a href="$Link" >$MenuTitle</a></li>

<% end_control %>
</ul>
<% end_if %>
<% end_if %>
<% end_if %>
</li>
<% end_control %>
</ul>

Avatar
AdamJ

20 July 2009 at 10:14pm Community Member, 145 Posts

willr, this documentation isn't really correct then...

http://doc.silverstripe.org/doku.php?id=templates#if_blocks

mhull, according to those docs (if its supported) you can use else_if, so instead of having the second if statement within the else statement for the first if statement, you could use an else_if.

Avatar
Willr

20 July 2009 at 10:34pm Forum Moderator, 5511 Posts

Actually I lie, != *is* supported according to the source in SSViewer. Whether it works or not well thats up to god. I was getting confused with <% if !$Val %>.. which is not implemented

Go to Top