Make navigation react depending on top level of document path

Alan Isaacson asked on September 23, 2016 14:16

I currently have a repeater that I use to create our main site navigation(I have found it to be more flexible than using a default Kentico nav). It currently makes a collection of list items using the code

<li class="vs-menu-primary__item">
<a href="{% GetDocumentUrl() %}" class="{% if (IsCurrentDocument()) { return "sel" } else { return "" } #%}" title="{% HTMLEncode(DocumentName) %}">{% HTMLEncode(DocumentName) %}</a>
</li>

This returns a navigation such as Home, Treat, Learn, About Us with the relevant item stylised if you are in that page (currently an underline. I now want to add that if I am in page under that set of navigation that I it should also show as selected. Such as

  1. 1st Level = Treat (currently works)
  2. 2nd Level = Dogs (Doesn't show Treat as selected)
  3. 3rd Level = Dentistry (Doesn't show Treat as selected)

I didn't know if I should be able to add an else if (CurrentDocument.Parent.Parent()) {return "sel"}

to it but I cannot get the syntax to work. Any help appreciated.

Correct Answer

Jan Hermann answered on September 23, 2016 14:56

Use just CurrentDocument.NodeAliasPath.StartsWith(NodeAliasPath)

0 votesVote for this answer Unmark Correct answer

Recent Answers


Anton Grekhovodov answered on September 23, 2016 14:46

Hi Alan,

Try to use this condition in IF statement:

(IsCurrentDocument() || CurrentDocument.Parent.NodeID == NodeID || CurrentDocument.Parent.Parent.NodeID == NodeID)

Where NodeID is a ID of current menu item.

0 votesVote for this answer Mark as a Correct answer

Alan Isaacson answered on September 23, 2016 15:03

Than you both for the answers, the answer from Jan was easier to implement. Thanks for the help.

0 votesVote for this answer Mark as a Correct answer

Anton Grekhovodov answered on September 23, 2016 15:05

Yeah, His solution is better:)

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.