From 2fb95f1de699fbc3c39ac83dbf5fabb63af96642 Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Sun, 27 Nov 2005 16:43:56 +0000 Subject: [PATCH] Changed template library system so that it looks for a module-level variable named 'register' rather than the first instance of template.Library it finds git-svn-id: http://code.djangoproject.com/svn/django/trunk@1461 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/core/template/__init__.py | 12 +++++------- docs/templates_python.txt | 9 ++++----- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/django/core/template/__init__.py b/django/core/template/__init__.py index 8144e651a1969..c399c93c1425b 100644 --- a/django/core/template/__init__.py +++ b/django/core/template/__init__.py @@ -890,13 +890,11 @@ def get_library(module_name): mod = __import__(module_name, '', '', ['']) except ImportError, e: raise InvalidTemplateLibrary, "Could not load template library from %s, %s" % (module_name, e) - for k, v in mod.__dict__.items(): - if isinstance(v, Library): - lib = v - libraries[module_name] = lib - break - if not lib: - raise InvalidTemplateLibrary, "Template library %s does not have a Library member" % module_name + try: + lib = mod.register + libraries[module_name] = lib + except AttributeError: + raise InvalidTemplateLibrary, "Template library %s does not have a variable named 'register'" % module_name return lib def add_to_builtins(module_name): diff --git a/docs/templates_python.txt b/docs/templates_python.txt index 904844fa2541e..de212cd141c65 100644 --- a/docs/templates_python.txt +++ b/docs/templates_python.txt @@ -443,14 +443,13 @@ the given Python module name, not the name of the app. Once you've created that Python module, you'll just have to write a bit of Python code, depending on whether you're writing filters or tags. -To be a valid tag library, the module contain a module-level variable that is a -``template.Library`` instance, in which all the tags and filters are -registered. So, near the top of your module, put the following:: +To be a valid tag library, the module contain a module-level variable named +``register`` that is a ``template.Library`` instance, in which all the tags and +filters are registered. So, near the top of your module, put the following:: from django.core import template - register = template.Library() -Convention is to call this instance ``register``. + register = template.Library() .. admonition:: Behind the scenes