Copied to clipboard

Flag this post as spam?

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


  • Harry 3 posts 73 karma points
    Aug 27, 2020 @ 08:43
    Harry
    0

    If current page is the home node, add class.

    Hi,

    I'm pretty new to Umbraco and .NET.

    What I am trying to achieve is, I have created a div section within the master template, which has been applied to the Home Page Doc Type as well as other Pages.

    However, I only want to add the class of "home" to the div on the Home page Node, not any other of the pages with contain the div from the master template.

    Any help would be much appreciated. Thanks.

  • Yakov Lebski 427 posts 1654 karma points
    Aug 27, 2020 @ 15:38
    Yakov Lebski
    0

    You can pass parameter via ViewBag from view to master

  • Steve Morgan 1278 posts 4216 karma points c-trib
    Aug 27, 2020 @ 16:45
    Steve Morgan
    0

    Hi Harry,

    Here's a quick example. I've cut the head section out for clarity.

    Hopefully it's readable - I create a variable "isHomepage" - checking if the root id node is the same as the current one.

    Then there is a ternary if / else block that adds a class to the body tag in razor.

    Ternary is just a posh way of saying shortened if {} else {} https://www.tutorialsteacher.com/csharp/csharp-ternary-operator

    The null is a way of not having blank space in the class.

    enter image description here

  • Harry 3 posts 73 karma points
    Aug 28, 2020 @ 08:22
    Harry
    0

    Hi Steve,

    Thanks for responding. This is the solution I came up with after doing some research.

    However, I will test your method because my method returns an empty class on the pages I do not want to apply the class of "home".

    String homeClass = string.Empty; if (Model.Id == 1057) { homeClass = "home"; }

    Thanks again for your help.

  • 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