This blog post will go through how I setup a basic Controller rendering using Glass Mapper Model.

Visit http://www.glass.lu/mapper/sc for more information on Glass Mapper for Sitecore.

TIHIDI: Stands for This Is How I Do It. I am going to write a series of blog posts going through how I do Sitecore related work. Hope it helps you!

We are going to use the same infrastructure as my previous post TIHIDI: Implement a simple view rendering in Sitecore MVC and Glass.

Keeping the model as is, we need to add the code to render this via a Controller Rendering. This functionality really does not need a controller rendering as we were able to do this in a view rendering but I wanted to give you an example.

Before we get into controllers, we need to setup a couple of things related to Glass and Business Logic.

I setup a ControllerSCContext similar to Nathanael Mann (the Mann). You can read about it further here. Why do I do it this way? I do not know, I was taught by the master(s) to do it this way. If there is a more efficient way to do it, please send me links.

using Glass.Mapper.Sc; namespace TIHIDI.Business.GlassSC { public interface IControllerSCContext : ISitecoreContext { T GetDataSource<T>() where T : class; T GetRenderingParameters<T>() where T : class; } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 using Glass . Mapper . Sc ; namespace TIHIDI . Business . GlassSC { public interface IControllerSCContext : ISitecoreContext { T GetDataSource <T> () where T : class; T GetRenderingParameters <T> ( ) where T : class ; } }

using Glass.Mapper.Sc; using Sitecore.Mvc.Presentation; using System; namespace TIHIDI.Business.GlassSC { public class ControllerSCContext: SitecoreContext, IControllerSCContext { private IGlassHtml _glassHtml; public ControllerSCContext(IGlassHtml glassHtml) { _glassHtml = glassHtml; } public T GetRenderingParameters<T>() where T : class { if (RenderingContext.CurrentOrNull != null) { string parameters = RenderingContext.CurrentOrNull.Rendering["Parameters"]; if (String.IsNullOrEmpty(parameters)) { return _glassHtml.GetRenderingParameters<T>(parameters); } } return default(T); } public T GetDataSource<T>() where T : class { string dataSource = RenderingContext.CurrentOrNull.Rendering.DataSource; if (String.IsNullOrEmpty(dataSource)) { return default(T); } Guid dataSourceId; return Guid.TryParse(dataSource, out dataSourceId) ? GetItem<T>(dataSourceId) : GetItem<T>(dataSource); } /// <summary> /// if the rendering context and data source has been set then returns the data source item, otherwise returns the context item. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="isLazy"></param> /// <param name="inferType"></param> /// <returns></returns> public T GetControllerItem<T>(bool isLazy = false, bool inferType = false) where T : class { T renderingItem; if (RenderingContext.Current == null || RenderingContext.Current.Rendering == null || string.IsNullOrEmpty(RenderingContext.Current.Rendering.DataSource)) { return GetCurrentItem<T>(false, false); } try { renderingItem = this.GetRenderingItem<T>(isLazy, inferType); } catch (InvalidOperationException invalidOperationException) { renderingItem = GetCurrentItem<T>(false, false); } return renderingItem; } /// <summary> /// Returns the data source item. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="isLazy"></param> /// <param name="inferType"></param> /// <returns></returns> public virtual T GetRenderingItem<T>(bool isLazy = false, bool inferType = false) where T : class { if (RenderingContext.Current == null || RenderingContext.Current.Rendering == null || string.IsNullOrEmpty(RenderingContext.Current.Rendering.DataSource)) { return default(T); } return GetItem<T>(RenderingContext.Current.Rendering.DataSource, isLazy, inferType); } } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 using Glass . Mapper . Sc ; using Sitecore . Mvc . Presentation ; using System ; namespace TIHIDI . Business . GlassSC { public class ControllerSCContext : SitecoreContext , IControllerSCContext { private IGlassHtml _ glassHtml ; public ControllerSCContext ( IGlassHtml glassHtml ) { _ glassHtml = glassHtml ; } public T GetRenderingParameters <T> () where T : class { if (RenderingContext.CurrentOrNull != null) { string parameters = RenderingContext.CurrentOrNull.Rendering["Parameters"]; if (String.IsNullOrEmpty(parameters)) { return _glassHtml.GetRenderingParameters <T> (parameters); } } return default(T); } public T GetDataSource <T> () where T : class { string dataSource = RenderingContext.CurrentOrNull.Rendering.DataSource; if (String.IsNullOrEmpty(dataSource)) { return default(T); } Guid dataSourceId; return Guid.TryParse(dataSource, out dataSourceId) ? GetItem <T> (dataSourceId) : GetItem <T> (dataSource); } /// <summary> /// if the rendering context and data source has been set then returns the data source item, otherwise returns the context item. /// </summary> /// <typeparam name = "T" > </typeparam> /// <param name = "isLazy" > </param> /// <param name = "inferType" > </param> /// <returns> </returns> public T GetControllerItem <T> (bool isLazy = false, bool inferType = false) where T : class { T renderingItem; if (RenderingContext.Current == null || RenderingContext.Current.Rendering == null || string.IsNullOrEmpty(RenderingContext.Current.Rendering.DataSource)) { return GetCurrentItem <T> (false, false); } try { renderingItem = this.GetRenderingItem <T> (isLazy, inferType); } catch (InvalidOperationException invalidOperationException) { renderingItem = GetCurrentItem <T> (false, false); } return renderingItem; } /// <summary> /// Returns the data source item. /// </summary> /// <typeparam name = "T" > </typeparam> /// <param name = "isLazy" > </param> /// <param name = "inferType" > </param> /// <returns> </returns> public virtual T GetRenderingItem <T> (bool isLazy = false, bool inferType = false) where T : class { if (RenderingContext.Current == null || RenderingContext.Current.Rendering == null || string.IsNullOrEmpty(RenderingContext.Current.Rendering.DataSource)) { return default(T); } return GetItem <T> ( RenderingContext . Current . Rendering . DataSource , isLazy , inferType ) ; } } }

Once that is done, I setup my business logic code.

using TIHIDI.Models.Templates.Content; namespace TIHIDI.Business.Content { public interface IContentLogic { ITextBlock GetTextBlock(); } } 1 2 3 4 5 6 7 8 9 10 11 12 using TIHIDI . Models . Templates . Content ; namespace TIHIDI . Business . Content { public interface IContentLogic { ITextBlock GetTextBlock ( ) ; } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using TIHIDI.Business.GlassSC; using TIHIDI.Models.Templates.Content; namespace TIHIDI.Business.Content { public class ContentLogic : IContentLogic { private readonly IControllerSCContext _currentContext; public ContentLogic(IControllerSCContext currentContext) { _currentContext = currentContext; } public ITextBlock GetTextBlock() { return _currentContext.GetDataSource<ITextBlock>(); } } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 using System ; using System . Collections . Generic ; using System . Linq ; using System . Text ; using System . Threading . Tasks ; using TIHIDI . Business . GlassSC ; using TIHIDI . Models . Templates . Content ; namespace TIHIDI . Business . Content { public class ContentLogic : IContentLogic { private readonly IControllerSCContext _ currentContext ; public ContentLogic ( IControllerSCContext currentContext ) { _ currentContext = currentContext ; } public ITextBlock GetTextBlock ( ) { return _ currentContext . GetDataSource <ITextBlock> ( ) ; } } }

I know the code is light but this shows you where the logic part of the code is supposed to be. I like my controllers as lean as possible. The next step is to define a controller.

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using TIHIDI.Business.Content; namespace TIHIDI.Web.Controllers { public class ContentController : Controller { private readonly IContentLogic _contentLogic; public ContentController(IContentLogic contentLogic) { _contentLogic = contentLogic; } public ActionResult RenderTextBlock() { return View(_contentLogic.GetTextBlock()); } } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 using System ; using System . Collections . Generic ; using System . Linq ; using System . Web ; using System . Web . Mvc ; using TIHIDI . Business . Content ; namespace TIHIDI . Web . Controllers { public class ContentController : Controller { private readonly IContentLogic _ contentLogic ; public ContentController ( IContentLogic contentLogic ) { _ contentLogic = contentLogic ; } public ActionResult RenderTextBlock ( ) { return View ( _ contentLogic . GetTextBlock ( ) ) ; } } }

We also need to register our new interfaces with Simple Injector.

container.Register<IControllerSCContext>(()=> new ControllerSCContext(container.GetInstance<IGlassHtml>())); container.Register<IContentLogic, ContentLogic>(); 1 2 3 4 container . Register <IControllerSCContext> ( ( ) = > new ControllerSCContext(container.GetInstance <IGlassHtml> ())); container.Register <IContentLogic, ContentLogic > ( ) ;

Once that is done, its time to just copy our view rendering code since its going to be exactly the same. Create a new view under Views\Content.

@inherits Glass.Mapper.Sc.Web.Mvc.GlassView<TIHIDI.Models.Templates.Content.ITextBlock> @using Sitecore.Mvc @if (Model != null) { <h3>@Editable(Model, y => y.Heading)</h3> <h4>@Editable(Model, y => y.SubHeading)</h4> <div class="content">@Editable(Model, y => y.Content)</div> } 1 2 3 4 5 6 7 8 9 10 11 12 @ inherits Glass . Mapper . Sc . Web . Mvc . GlassView <TIHIDI.Models.Templates.Content.ITextBlock> @using Sitecore.Mvc @if (Model != null) { <h3> @ Editable ( Model , y = > y.Heading) </h3> <h4> @ Editable ( Model , y = > y.SubHeading) </h4> <div class = "content" > @ Editable ( Model , y = > y.Content) </div> }

Now that its all wired up, lets build and publish.

Setup a controller rendering in Sitecore to map this new controller.



Modify your item’s presentation to include this new rendering and fire it up.





If you have any questions or concerns, please get in touch with me. (@akshaysura13 on twitter or on Slack).