Contentstack Modular Blocks Models Example

I want to have a type of page (maybe like a blog, but much more controlled and strongly typed) that lets me add a bunch of different kinds (blocks) of content, where each of those things lets me specify which partial view to render their data. I could use any rendering technology; I just want to manage some metadata for it in the CMS to avoid hard-coding.

I want to be able to put these different types of blocks of content together into a page. Some blocks may be Rich Text, others markdown, etc. For now, that page is just a sequential list. I made the mistake of calling these things Elements.

The first thing I need is an enum to identify the types of blocks.

Then I need a base class for the Block Models, which exposes one property of that enum. Notice how this derives from, specifying its own type and the enum type as its generic parameters.

Then I need my Block Model classes that inherit from that base class to model the various types of blocks and set the enum (honestly I am not sure how best to implement that property in the base class or implementing classes):

To make it reusable across Content Types, I implemented this as a Contentstack Global Group (hence the namespace) for a Modular Blocks Field named Members in the Content Type. To use it, add a property of this type named after the field to the Entry Model.

As with the enum, I am not sure of the best C# technique to implement the PartialView property override. I want implementations to specify both.

Just noting that I want to maximize code-reuse, avoid hard-coding, and so forth. So all of my Entries can reuse a single view and it would be easier to reuse a partial view than implement a new one, but I still want developers and CMS users to be able to select views and partial views.

I already described how to register the JsonConverter: We still need to implement a repository abstraction and configure dependency injection, which is where that should occur.

One thought on “Contentstack Modular Blocks Models Example

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: