2

I'm looking for best practices to implement a Controller Rendering using glass mapper and TDS code-generation.

I've read and liked this approach https://www.akshaysura.com/2016/08/01/tihidi-implement-a-simple-controller-rendering-in-sitecore-mvc/ but I'm wondering if GlassController has a native way to access the rendering context as Sitecore API does with RenderingContext.Current.Rendering.Item

How are others creating the Controller Renderings using glass?

2 Answers 2

4

If your controller inherits fromt the GlassController<T> then you have access to several properties and methods to access either the context item or the datasource item.

Item ContextItem
Item DataSourceItem
Item LayoutItem

ContextItem just returns Sitecore.Context.Item.

DataSourceItem returns the specified data source item.

LayoutItem returns the data source item if it isn't null, otherwise the context item.

Besides those you have the following matching methods and properties. The properties casts to the type parameter specified in GlassController<T> while you can specify the type for the methods.

// Properties
T Context
T DataSource
T Layout

// Methods
T GetContextItem<T>()
T GetDataSourceItem<T>()
T GetLayoutItem<T>()

GlassController<T> also has a RenderingContextWrapper property where you have access to the current data source value and rendering parameters.

You of course also have access to the SitecoreContext property.

3
  • Great explanation
    – Melvinr
    Nov 24, 2016 at 16:04
  • Great! Just wanted know, whether you have code snippet for unit test case using GlassController ?
    – PaRsH
    Dec 2, 2016 at 9:17
  • @PaRsH Normally you shouldn't have logic worth unit testing in your controllers, and you should probably create a separate question for this. Dec 2, 2016 at 9:24
3

In the example below, I am using DI to new up all the Glass Mapper glasses. We are using SitecoreContext and GlasHtml in our repositories to do the work.

Controller

private readonly ICartRepository _cartRepository;

public CartController(ICartRepository cartRepository)
{
        _cartRepository = cartRepository;
}

public ActionResult MiniCart()
{
      try
      {
          var model = _cartRepository.GetHeaderMiniCart();

          return View(model);
      }
      catch (Exception e)
      {
          return IsPageEditorEditing ? ErrorView(e) : View(new MiniCart());
      }
}

Repository

private readonly ISitecoreContext _sitecoreContext;
private readonly IGlassHtml _glassHtml;
private readonly IRenderingService _renderingService;

public CartRepository(ISitecoreContext sitecoreContext, IGlassHtml glassHtml, IRenderingService renderingService)
{
      _sitecoreContext = sitecoreContext;
      _glassHtml = glassHtml;
      _renderingService = renderingService;
}

Data function, returns editable HTML so we can edit it as if it were a rendering model.

public MiniCart GetHeaderMiniCart()
{
        var model = renderingService.GetGlassModelFromDataSource<Header>();

        return new MiniCart
        {
            Icon = _glassHtml.Editable(model, x => x.CartImage),
            Link = _glassHtml.Editable(model, x => x.CartButtonLink),
            Count = 2
        };
}

The Render Service Helper interface and class.

public interface IRenderingService
{
    /// <summary>
    /// Get a glass model from the item data source
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <returns>T</returns>
    T GetGlassModelFromDataSource<T>() where T : GlassBase;
}


public class RenderingService : IRenderingService
{
    /// <summary>
    /// Get a glass model from the item data source
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <returns>T</returns>
    public T GetGlassModelFromDataSource<T>() where T : GlassBase
    {
        if (string.IsNullOrEmpty(RenderingContext.Current.Rendering.DataSource))
        {
            throw new MissingMemberException("A rendering must have a datasource.");
        }

        return RenderingContext.Current.Rendering.Item.GlassCast<T>();
    }
}

The model

public class MiniCart
{
     public string Icon {get; set;}
     public string Link {get; set;}
     public int Count {get; set;}
}

Then in the view

@model MiniCart

My Link @Html.Raw(Model.Link)
My Icon @Html.Raw(Model.Icon)
3
  • Where does the "IRenderingService" come from?
    – Melvinr
    Nov 23, 2016 at 17:20
  • Sorry forgot that helper class. Added.
    – Chris Auer
    Nov 23, 2016 at 17:33
  • And if you don't want your items editable, you can use make minicart take values and not _glassHtml.Editable text.
    – Chris Auer
    Nov 23, 2016 at 17:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.