Simpler CMS Plugins #1373

ojii opened this Issue Aug 6, 2012 · 2 comments


None yet
2 participants

ojii commented Aug 6, 2012

For a project I just wrote this base plugin and arguably, it's behavior (not this code) should be the default:

# -*- coding: utf-8 -*-
from cms.plugin_base import CMSPluginBase, CMSPluginBaseMetaclass
import re

class BasePluginMeta(CMSPluginBaseMetaclass):
    def __new__(cls, name, bases, attrs):
        super_new = super(BasePluginMeta, cls).__new__
        parents = [base for base in bases if isinstance(base, BasePluginMeta)]
        klass = super_new(cls, name, bases, attrs)
        if not parents:
            return klass = re.sub("([a-z])([A-Z])","\g<1> \g<2>", name)
        return klass

class BasePlugin(CMSPluginBase):
    __metaclass__ = BasePluginMeta

    admin_preview = False

    def render(self, context, instance, placeholder):
        context['instance'] = instance
        return context

What this does is automatically setting the plugin name to the class name (inserting spaces, so HelloWorld becomes Hello World. It defines a default render method which just sets the 'instance' variable in the context. And it disables admin preview because admin preview is hardly ever useful.

Note that the code above is a little specific and always applies the name patch, this would obviously be conditional.


digi604 commented Aug 7, 2012

Where is the plugin name used? Will it need to be translatable?


ojii commented Aug 7, 2012

No in this case not, and this would only be the default case of course. I don't want to remove the name attribute.

@ojii ojii closed this in 3ebfde4 Oct 11, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment