This package can be used to parse out Portal Engine widgets from Rich Text fields.
Install
Nuget Package Manager
Kentico 12 MVC is amazing. Its a huge step forward for a CMS that has so much power and provides so much customisation ability to developers. That said, the forms have caused me some headaches over.
Usage
Dependency Injection
Autofac
If you're using AutoFac then the following will suffice...
Other DI
Within your dependency injection, ensure that the following interfaces are registered to their corresponding classes. They should all be registered as singletons.
IRichTextResolver
=RichTextResolver
IWidgetResolver
=WidgetResolver
IWidgetRegistry
=WidgetRegistry
Configuration
Create your widgets in Kentico as normal. The properties you create do not need to be represented on the webpart that the widget is based upon.
Remember that this package will only give you access to the widgets property values, not any WebForms UserControl functionality.
In your MVC application create a model class that has the same properties as you created for the widget in Kentico. Make this model class implement the interface
IWidgetmodel
from this package.IWidgetRegistry.RegisterWidget<YourWidgetModel>(string YourWidgetCodeName)
needs to be called to register each widget that you want to use. There are two optional parameters for this method that allow you to specify action delegates that can be used to perform additional logic before or after widget resolution.The
beforeAction
returns a JObject
and is passed a JObject
of the parsed widget properties.The afterAction returns, and is passed, an object of your widget model.
For example
Autofac
If you're using Autofac you can register your widgets by creating a new class that implements
IStartable
from the Autofac package and inject into its constructor the IWidgetRegistry
.Register your widgets with the WidgetRegistry as follows within the
Start()
method.Then in your dependency injection config register the
IStartable
:Resolving Richtext
In your ViewModel set the rich-text property to type
IRichTextData
.Inject
IRichTextResolver
into your controller.When you retrieve the
string
property from the Rich Text property of the Kentico object/document, call RichTextresolver.Resolve(string text)
on it.In your views create a view at
~/Views/Shared/DisplayTemplates/YourWidgetModelName.cshtml
that accepts your widget model as its ViewModel and renders the widget data as you desire.Important: If you wish to control how the HTML portions of the RichText are rendered (such as calling ResolveUrls on it) then you must also create a DisplayTemplate view called
RichTextHtml.cshtml
In the view that your controller returns, to render the
IRichTextData
property of the ViewModel using DisplayFor
:This will automatically render the RichText using the display templates.