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 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.
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):
- Code Block Model: https://pastebin.com/rwLBJjT6
- Code Link Block Model: https://pastebin.com/P3TwNVrx
- Download Block Model: https://pastebin.com/7V5TH8E7
- Image Block Model: https://pastebin.com/yabxEwkf
- Markdown Block Model: https://pastebin.com/Vmr0nqf4 uses https://pastebin.com/7e36z1d1
- Markup Block Model: https://pastebin.com/9W5EACp6 uses https://pastebin.com/t5F0naKg
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: 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.
One thought on “Contentstack Modular Blocks Models Example”