Skip to content

Instantly share code, notes, and snippets.

@gregplaysguitar
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gregplaysguitar/aeac2702562c5b0771a1 to your computer and use it in GitHub Desktop.
Save gregplaysguitar/aeac2702562c5b0771a1 to your computer and use it in GitHub Desktop.
Djangocms2000 template tag file for Coffin & Jinja2 compatibility
# -*- coding: utf-8 -*-
from django.conf import settings
from coffin import template
from jinja2 import contextfunction, Markup
from cms.application import get_rendered_block, get_rendered_image
from cms.templatetags.cms_editor import cms_editor
from cms.utils import is_editing
from cms.models import Page
register = template.Library()
def process_kwargs(context, kwargs):
'''Pre-process the arguments from the jinja2 template function call so
they make sense to the cms.'''
# cms will make use of the request, if available
if 'request' in context and 'request' not in kwargs:
kwargs['request'] = context['request']
# The site kwarg resolves ambiguity between site and page blocks,
# without needing to pass SITE_ID from the template
site = kwargs.pop('site', False)
if site and not kwargs.get('site_id', None):
kwargs['site_id'] = settings.SITE_ID
# If an explicit url is passed, use it to find the related object
url = kwargs.pop('url', None)
if url:
kwargs['related_object'] = Page.objects.get_for_url(url)
return kwargs
def conditional_escape(obj):
if type(obj) is unicode:
return Markup(obj)
else:
return obj
@register.object
@contextfunction
def cms_block(context, *args, **kwargs):
rendered = get_rendered_block(*args, **process_kwargs(context, kwargs))
return conditional_escape(rendered)
@register.object
@contextfunction
def cms_image(context, *args, **kwargs):
rendered = get_rendered_image(*args, **process_kwargs(context, kwargs))
return conditional_escape(rendered)
register.object('cms_editor', contextfunction(lambda c: Markup(cms_editor(c))))
register.object('cms_editing', is_editing)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment