Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Simpler CMS Plugins #1373

Closed
ojii opened this Issue · 2 comments

2 participants

@ojii
Collaborator

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
        klass.name = 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
Collaborator

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

@ojii
Collaborator

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 issue from a commit
@ojii ojii Saner defaults for plugins
* Fixes #1373.
* The default name for plugins is now nicer.
* The render method of plugins now by default adds.
  the instance and placeholder to the context and
  returns it, eliminating the need to overwrite this
  method in simple plugins.
3ebfde4
@ojii ojii closed this in 3ebfde4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.