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.

https://pastebin.com/V1i01tek

Then I need a base class for the Block Models, which exposes one property of that enum. Notice how this derives from https://deliverystack.net/2020/07/23/generic-base-class-with-jsonconverter-for-contentstack-block-model-classes/, specifying its own type and the enum type as its generic parameters.

https://pastebin.com/93U076ye

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.

https://pastebin.com/TS4wFEEh

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: https://deliverystack.net/2020/07/23/generic-base-class-with-jsonconverter-for-contentstack-block-model-classes/. We still need to implement a repository abstraction and configure dependency injection, which is where that should occur.

Published by John West

I like Twizzlers and um

One thought on “Contentstack Modular Blocks Models Example

Leave a Reply

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

WordPress.com Logo

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

Google photo

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

Twitter picture

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

Facebook photo

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

Connecting to %s

%d bloggers like this: