Copied to clipboard

Flag this post as spam?

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


  • Kyle Skrinak 272 posts 327 karma points
    Jul 31, 2012 @ 03:41
    Kyle Skrinak
    0

    Classing the body tag by URL path

    I'd like to have the ability to class my body tag depending on the current URL. Thus:

    http://www.example.com would render

    <body class="front">

    http://www.example.com/section/landing would render:

    <body class="not-front section">

    Where to start? Thanks!

  • Peter Gregory 408 posts 1614 karma points MVP 3x admin c-trib
    Jul 31, 2012 @ 04:34
    Peter Gregory
    1

    Your options could be as follows.

    1: You could write a small piece of inline C# to achieve this by looking at what the docmenttype is for the current page.

    class="<%= umbraco.NodeFactory.Node.GetCurrent().Nodetypealias == "HomePage" ? "front":"not-front" %>"

    2: Write a small razor macro to work it out.  The razor code would look something like this

    @(Model.NodeTypeAlias == "HomePage" ? "front" : "not-front")

    and then on the page in the body tag 

    <body class="<umbraco:macro alias="CheckIfHome" runat="server />">
  • Kyle Skrinak 272 posts 327 karma points
    Jul 31, 2012 @ 04:57
    Kyle Skrinak
    0

    It seems that .Nodetypealias was depreciated in 4.5? http://bit.ly/M01HZm

    I like the #2 better, if only as I want to do more than class body with front/non-front, i.e., the first term in a path as in my example. Using a call to a macro is easier to manage than inline code. HOWEVER, nodetypealias is returning the doctype used, not whether it's the front page for the site.

  • Peter Gregory 408 posts 1614 karma points MVP 3x admin c-trib
    Jul 31, 2012 @ 05:29
    Peter Gregory
    1

    It is not deprecated as such as it depends if you are querying the node using XPath in XSLT or via the API.  It should have read NodeTypeAlias and as we are using the API and Razor in the above examples it is applicable. 

    I used the Document Type Alias as an example of how to work out if its the homepage because generally you will only every use a single doctype to represent your homepage.  Another option could be to test against the Level property of a document.

    @Model.Level == 1

    But again this entirely depends on how you have structured your tree.

     

  • Kyle Skrinak 272 posts 327 karma points
    Jul 31, 2012 @ 17:50
    Kyle Skrinak
    0

    Oops; double post.

  • Kyle Skrinak 272 posts 327 karma points
    Jul 31, 2012 @ 17:56
    Kyle Skrinak
    0

    I just had my first coding experience with Razor. Here's the result:

    @{
      var bodyClass = "";
      
      if (Model.NodeTypeAlias == "WLHomePage" ) {
         bodyClass = "front";
      } else {
         bodyClass = "not-front";
      }
      if (Model.Level < 2) {
        bodyClass = bodyClass + " " + Model.Url;
      }
      
      @bodyClass;
    }

    I'll still need to split and get the first arg from the "model.level" section, i.e.,

    http://www.example.com/section/page.aspx
    <---------------------|^^^^^^^|-------->

    Rendering:

    <body class="not-front section">...

    that's to come when I have more nested pages.

    Thanks!

Please Sign in or register to post replies

Write your reply to:

Draft