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:

3 Responses to To use @model or not, in umbraco Razor makroes

  1. Henri T says:

    Personally, i don’t like the dynamicnode stuff very much. It feels unpolished, the syntax can be weird, the names of properties can be weird, and without the cheatsheet i am just lost even though ive been using dynamicnode and razor for about a year.

    I prefer the static typing that comes with exporting the doctypes to .net and using a small class library with small methods that does the getting of the correct nodes. Then i can just do var items = new Menus().GetMainMenu(); And if something is broken, the compiler will tell me.

    The uQueryExtensions are like coffeescript, a way to hide something awkward with another abstraction layer.

  2. joeriks says:

    It’s nice with so many alternatives in Umbraco. I like uQuery, but for most daily navigation stuff I use the built in dynamicnode querying, .Where(“naviHide==false) http://our.umbraco.org/projects/developer-tools/razor-dynamicnode-cheat-sheet

    • maanehunden says:

      Brilliant,.. thanks for the link joeriks, this documentation is priceless. I hope that Henri T agrees with me .. (c:

Leave a reply to Henri T Cancel reply