Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Mads Sørensen 188 posts 433 karma points
    Feb 11, 2015 @ 10:58
    Mads Sørensen
    0

    Any subpages then add-class Partial view

    Hi Guys
    Still in a lot of questions of doing the Razor :P

    I would like to check for "Children.Any" and if there are any then add a class of "menu-item-has-children".
     

    <li class="@(topPage.Children.Any() ? "menu-item-has-children")">

     

    I'm really not sure if the inline script is the right way to do it?

    I just know that it's possible to add like class attributes in XSLT? so whats the best way to do it in Partials/razor :D

  • Jan Skovgaard 11258 posts 23500 karma points MVP 7x admin c-trib
    Feb 11, 2015 @ 11:16
    Jan Skovgaard
    1

    Hi Mads

    Perhaps you can use topPage.Children.Count() instead? The count will return the number of existing children so it will be 0,1,2 etc.

    So this should work

    <li class="@(topPage.Children.Count() > 0 ? "menu-item-has-children" : "menu-item")">
    

    So if there are any children then the "menu-item-has-children" class will be set otherwise the "menu-item" class will be set. If you don't wish to add a clash you can just leave it empty or maybe even just use null. Not sure about that one though...still new at this way of thinking when rendering things :)

    Inline is the way to go when using Razor syntax...I liked the xslt way of doing it much more though :)

    Hope this helps and works.

    /Jan

  • Mads Sørensen 188 posts 433 karma points
    Feb 11, 2015 @ 11:48
    Mads Sørensen
    0

    Hi Jan
    Great reply - it makes totaly sense - But yeah the XSLT way I kind of like better to :)

    Bottomline is that the code works but it really is messy to look at if you have, like me, diffrent "if's" at the same element :P

    It looks like this for the moment:

    <li class="@(topPage.IsAncestorOrSelf(CurrentPage) ? "current" : null) @(topPage.Children.Where("TemplateId != 0 && Visible").Count() > 0 ? "menu-item-has-children" : "menu-item")"> 
  • Jan Skovgaard 11258 posts 23500 karma points MVP 7x admin c-trib
    Feb 11, 2015 @ 11:59
    Jan Skovgaard
    0

    Hi Mads

    Thanks, you're welcome :)

    Yup, it's really ugly to look at - it makes my eyes bleed.

    You can probably save some of the expressions in variables making it look a tad nicer but even though you do that it will stille be a bit ugly.

    But I'm thinking you should be able to save the count into a var like

    var numberOfChildren = @topPage.Children.Where("TemplateId != 0 && Visible").Count();
    

    And then in your test just check numbersOfChildren > 0 for instance

    /Jan

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies