Skip to content

Instantly share code, notes, and snippets.

@joeriks
Created March 23, 2011 02:33
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joeriks/882512 to your computer and use it in GitHub Desktop.
Save joeriks/882512 to your computer and use it in GitHub Desktop.
Umbraco DynamcNode Extensions
///
/// This is a first attempt to make some useful extension methods to dynamic node and dynamic node list
/// Comments and collab is more than welcome!
///
/// Ref (Gareth Evans):
/// http://umbraco.com/follow-us/blog-archive/2011/2/28/umbraco-razor-feature-walkthrough-%E2%80%93-part-3
///
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using umbraco.MacroEngines;
namespace dncollab
{
public static class RazorExtensions
{
/// <summary>
/// Return PropertyValue as DateTime, if property does not exist or
/// is not a date the function will return DateTime.MinValue
/// </summary>
/// <param name="dn"></param>
/// <param name="propertyName"></param>
/// <returns></returns>
public static DateTime PropertyAsDateTime(this DynamicNode dn, string propertyName)
{
var property = dn.GetProperty(propertyName);
if (property==null) return DateTime.MinValue;
DateTime dt = DateTime.MinValue;
DateTime.TryParse(property.Value.ToString(), out dt);
return dt;
}
/// <summary>
/// Return PropertyValue as String, if property does not exist or
/// is null the function will return empty string
/// </summary>
/// <param name="dn"></param>
/// <param name="propertyName"></param>
/// <returns></returns>
public static string PropertyAsString(this DynamicNode dn, string propertyName)
{
var property = dn.GetProperty(propertyName);
if (property == null) return "";
return property.Value.ToString();
}
/// <summary>
///
/// </summary>
/// <param name="dn"></param>
/// <param name="propertyName"></param>
/// <returns></returns>
public static bool PropertyNotEmpty(DynamicNode dn, string propertyName)
{
var property = dn.GetProperty(propertyName);
return ((property!=null) && property.Value.ToString() != "");
}
/// <summary>
/// Gets first AncestorOrSelf with a non empty property of name propertyName
/// </summary>
/// <param name="dn"></param>
/// <param name="propertyName"></param>
/// <returns></returns>
public static DynamicNode AncestorOrSelfNotEmptyProperty(this DynamicNode dn, string propertyName)
{
dynamic n = dn;
while (!PropertyNotEmpty(n, propertyName) && n.Parent != null) { n = n.Parent; }
if (PropertyNotEmpty(n, propertyName))
return n;
else
return null;
}
/// <summary>
/// Gets property value recursively
/// @{current = (umbraco.MacroEngines.DynamicNode)Model;}
/// <h1>Site Name: @current.PropertyValueRecursive("siteName")</h1>
///
/// </summary>
/// <param name="dn"></param>
/// <param name="propertyName"></param>
/// <returns></returns>
public static string PropertyAsStringRecursive(this DynamicNode dn, string propertyName)
{
dynamic n = dn.AncestorOrSelfNotEmptyProperty(propertyName);
if (n != null)
return n.GetProperty(propertyName).Value.ToString();
else
return "";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment