Opened 19 years ago

Closed 19 years ago

Last modified 18 years ago

#1140 closed enhancement (wontfix)

[patch] using another type of template (zpt) with render_to_response / render_to_string

Reported by: bas@… Owned by: Adrian Holovaty
Component: Template system Version: 0.90
Severity: normal Keywords: templates, zpt, pagetemplates
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Hi!

for some of my sites i use zpt (ZopePageTemplates).
when making login/logout pages with django.views.auth.login i found that
i could not use zpt because the pages where automaticly send
to render_to_response and required django templates.

for this i made a small patch to loader.py to allow setting an arbirary
prefered templating system. (such as django.contrib.pagetemplate.pagetemplate.get_template)

the patch is far from perfect (but it works :)). it's missing a change to conf.global_settings.py
so the TEMPLATE_SYSTEM_PREFERED configuration is not mandatory. (should be set default to "")
also the code and naming could be better and more consistent i guess.

it would be nice to have something like a list for TEMPLATING SYSTEMS much like TEMPLATE_LOADERS.
so one could specify with templating systems are in use and have the templating system search all
of those. (as far as zpt goes this is perfect cause they both work with a template instance and
a context to render a page, so there is not much difference between calling zpt or djangotemplates)

settings.py

# For no other prefered templating system
# TEMPLATE_SYSTEM_PREFERED = ""

# For zpt/tal templates
TEMPLATE_SYSTEM_PREFERED = "django.contrib.pagetemplate.pagetemplate.get_template"

patch

--- loader.py.bak       2005-12-23 00:26:59.000000000 +0100
+++ loader.py   2005-12-29 23:44:20.000000000 +0100
@@ -22,7 +22,19 @@

 from django.core.exceptions import ImproperlyConfigured
 from django.core.template import Origin, StringOrigin, Template, Context, TemplateDoesNotExist, add_to_builtins
-from django.conf.settings import TEMPLATE_LOADERS, TEMPLATE_DEBUG
+from django.conf.settings import TEMPLATE_LOADERS, TEMPLATE_DEBUG, TEMPLATE_SYSTEM_PREFERED
+
+if TEMPLATE_SYSTEM_PREFERED:
+    i = TEMPLATE_SYSTEM_PREFERED.rfind('.')
+    module, attr = TEMPLATE_SYSTEM_PREFERED[:i], TEMPLATE_SYSTEM_PREFERED[i+1:]
+    try:
+       CustomTemplateModule = __import__(module, globals(), locals(), [attr])
+    except ImportError, e:
+        raise ImproperlyConfigured, 'Error importing template system %s: "%s"' % (module, e)
+    try:
+        CustomTemplate = getattr(CustomTemplateModule, attr)
+    except AttributeError:
+        raise ImproperlyConfigured, 'Module "%s" does not define a "%s" callable Template System' % (module, attr)

 template_source_loaders = []
 for path in TEMPLATE_LOADERS:
@@ -87,10 +99,27 @@
     the templates in the list. Returns a string.
     """
     dictionary = dictionary or {}
-    if isinstance(template_name, (list, tuple)):
-        t = select_template(template_name)
+
+    def get_django_template(template_name):
+       if isinstance(template_name, (list, tuple)):
+           t = select_template(template_name)
+       else:
+           t = get_template(template_name)
+       return t
+
+    def get_custom_template(template_name):
+       try:
+           t = CustomTemplate(template_name)
+       except TemplateDoesNotExist, e:
+           # Fall back on Django Style Templates
+           t = get_django_template(template_name)
+       return t
+
+    if TEMPLATE_SYSTEM_PREFERED:
+       t = get_custom_template(template_name)
     else:
-        t = get_template(template_name)
+       t = get_django_template(template_name)
+
     if context_instance:
         context_instance.update(dictionary)
     else:

Change History (8)

comment:1 by Adrian Holovaty, 19 years ago

Summary: using another type of template (zpt) with render_to_response / render_to_string[patch] using another type of template (zpt) with render_to_response / render_to_string

comment:2 by bas@…, 19 years ago

sorry forgot that:

def get_custom_template(template_name):

also has to check if template_name is a tuple or list and if so
iterate the list, the same way select_template does.

comment:3 by anonymous, 19 years ago

yet another update of the patch...

this time to also support flatpages and hopefully almost anything that uses core.loader

Patch

--- loader.py.bak       2005-12-23 00:26:59.000000000 +0100
+++ loader.py   2005-12-30 00:45:07.000000000 +0100
@@ -22,7 +22,19 @@

 from django.core.exceptions import ImproperlyConfigured
 from django.core.template import Origin, StringOrigin, Template, Context, TemplateDoesNotExist, add_to_builtins
-from django.conf.settings import TEMPLATE_LOADERS, TEMPLATE_DEBUG
+from django.conf.settings import TEMPLATE_LOADERS, TEMPLATE_DEBUG, TEMPLATE_SYSTEM_PREFERED
+
+if TEMPLATE_SYSTEM_PREFERED:
+    i = TEMPLATE_SYSTEM_PREFERED.rfind('.')
+    module, attr = TEMPLATE_SYSTEM_PREFERED[:i], TEMPLATE_SYSTEM_PREFERED[i+1:]
+    try:
+       CustomTemplateModule = __import__(module, globals(), locals(), [attr])
+    except ImportError, e:
+        raise ImproperlyConfigured, 'Error importing template system %s: "%s"' % (module, e)
+    try:
+        CustomTemplate = getattr(CustomTemplateModule, attr)
+    except AttributeError:
+        raise ImproperlyConfigured, 'Module "%s" does not define a "%s" callable Template System' % (module, attr)

 template_source_loaders = []
 for path in TEMPLATE_LOADERS:
@@ -65,11 +77,13 @@
             pass
     raise TemplateDoesNotExist, name

-def get_template(template_name):
+def get_template(template_name, template_system_prefered=TEMPLATE_SYSTEM_PREFERED):
     """
     Returns a compiled Template object for the given template name,
     handling template inheritance recursively.
     """
+    if template_system_prefered:
+       return get_custom_template(template_name)
     return get_template_from_string(*find_template_source(template_name))

 def get_template_from_string(source, origin=None ):
@@ -79,6 +93,24 @@
     """
     return Template(source, origin)

+def get_custom_template(template_name):
+       if isinstance(template_name, (list, tuple)):
+           t = None
+           for templ in template_name:
+               try:
+                   t = CustomTemplate(templ)
+               except TemplateDoesNotExist, e:
+                   continue
+           if t==None:
+               t = select_template(template_name, template_system_prefered="")
+       else:
+           try:
+               t = CustomTemplate(template_name)
+           except TemplateDoesNotExist, e:
+               # Fall back on Django Style Templates
+               t = get_template(template_name, template_system_prefered="")
+       return t
+
 def render_to_string(template_name, dictionary=None, context_instance=None):
     """
     Loads the given template_name and renders it with the given dictionary as
@@ -97,11 +129,14 @@
         context_instance = Context(dictionary)
     return t.render(context_instance)

-def select_template(template_name_list):
+def select_template(template_name_list, template_system_prefered=TEMPLATE_SYSTEM_PREFERED):
     "Given a list of template names, returns the first that can be loaded."
     for template_name in template_name_list:
         try:
-            return get_template(template_name)
+            if template_system_prefered:
+                return get_custom_template(template_name)
+            else:
+                return get_template(template_name)
         except TemplateDoesNotExist:
             continue
     # If we get here, none of the templates could be loaded

comment:4 by anonymous, 19 years ago

priority: normalhigh

comment:5 by Adrian Holovaty, 19 years ago

priority: highnormal

comment:6 by asmodai@…, 19 years ago

milestone: Version 0.91Version 0.92

0.91 was released, move to 0.92.

comment:7 by Adrian Holovaty, 19 years ago

Resolution: wontfix
Status: newclosed

Thanks for this patch, but this is out of the scope of Django for the time being.

comment:8 by Adrian Holovaty, 18 years ago

milestone: Version 0.92

Milestone Version 0.92 deleted

Note: See TracTickets for help on using tickets.
Back to Top