Copied to clipboard

Flag this post as spam?

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


  • Harry 8 posts 78 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 554 posts 2119 karma points
    Aug 27, 2020 @ 15:38
    Yakov Lebski
    0

    You can pass parameter via ViewBag from view to master

  • Steve Morgan 1346 posts 4453 karma points c-trib
    Aug 27, 2020 @ 16:45
    Steve Morgan
    1

    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 8 posts 78 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.

  • Prasant 10 posts 90 karma points
    Dec 12, 2022 @ 10:02
    Prasant
    0

    Thanks @Harry

    if (Model.Id == 1057) it worked for me

  • Yakov Lebski 554 posts 2119 karma points
    Dec 12, 2022 @ 12:49
    Yakov Lebski
    0

    generally is not so good idea to use an id or page name in code, it can be changed

    you can use

    1. check the document type var isHomepage=Model.IsDocumentType("homepage")
    2. pass variable via viewbag, from On Homepage.chtml

      ViewBag.BodyClass = "homepage";

    On _Layout

    <body class="@ViewBag.BodyClass">
    
Please Sign in or register to post replies

Write your reply to:

Draft