Generic Fields

Last Updated: 2019-10-11

If you want to read more about the fields included in Piranha, please refer to Standard Fields.

Generic fields are custom fields that are built around Generic Types. These fields work in the same way as custom field but has a number of extra rules to them.

Create a Base Class

A generic field must have a non-generic base class. This type will be used when resolving the name of the EditorTemplate. For example:

public abstract class MyGenericFieldBase : Piranha.Extend.IField
{
...
}

[Field(Name = "My Generic")]
public class MyGenericField<T> : MyGenericFieldBase
{
...
}

Create The EditorTemplate

Next up is creating the EditorTemplate that should render the field. The template should be located in:

~/Areas/Manager/Views/Shared/EditorTemplates/

The name should be the same as the base class of your generic field, in this example it would be MyGenericFieldBase.cshtml.

@model MyNamespace.MyCoolFieldBase

<div>Some HTML code here</div>

Register The Field

Since field registration is type based you need to register each use of your generic field, for example:

App.Fields.Register<MyGenericField<string>>();
App.Fields.Register<MyGenericField<int>>();