Skip to main content

Extensible Template Plugins for Django

Project description

https://badge.fury.io/py/dj-n3-templateplugins.svg https://travis-ci.org/Jaredn/dj-n3-templateplugins.svg?branch=master https://codecov.io/gh/Jaredn/dj-n3-templateplugins/branch/master/graph/badge.svg

Extensible Template Plugins for Django

Documentation

The full documentation is at https://dj-n3-templateplugins.readthedocs.io.

Quickstart

Install django-n3-templateplugins:

pip install dj-n3-templateplugins

Add it to your INSTALLED_APPS:

INSTALLED_APPS = (
    ...
    'dj_n3_templateplugins.apps.DjN3TemplatepluginsConfig',
    ...
)

Features

If you want to build an extensible platform, you may find the need to allow the developers that use your app to alter or add on to the functionality that you provide. This django package attempts to add ‘plugin’ functionality to your django app.

To give an example of why I’m creating this… My company built an in-house django asset management system that we want to open-source, but there is a bunch of business-logic throughout the Templates and Views that wouldn’t make sense in an open-sourced app. So in order to open source our asset management system we needed to make our custom business-logic pluggable. Plus, anyone that used our asset management system would probably want a similar feature for their own business logic.

How it works:

End-Users of your app create a ‘plugin’, which is just a Python Class which requires two methods to be created:

# plugin.py
class Plugin(TemplatePlugin):
    def get_context_data():
        # How the plugin adds extra data to your Views/Templates
        return {'foo': 'some context'}

    def render_html():
        # How the plugin uses the context (and any provided by your view) to return HTML code (Full Django Template
        # syntax is allowed here!)
        return '<p><b>I am HTML</b> and I can use my context like this: {{ plugins.plugin_name.context.foo }}</p>'

These plugins get placed in the configurable directory:

# settings.py
N3PLUGINS_PLUGIN_DIR = '/some/path/with/plugins'

You then use the provided decorator @load_plugins to decorate your views like this:

# views.py
@load_plugins
class SomeView(DetailView):

This decorator will set a class property called self.plugins which is a dictionary. You then use this at any point in your view’s code and make sure it gets added to the Context object that is passed in for Template rendering.

In your template you then use django-n3-templateplugins provided templatetag to render the plugin’s html, which looks something like this:

# some_template.html
{% load render_plugin %}
{% for k, v in plugins.items %}
    {% render_plugin v.plugin_pbject %}
{% endfor %}

And that’s it!

  • TODO

Running Tests

Does the code actually work?

source <YOURVIRTUALENV>/bin/activate
(myenv) $ pip install tox
(myenv) $ tox

Credits

Tools used in rendering this package:

History

0.1.0 (2017-09-16)

  • First release on PyPI.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for dj-n3-templateplugins, version 0.2.0
Filename, size File type Python version Upload date Hashes
Filename, size dj-n3-templateplugins-0.2.0.tar.gz (8.6 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page