Is the current page a descendant of a specific node id?

How to test the use of a razor template in Umbraco to determine if the current page is a stream of a specific node? I will use case case.

+2
source share
3 answers

Not sure if these methods still work with the latest version of Umbraco, but with 4.7.1 and DynamicNode following methods are used:

 @Model.AncestorOrSelf(string nodeTypeAlias) @Model.AncestorOrSelf(int level) @Model.AncestorOrSelf(Func<DynamicNode, bool> func) 

and those auxiliary functions:

 @Model.IsDescendant(DynamicNode[,valueIfTrue][,valueIfFalse]) @Model.IsDescendantOrSelf(DynamicNode[,valueIfTrue][,valueIfFalse]) 
+3
source

If you use uComponents / uQuery from http://ucomponents.codeplex.com/ , you can do something like:

 var isChildOf = uQuery.GetCurrentNode().GetAncestorNodes().Where(n => n.NodeTypeAlias == "MyHomePage").First() != null; 

(Note: I have not tried this code, but had similar in production)

0
source

A quick and dirty way to do this is to check the node Path property (I think Model.Path should get it). This should contain either a single line of comma, or an array of numbers (not sure if from the top of my head) the path from node back to the root of the site. You can check your parent node in this property. This will save on more expensive node searches using LINQ or uQuery.

0
source

All Articles