Michelle
Michelle

Reputation: 249

Kentico - Hierarchical Viewer - Different Transformations for Different MenuClass

I use Hierarchical Viewer to build a menu. What I want to achieve but don't know how is to do an If statement in the Header and Footer Transformation to say: if the Menu Class contains "menu-large", then display abc, if not then display xyz, something like below. My Transformation is Text/XML. Thanks for your help!

{% 
result = "<ul class=\"dropdown-menu\">"; // default value
if(HTMLEncode(DocumentMenuClass).Contains("menu-large")) {result ="<ul class=\"dropdown-menu megamenu\"><div class=\"container megamenu-container\"><div class=\"row\">"}  
return result;
#%} 

Upvotes: 0

Views: 291

Answers (4)

Matt Kayan
Matt Kayan

Reputation: 45

As Trevor stated, header/footer transformations don't represent documents, they just get displayed before/after item transformations (which do represent the documents).

If you're using an item transformation to display the parent of your menu items, you can use Prashant's code to get the DocumentMenuClass value there.

Upvotes: 1

Trevor F
Trevor F

Reputation: 1437

The header and footer do not have context of any page, so you can't see the menu css field.

You want to use the first item transformation type, that should trigger on the first of it's level.

Upvotes: 2

Prashant Verma
Prashant Verma

Reputation: 64

You need to simply use ternary operator and place your condition like below in your transformation.

{% HTMLEncode(DocumentMenuClass).Contains("menu-large") ? "<ul class='dropdown-menu megamenu'><div class='container megamenu-container'><div class='row'>" : "<ul class='dropdown-menu'>" %}

This should work as expected.

Happy to help

Upvotes: -1

Roman Hutnyk
Roman Hutnyk

Reputation: 1549

First thing I'd check are the output of HTMLEncode(DocumentMenuClass) and HTMLEncode(DocumentMenuClass).Contains("menu-large") calls. This might give you an idea why you're not getting expected result.

Your macro looks correct, but you could also try this:

When defining conditions or loops, you can leave the body of the loop/condition open and close it later in another macro expression. This allows you to apply the command to text content or HTML code placed between the macro expressions. Open commands can be particularly useful in macro-based transformations or various types of HTML templates.

Also you could try to rewrite your macro without return statement like most examples in documentation.

Upvotes: 0

Related Questions