Template Plugins

Template plugins extend the functionality within templates.

IPluginBlock

class portal.generic.plugin_interfaces.IPluginBlock

This allows extends {% pluginblock “myblock” “test” %} tag and add elements to a page. The arguments are passed through to the plugin

IPluginBlock plugins, are instantiated by the pluginblock template tag. The pluginblock {% pluginblock “myblock” “test” %} would try all the plugins with the name “myblock”, and pass through the variable “test” to them. Any returned string will be passed back through to the template. The return from the plugin should be text or HTML and is applied to the page. It should take two arguments, firstly the parameter that is sent through, this isaimed to match a string of the plugin’s name, secondly the complete list of arguments as *args

The name of the plugin should be equal to the first argument in the plugin tag so that its functionality will be called.

Requires:
  • __init__ method that defines a name for the plugin

  • args - arguments are sent through to the plugin so that you get template

    context, see which pluginblock is being called.

  • return_string method that returns the string that will be passed though to the template.

Returns:
  • return_string, a dictionary that will be rendered in place of {% pluginblock %}

The return_string should return a dictionary that is made up of a guid and a template or guid and a string.
  • GUID - should be unique string

  • template - reference to a template in the system as a relative filepath.

  • string - a string of text. This can either be just text, or include Django template tags.

See the example plugins for more information and implementation examples.

A simple example that would match {% pluginblock “myblock” %} and return the string “me” back into the page:

from portal.pluginbase.core import Plugin
from portal.utils.plugin_interfaces import IPluginBlock

class MyBlockPlugIn(Plugin):
    """ PLUGIN EXAMPLE
        This plugin will simply return the string "me" in return of a
        template tag {% pluginblock "myblock" %}
    """

    implements(IPluginBlock)

    def __init__(self):
        self.name = "myblock"

    def return_string(self, tagname, current_context, *args):
        return {'guid':'af3ab1dc-330d-11e1-a481-0bc5dac742d2',\
                'template':'me'}

pluginblock1 = MyBlockPlugIn()

This example would render a template ‘website/test.html’ using a context, and return the results back into the page:

from portal.pluginbase.core import Plugin
from portal.utils.plugin_interfaces import IPluginBlock

class MyOtherBlockPlugIn(Plugin):
    """ PLUGIN EXAMPLE
        This plugin will returns a template where there is
        the template tag {% pluginblock "myblockb" %}
    """

    implements(IPluginBlock)

    def __init__(self):
        self.name = "myblockb"

    def return_string(self, tagname, current_context, *args):
        return {'guid':'af3ab1dc-330d-11e1-a481-0bc5dac742d2',\
                'template':'website/test.html', \
                'context' : {'status' : 'This is a test'}}

pluginblock2 = MyOtherBlockPlugIn()

By using the two examples above you can inject content into pluginblock holders in the theme.

Please note that as of Cantemo 1.2 pluginblocks return_string will be sent the tagname - with information on which plugin block. And it will also take the current template context - information such as the currently logged in user, which page is being rendered and so forth. Take care that any error that you introduce here should die sliently.