Renderer (feincms3.renderer)

class feincms3.renderer.TemplatePluginRenderer[source]

This renderer allows registering functions, templates and context providers for plugins. It also supports rendering plugins’ templates using the rendering context of the surrounding template without explicitly copying required values into the local rendering context.

plugins()[source]

Return a list of all registered plugins, and is most useful when passed directly to one of django-content-editor’s contents utilities:

page = get_object_or_404(Page, ...)
contents = contents_for_item(page, renderer.plugins())
register_string_renderer(plugin, renderer)[source]

Register a rendering function which is passed the plugin instance and returns a HTML string:

renderer.register_string_renderer(
    RichText,
    lambda plugin: mark_safe(plugin.text),
)
register_template_renderer(self, plugin, template_name, context=default_context)[source]

Register a renderer for plugin using a template. The template uses the same mechanism as {% include %} meaning that the full template context is available to the plugin renderer.

template_name can be one of:

  • A template path
  • A list of template paths
  • An object with a render method
  • A callable receiving the plugin as only parameter and returning any of the above.

context must be a callable receiving the plugin instance and the template context and returning a dictionary. The default implementation simply returns a dictionary containing a single key named plugin containing the plugin instance.

# Template snippets have access to everything in the template
# context, including for example ``page``, ``request``, etc.
renderer.register_template_renderer(
    Snippet,
    lambda plugin: plugin.template_name,
)

# Additional context can be provided:
renderer.register_template_renderer(
    Team,
    'pages/plugins/team.html',  # Can also be a callable
    lambda plugin, context: {
        'persons': Person.objects.filter(
            # Assuming that the page has a team foreign key:
            team=plugin.parent.team,
        ),
    },
)
render_plugin_in_context(plugin, context=None)[source]

Render a plugin, passing on the template context into the plugin’s template (if the plugin uses a template renderer).

feincms3.renderer.default_context(plugin, context)[source]

Return the default context for plugins rendered with a template, which simply is a single variable named plugin containing the plugin instance.