Umbraco Razor Feature Walkthrough – Part 4

Tuesday, March 01, 2011 by Gareth Evans

Part 4 of the feature walkthrough continues with features available in the TAFKA 4.6.2 release.
Today, I'm covering .Where to filter nodes and .OrderBy to order nodes

RC1 is just around the corner and there's a bunch more features coming - i'll try and cover those too when they come out -
We've got more functions for navigating your tree, slightly better support for some .Where edge cases, .Where extension methods and more!

Where

Taking this implementation to the next level, we quickly found we wanted to select a random number of elements which should be visible but were still published.
An example is if you have a boolean property (defined as the True/False data type) on your document type that indicates if an item should be featured or not.

With DynamicObject (DynamicNode too, because it inherits from), the c# compiler / razor parser doesn't allow you to use the familiar lambda syntax to filter your sets.
This is because we now return a DynamicObject [DynamicNodeList] to allow method chaining.

This will not work in 4.6.1 or 4.7.

@Model.Children.Where(item => item.shouldBeFeatured);

//Lambda's cannot be used against Dynamic Objects


With the new 4.7 syntax, you can now use .Where to filter your nodesets.
To do this, we took the string parser from the 2008 linq samples and then added support for working with DynamicObjects

I saved the best for last!

A simplistic filter by a single boolean:

@Model.Children.Where("shouldBeFeatured")


A longhand filter, demonstrating the equality operator and type safety:

@Model.Children.Where("shouldBeFeatured == true")


Using NotEquals:

@Model.Children.Where("shouldBeFeatured != true")


Using GreaterThan against a numeric property

@Model.Children.Where("catCount > 1")


Using Modulus (to get the remainder of a number)
(will return any children where the number of cats is even)

@Model.Children.Where("catCount % 2 == 0") 


Using string comparisons and boolean logic (|| [or], && [and]) - also shows how to nest strings

@Model.Children.Where("menuType == \"Top Menu\ || menuType == \"Bottom Menu\"") 


Chaining into your own extension method
(will return 8 randomly selected nodes which are marked as featured items)

@Model.Children.Where("shouldBeFeatured").Random(8)


Passing Variables from outer scope into the .Where expression:
If you need to access a variable from outside the scope (e.g. a number etc) and don't want to pass it as a constant,

var maxLevelForSitemap = 4;
var values = new Dictionary<string,object>();
values.Add("maxLevelForSitemap", maxLevelForSitemap) ;        
var items = node.Children.Where("ShouldBeVisible == true && Level <= maxLevelForSitemap", values);


OrderBy

We have also added support for OrderBy, this lets you sort your nodesets by the properties on the nodes themselves.

Simplistic ordering by a single property:

@Model.Children.OrderBy("catCount")


More complex ordering by multiple properties, with descending/ascending support:

@Model.Children.OrderBy("catCount, colour desc")


Warning: There's a small bug in the 4.7 beta which means you can't order by a single descending column. This is fixed in RC1.

 

Conclusion

That pretty much wraps up my additions to Razor in 4.7beta, hopefully the new syntax improves your ability to work with razor to build websites with umbraco :)
Some more parts to come once the RC is released

I'm Gareth Evans, Follow me at @agrath on twitter, and here's a few links:
The new Razor forum on our.umbraco: http://our.umbraco.org/forum/developers/razor
Codeplex for any feature requests or bugs: http://umbraco.codeplex.com/

Read more from the Umbraco Razor walkthrough series

Part 1
Part 2
Part 3
Part 4
Part 5
Part 6
Part 7
Part 8

4 comment(s) for “Umbraco Razor Feature Walkthrough – Part 4”

  1. Gravatar ImageBiagio Paruolo Says:

    This link is white ... http://umbraco.com/follow-us/blog-archive/2011/2/23/umbraco-47-razor-feature-walkthrough---part-1

  2. Gravatar ImageDan Diplo Says:

    You can find all the blog posts in this series linked from Our Umbraco at:

    http://our.umbraco.org/wiki/reference/code-snippets/razor-snippets

  3. Gravatar ImageMurray Says:

    Note: using the values dictionary implies some type coercion that raw strings do not.
    I've found this particularly handy for dates,

    An example may help explain what I mean...

    This will work as expected:
    Dictionary();
    values.Add("startQuery", startQuery);
    @Model.Descendants("EventPage").Where("startDate > startQuery", values)

    and obviously this does not
    @Model.Descendants("EventPage").Where(string.Format("startDate > \"{0}\"" , startQuery)

  4. Gravatar ImageMurray Says:

    Do you have an example of how to use Descendants (Func func)

    I've asked this question on the forum here:
    http://our.umbraco.org/forum/developers/razor/25939-How-do-I-use-Descendants-(Func-INode,-bool-func)

Leave a comment