Question

I have what I feel is a very simple question about Umbraco, but one that has as of yet no apparent answer.

I have a razor template, standard stuff, with @ displaying variables and some inline C# code.

At one point in the template I use:

@Umbraco.RenderMacro("myCustomMacro");

no problems there, everything works as expected.

Now, this macro is inserted on every page (it's in the master template) but I have a page property that allows the content authors to turn it on and off via a check box in the page properties, again so far so good everything works perfectly.

However I now find that for a certain "document type" this component MUST be displayed, so I've been trying to find a way to perform that check.

Now in my mind, this should be as simple as doing something like this:

@{
  if(CurrentPage.documentType == "someDocTypeAliasHere")
  {
     //Render the macro
  }
  else
  {
     // Render the macro only if the tick box is checked
  }
 }

as I say, this is (or I believe it should be anyway) a very simple operation, but one that so far does not seem to have a result.

What Have I tried so far?

Well apart from reading every page on our-umbraco that mentions anything to do with razor & the @CurrentPage variable, Iv'e been through the razor properties cheat sheet, and tried what would appear to be the most common properties including (In no specific order):

@CurrentPage.NodeTypeAlias
@CurrentPage.NodeType
@CurrentPage.ContentType
@CurrentPage.DocumentType

and various letter case combinations of those, plus some others that looked like they might fit the bill.

Consistently the properties either don't exist or are empty so have no useable information in them to help determine the result.

So now after a couple of days of going round in circles, and not getting anywhere I find myself here..

(Please note: this is not a search the XSLT question, or iterate a child collection or anything like that, so any requests to post XSLT, Macros, Page templates or anything like that will be refused, all I need to do is find a way to determine the Document Type of the current page being rendered.)

Cheers

Shawty

PS: Forgot to mention, I'm using

umbraco v 4.11.8 (Assembly version: 1.0.4869.17899)

Just in case anyone asks.

Was it helpful?

Solution 3

think you do actually need to create a node each time when you are on the page to access the pages properties like nodetypealias and stuff, try this i have the same kind of functionality on my site, http://rdmonline.co.uk/ but in the side menu where depending on the page/section it shows a diff menu links.

    @{
        var currentPageID = Model.Id;
        var currentPageNode = Library.NodeById(currentPageID);

        if (currentPageNode.NodeTypeAlias == "someDocTypeAliasHere")
          {
             //Render the macro
          }
          else
          {
             // Render the macro only if the tick box is checked
          }
     }

Let me know if this works for you.

OTHER TIPS

In Umbraco 7 use currentPageNode.DocumentTypeAlias

In Umbraco 7.1 I use: @if (@CurrentPage.DocumentTypeAlias == "NewsItem")

This is a bit unrelated to this post, but searching Google brought me to this post, so I thought I'd share in case anoyne else is dealing with this issue: In Umbraco 7, to get all content in the site for a specific type:

var articles = CurrentPage.AncestorOrSelf(1).Descendants()
                   .Where("DocumentTypeAlias == \"BlogPost\"").OrderBy("CreateDate desc");

If your razor view inherits from Umbraco.Web.Mvc.UmbracoViewPage, you could also use UmbracoHelper:

@if (UmbracoHelper.AssignedContentItem.DocumentTypeAlias.Equals("NewsItem")) { ... }

Querying for a specific DocumentType is also easy:

UmbracoHelper.AssignedContentItem.Descendants("NewsItem")

This code will recursively return the list of IPublishedContent nodes. If you wish to use this list with your specific DocumentType information, these items would have to be mapped to the specific type. Other than that, IPublishedContent gives you the basic information for the nodes.

I've later saw that you have been using an older version of Umbraco. :) This implementation is only for v7.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top