Two level menu with transformations

Andrew Campkin asked on March 13, 2017 01:13

Hi everyone,

I am attempting to use a transformation to create a 2 level navigation with dropdowns. I am using the below code to try and highlight the navigation item, if a page within the dropdown is selected. This works ok in the development environment but we are having issues deploying this. We think it is because the page ids are different between the systems.

When using the macro debug feature, we can see different page ids than the ones we see in the database so I'm not sure what has happened. Any ideas?

{% if (IsCurrentDocument() || (NodeHasChildren && CurrentDocument.Parent.Id == NodeId)) { return "active" } else { return "" } %}

Correct Answer

Jan Hermann answered on March 13, 2017 12:47

This code is potentially problematic:

CurrentDocument.Parent.Id == NodeId

Please, always compare same properties:

CurrentDocument.Parent.NodeID == NodeID

Also, there is already a macro for this:


1 votesVote for this answer Unmark Correct answer

Recent Answers

Roman Hutnyk answered on March 13, 2017 12:01

IDs are different, but that shouldn't be an issue unless they are hardcoded in transformation or somewhere else.

What is your issue? Could you please provide more details? Does it adds classes to menu items? Are appropriate CSS classes implemented in production?

0 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on March 13, 2017 16:19 (last edited on March 13, 2017 16:20)

What's the web part are you using for your menu? Look at Best Practices for Implementing Site Navigation in Kentico. Two-level menu can be easily done with Hierarchical viewer web part.

0 votesVote for this answer Mark as a Correct answer

Andrew Campkin answered on March 13, 2017 21:08 (last edited on March 24, 2018 12:15)

Thanks Jan Hermann, this is exactly what we needed. The solution is now simply:

{% if (IsDocumentOnSelectedPath()) { return "active" } else { return "" } %}
0 votesVote for this answer Mark as a Correct answer

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