To use @model or not, in umbraco Razor makroes

Making navigation makroes is allays fun. And using the latest tools like MVC makes is even more exiting. I’ve been using XSLT for this kind of tasks like forever and you can almost say since the beginning of my web developer days.  I’ve tried experimenting with facing out XSLT by using repeaters and user- controls, but i allays ended up using Xpath to getting the things done, a  hard habit to break  (c:
In my recent projects I’ve started exploring the new and exiting Razor universe.  With it’s own challengers ,. like this one..

I wanted to get all the  unhidden first level nodes,

var home = @Model.AncestorOrSelf(2); 
var subItems = home.Children();
@foreach (DynamicNode item in subItems)
{
  //i've made a helper method to get values from nodes
string naviHideValue = umbracoExtensions.GetValueFromNode(item ,"naviHide");
if( naviHideValue )
{
//Do stuff here
  } 
}

That is very cumbersome and you have to traverse the nodes to filter them out.  I wanted to filter out the unwanted nodes before the for loop but that was not possible,  you just cant use lambda expression  on dynamic nodes, and i didn’t want to cast all my dynamic nodes,.. they are after all dynamic  (c: So dont be suprised if you get one of these “Error 237 Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type ” bad boys.

The solution came in a form of “uComponents.Core.uQueryExtensions”. the uComponents package is a must, i really approve those guise so many useful datatypes.. Well back to the subject  uComponents  is a topic of its own, that i promise  i’ll blog about in the future.

@using uComponents.Core
@using uComponents.Core.uQueryExtensions

@inherits DynamicNodeContext
@{

var homePage = uQuery.GetCurrentNode().GetAncestorOrSelfNodes().FirstOrDefault(node => node.Level() == 1);

var subItems = homePage.GetChildNodes().Where(node => node.GetPropertyAsBoolean("naviHide")); 
  @foreach (var item in subItems)
  {
    //do your thing
  }
}

so @model gets you the current node but i lack my Linq, and i did start writing extension methods before i found about the “uComponents.Core.uQueryExtensions” methods anyway. So until i learn otherwise i will use uQueryExtensions and linq for my filtering.

happy programming (c:

Advertisements