11

As a test, I wanted to create a rendering for an SXA site. It had to be added to the toolbox and have a controller, model, and view.

I read this Creating new renderings that are compatible with SXA and now I am somehow confused as in that answer only the controller and rendering parameters are mentioned and I remember this slide from the Sitecore Symposium:

enter image description here

Here they also mentioned:

  • adding a div to my view
  • implementing IRenderingModelBase for the model

So now my questions are:

  1. Do I need to do add this div to the view and what if I don't?
  2. Do I need to implement that interface and what will I benefit (or what will I miss if I don't)?
  3. If I have a custom model that implements this interface, what is the best way to fill all the properties involved? I found a GetModel() in the controller and a ModelRepository that might help but then I need to copy the values..

2 Answers 2

16
  1. You should. Here are few things that you should know.

    Without this div element your rendering will not be SXA compliant.

    If you decide to skip it you will lose following features/abilities:

    • your rendering will not be able to use "SXA styles". This div is the main wrapper and inside class attribute other classes which can be specified in Rendering Parameters will be injected (if your rendering parameters inherit from /sitecore/templates/Foundation/Experience Accelerator/Presentation/Rendering Parameters/IStyling).
    • your rendering will not be recognised by Creative Exchange. Bear in mind that if you are going to allow designers to export your theme someone can add additional CSS classes and CE will not be able to import it then.
    • if you decide to write a rendering with JS logic it is easier to register your JS logic (XA.register('accordions', XA.component.accordions);). You don't have to worry about loading order. You just implement 'interface' and your rendering will be initialized when it will be possible.
  2. You don't have to. I would say you will never implement this interface directly. What you can do is to use existing RenderingModelBase which implements IRenderingModelBase and derive from it. This is how it should be done and how it is done in SXA. To see whare are the benefits of using that model base class go to Appendix 1 to learn more.

  3. See how it is filled in FacebookCommentsRepository. There is FillBaseProperties(model); method which comes from Sitecore.XA.Foundation.Mvc.Repositories.Base.ModelRepository

public override IRenderingModelBase GetModel()
{
    FacebookCommentsRenderingModel model = new FacebookCommentsRenderingModel();
    FillBaseProperties(model);
    model.DataHref = GetDataHref();
    model.PostsCount = GetPostsCount();
    model.ColorScheme = GetColorScheme();
    return model;
}

This is how the new simple model could look like. As you can see it derive from RenderingModelBase and decorate type with 3 tiny properties.

public class FacebookCommentsRenderingModel : RenderingModelBase
{
    public string DataHref { get; set; }
    public string PostsCount { get; set; }
    public string ColorScheme { get; set; }
}

Appendix 1

RenderingModelBase class contains:

public Item Item { get; set; }
public Item PageItem { get; set; }
public IRendering Rendering { get; set; }
public Item DataSourceItem { set; get; }
public IEnumerable<string> CssClasses { get; set; }
public MvcHtmlString MessageEditHere { get; set; }
public MvcHtmlString MessageIsEmpty { get; set; }
public string HeadingTag { get; set; }
public bool IsControlEditable { get; set; }
public bool IsEdit { get; set; }
public RenderingWebEditingParams RenderingWebEditingParams { get; set; }
public Dictionary<string, List<string>> Attributes { get; set; }

I will not describe every property, but some interesting are:

  • IsEditable - invokes getControlEditability pipeline and determine whether your control can be editable. Used to display rendering in a different way in different context

  • MessageEditHere - this is an auto-formatted string that can be returned to indicate that rendering should be edited HERE. Sometimes your rendering may return nothing due to lack of data source or some data. This is s string that should be displayed to make rendering still visible on the page

  • MessageIsEmpty- this is auto formatted string that can be returned to indicate that rendering has no data source

  • CssClasses - collection of css classess which I described in (1)

  • Rendering - your current rendering item

  • DataSourceItem - automatically resolved data source item of your rendering (Datasource from rendering parameters)

1

This is an incomplete answer to start the discussion, but just from what I've found:

The div and the @Model.CssClasses.Aggregate() seems to add all of the classes for variants, grid sizing, and other options selected in experience editor to the rendered HTML. I think it would be wise to make sure that's there, from my experimentation, but I have successfully added other renderings I've created before to a page in SXA without this.

For example, when rendered with a carousel with a full bleed image in it, vs the view code: enter image description here

The interface, decompiled, shows these properties:

public interface IRenderingModelBase
{
  // Properties
  IEnumerable<string> CssClasses { get; set; }
  Item DataSourceItem { get; set; }
  string HeadingTag { get; set; }
  bool IsControlEditable { get; set; }
  bool IsEdit { get; set; }
  Item Item { get; set; }
  MvcHtmlString MessageEditHere { get; set; }
  MvcHtmlString MessageIsEmpty { get; set; }
  Item PageItem { get; set; }
  IRendering Rendering { get; set; }
  RenderingWebEditingParams RenderingWebEditingParams { get; set; }
}

Some of those feel obvious, but again, not sure what you're gaining with some of them, either.

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.