Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I determine the current pages document type in umbraco?

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.

like image 291
shawty Avatar asked May 26 '13 14:05

shawty


People also ask

What is a document type in umbraco?

A Document Type is a data container in Umbraco where you can add Properties (data fields/attributes) to input data. Each Property has a Data Type like text string, number, or rich text body. Umbraco outputs the input data using Templates.

How do I change the file type in umbraco?

Right clicking on the page name and selecting Change Document Type on the context menu will open up a new fly out pane. On this pane you will see options to select, the new type and the new template.


3 Answers

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.

like image 199
denford mutseriwa Avatar answered Oct 23 '22 17:10

denford mutseriwa


In Umbraco 7 use currentPageNode.DocumentTypeAlias

like image 41
user3887964 Avatar answered Oct 23 '22 17:10

user3887964


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

like image 39
Leszek P Avatar answered Oct 23 '22 18:10

Leszek P