From 1eca0e95cf1aa9b7a2d2806999609924e2bc3afe Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Sat, 29 Nov 2014 00:05:56 +0100 Subject: [PATCH] Added Django template backend. --- django/template/backends/base.py | 3 +++ django/template/backends/django.py | 41 ++++++++++++++++++++++++++++++ django/template/backends/dummy.py | 3 +++ django/template/backends/jinja2.py | 3 +++ django/template/backends/utils.py | 3 +++ 5 files changed, 53 insertions(+) create mode 100644 django/template/backends/django.py diff --git a/django/template/backends/base.py b/django/template/backends/base.py index f70114499f..25a7dd2a15 100644 --- a/django/template/backends/base.py +++ b/django/template/backends/base.py @@ -1,3 +1,6 @@ +# Since this package contains a "django" module, this is required on Python 2. +from __future__ import absolute_import + from django.core.exceptions import ( ImproperlyConfigured, SuspiciousFileOperation) from django.template.utils import get_app_template_dirs diff --git a/django/template/backends/django.py b/django/template/backends/django.py new file mode 100644 index 0000000000..a8dc2de300 --- /dev/null +++ b/django/template/backends/django.py @@ -0,0 +1,41 @@ +# Since this package contains a "django" module, this is required on Python 2. +from __future__ import absolute_import + +from django.conf import settings +from django.template.context import Context, RequestContext +from django.template.engine import Engine + + +from .base import BaseEngine + + +class DjangoTemplates(BaseEngine): + + app_dirname = 'templates' + + def __init__(self, params): + params = params.copy() + options = params.pop('OPTIONS').copy() + options.setdefault('debug', settings.TEMPLATE_DEBUG) + options.setdefault('file_charset', settings.FILE_CHARSET) + super(DjangoTemplates, self).__init__(params) + self.engine = Engine(self.dirs, self.app_dirs, **options) + + def from_string(self, template_code): + return Template(self.engine.from_string(template_code)) + + def get_template(self, template_name): + return Template(self.engine.get_template(template_name)) + + +class Template(object): + + def __init__(self, template): + self.template = template + + def render(self, context=None, request=None): + if request is None: + context = Context(context) + else: + context = RequestContext(request, context) + return self.template.render(context) diff --git a/django/template/backends/dummy.py b/django/template/backends/dummy.py index 813a0cf560..7df095baa6 100644 --- a/django/template/backends/dummy.py +++ b/django/template/backends/dummy.py @@ -1,3 +1,6 @@ +# Since this package contains a "django" module, this is required on Python 2. +from __future__ import absolute_import + import io import string diff --git a/django/template/backends/jinja2.py b/django/template/backends/jinja2.py index 8a71ee181e..cfdb86f0a4 100644 --- a/django/template/backends/jinja2.py +++ b/django/template/backends/jinja2.py @@ -1,3 +1,6 @@ +# Since this package contains a "django" module, this is required on Python 2. +from __future__ import absolute_import + import sys from django.conf import settings diff --git a/django/template/backends/utils.py b/django/template/backends/utils.py index 8147bb4988..881d83acf2 100644 --- a/django/template/backends/utils.py +++ b/django/template/backends/utils.py @@ -1,3 +1,6 @@ +# Since this package contains a "django" module, this is required on Python 2. +from __future__ import absolute_import + from django.middleware.csrf import get_token from django.utils.functional import lazy from django.utils.html import format_html