Copied to clipboard

Flag this post as spam?

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


  • Simon Busborg 7 posts 73 karma points
    Sep 08, 2014 @ 15:30
    Simon Busborg
    1

    Determining template type and using it for and if else statement.

    Hi all :)

    I',m fairly new to Umbraco, but I'm stuck at this one. Can I somehow get the template type, and use it in a if/else statement?

    example: if 'home' template, then add class 'home' for the body element, else 'do nothing'?

    I've been searching around the web for a couple of hours, without any luck.

    Project info: Umbraco v. 7.1.6 - MVC

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Sep 08, 2014 @ 15:52
    Dennis Aaen
    100

    Hi Flip, and welcome our!

    With this snippet of code, you are checking what document type you are using for the page. In the example I have a document type called TextPage

    @if(CurrentPage.DocumentTypeAlias == "TextPage"){
        <p>this is a textpage</p>
    }else{
        <p>this ia NOT a textpage</p>
    }

    Or you could use the one of the standard fields in your body tag like this: http://our.umbraco.org/documentation/Reference/Mvc/views#PropertiesavailableinViews and http://our.umbraco.org/documentation/Reference/Mvc/views#RenderingafieldwithUmbracoHelper

    @Umbraco.Field("nodeTypeAlias")

    Hope this helps, if not don't hesitate to write again.

    /Dennis

  • Jamie Howarth 306 posts 773 karma points c-trib
    Sep 08, 2014 @ 15:53
    Jamie Howarth
    1

    Hey,

    Here's a quick sample I knocked up. Hope it helps.

    @{
       var currentTemplateName = umbraco.cms.businesslogic.template.Template.GetTemplate(Model.Content.TemplateId).Alias;
       var cssClass = "";
       switch (currentTemplateName) {
           case "Homepage":
               cssClass = "home";
               break;
           default:
               break;
       }
    }

    <body class="@cssClass">
       ...
    <body>
  • Simon Busborg 7 posts 73 karma points
    Sep 08, 2014 @ 16:19
    Simon Busborg
    0

    @Dennis the nodeTypeAlias did the trick. For some reason I get an error when trying to do an if/else on the body element. This would be easier and more persistent in the long run, so that all other pages besides the frontpage, would get a class called page etc.

    But for now I'm good :) thanks for the help! – And to you too @Benjamin :)

  • Jamie Howarth 306 posts 773 karma points c-trib
    Sep 08, 2014 @ 16:33
    Jamie Howarth
    0

    @Flip Dennis' code tells you the document type of the page, my code snippet tells you the actual template (masterpage/Razor) being used - they are two different things (a document type can have multiple templates used to render it to HTML). But HTH regardless :-)

  • Simon Busborg 7 posts 73 karma points
    Sep 08, 2014 @ 16:46
    Simon Busborg
    0

    @Benjamin Ah, okay. I'm getting it now :) I'm so new to this Umbraco thing, but the community kicks ass :)

Please Sign in or register to post replies

Write your reply to:

Draft