lektor.types.Type (env, options)

  • New in Lektor Version 2.0

The fields in Records use types to specify the behavior of the values. Lektor comes with a wide range of built-in field types but it is possible to build your own by subclassing types class. A type is instantiated with two parameters: a reference to the Environment that it belongs to and a dictionary with configuration options from the ini file.

A field type has to implement the value_from_raw method and set the widget property as a very basic requirement.

To create a type you need to create a subclass. The name of the class needs to match the type name. If you want to name your type mything then it needs to be called MyThingType. Afterwards you can register it with the environment in setup_env:

from lektor.types.primitives import SingleInputType

class MyThingType(SingleInputType):
    widget = 'singleline-text'

    def value_from_raw(self, raw):
        return raw.value

def setup_env(self, **extra):
    self.env.add_type(MyThingType)

In the above example, the new type declares a base class of SingleInputType. This is appropriate for types that use a single-line input widget in the admin UI — it enables the use of the addon_label field option. Custom types that use more general input types should instead inherit from lektor.types.base.Type.

For more information see value_from_raw.

There is a more complete example in the Plugin How To.

Comments