Skip to content

Instantly share code, notes, and snippets.

@mikeedwards83
Created September 26, 2012 20:59
Show Gist options
  • Select an option

  • Save mikeedwards83/3790551 to your computer and use it in GitHub Desktop.

Select an option

Save mikeedwards83/3790551 to your computer and use it in GitHub Desktop.
Loading RenderingReferences using Glass
// First create an attribute that specifies that the property should load the rendering refences
public class SitecoreRenderingsAttribute : AbstractSitecorePropertyAttribute
{
public Device Device { get; set; }
}
//I am using an enumeration to determine which device to load
public enum Device
{
Default,
Print
}
//then create a data handler to load the references
public class SitecoreRenderingsHandler :AbstractSitecoreDataHandler
{
public override bool WillHandle(Configuration.SitecoreProperty property, IEnumerable<AbstractSitecoreDataHandler> datas, Dictionary<Type, Configuration.SitecoreClassConfig> classes)
{
return property.Attribute is SitecoreRenderingsAttribute &&
property.Property == typeof (IEnumerable<RenderingReference>);
}
public override object GetValue(global::Sitecore.Data.Items.Item item, ISitecoreService service)
{
//you may want to lazy load this
var device = item.Database.GetItem("/sitecore/layout/Devices/" + Device.ToString());
return item.Visualization.GetRenderings(new DeviceItem(device), false);
}
public override void SetValue(global::Sitecore.Data.Items.Item item, object value, ISitecoreService service)
{
throw new NotImplementedException();
}
public override bool CanSetValue
{
get { return false; }
}
public override void ConfigureDataHandler(Configuration.SitecoreProperty scProperty)
{
var attr = scProperty.Attribute as SitecoreRenderingsAttribute;
this.Device = attr.Device;
base.ConfigureDataHandler(scProperty);
}
public Device Device { get; set; }
}
//configure class to use the data handler
AttributeConfigurationLoader loader = new AttributeConfigurationLoader("Demo.Models, Demo");
loader.AddDataHandler(new SitecoreRenderingsHandler());
//configure the property on a class
[SitecoreClass]
public class MyModel{
[SitecoreRenderings(Device=Device.Print)]
public IEnumerable<RenderingReference> Renderings { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment