def _load_lib(lib): if not isinstance(lib, CoffinLibrary): # If this is only a standard Django library, # convert it. This will ensure that Django # filters in that library are converted and # made available in Jinja. lib = CoffinLibrary.from_django(lib) extensions.extend(getattr(lib, 'jinja2_extensions', [])) filters.update(getattr(lib, 'jinja2_filters', {})) globals.update(getattr(lib, 'jinja2_globals', {})) tests.update(getattr(lib, 'jinja2_tests', {})) attrs.update(getattr(lib, 'jinja2_environment_attrs', {}))
def _get_templatelibs(self): """Return an iterable of template ``Library`` instances. Since we cannot support the {% load %} tag in Jinja, we have to register all libraries globally. """ from django.conf import settings from django.template import get_library, InvalidTemplateLibrary libs = [] for a in settings.INSTALLED_APPS: try: path = __import__(a + '.templatetags', {}, {}, ['__file__']).__file__ path = os.path.dirname(path) # we now have the templatetags/ directory except ImportError: pass else: for f in os.listdir(path): if f == '__init__.py': continue if f.endswith('.py'): try: # TODO: will need updating when #6587 lands # libs.append(get_library( # "django.templatetags.%s" % os.path.splitext(f)[0])) l = get_library(os.path.splitext(f)[0]) if not isinstance(l, CoffinLibrary): # If this is only a standard Django library, # convert it. This will ensure that Django # filters in that library are converted and # made available in Jinja. l = CoffinLibrary.from_django(l) libs.append(l) except InvalidTemplateLibrary: pass return libs
"""Makes the template filters from the ``django.contrib.markup`` app available to both the Jinja2 and Django engines. In other words, adding ``coffin.contrib.markup`` to your INSTALLED_APPS setting will enable the markup filters not only through Coffin, but also through the default Django template system. """ from coffin.template import Library as CoffinLibrary from django.contrib.markup.templatetags.markup import register # Convert Django's Library into a Coffin Library object, which will # make sure the filters are correctly ported to Jinja2. register = CoffinLibrary.from_django(register)
if self.use_pdb: pdb_with_context(context) else: setup_readline_history() run_shell(context) return '' @register.tag def repl(parser, token): use_pdb = False bits = token.contents.split() if len(bits) > 1: if bits[1] == 'pdb': use_pdb = True else: raise TemplateSyntaxError('The second argument to the "repl" tag, if present, must be "pdb".') return REPLNode(use_pdb) try: from coffin.template import Library as CoffinLibrary import coffin.common except ImportError: pass else: # We could simply create a Coffin library in the first place, # of course, but this allows us to more easily maintain this # change as a fork. from template_repl.jinja2_ext import REPLExtension register = CoffinLibrary.from_django(register) register.tag(REPLExtension)