Skip to content

Commit

Permalink
Changed template library system so that it looks for a module-level v…
Browse files Browse the repository at this point in the history
…ariable 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
  • Loading branch information
adrianholovaty committed Nov 27, 2005
1 parent 2564f34 commit 2fb95f1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
12 changes: 5 additions & 7 deletions django/core/template/__init__.py
Expand Up @@ -890,13 +890,11 @@ def get_library(module_name):
mod = __import__(module_name, '', '', ['']) mod = __import__(module_name, '', '', [''])
except ImportError, e: except ImportError, e:
raise InvalidTemplateLibrary, "Could not load template library from %s, %s" % (module_name, e) raise InvalidTemplateLibrary, "Could not load template library from %s, %s" % (module_name, e)
for k, v in mod.__dict__.items(): try:
if isinstance(v, Library): lib = mod.register
lib = v libraries[module_name] = lib
libraries[module_name] = lib except AttributeError:
break raise InvalidTemplateLibrary, "Template library %s does not have a variable named 'register'" % module_name
if not lib:
raise InvalidTemplateLibrary, "Template library %s does not have a Library member" % module_name
return lib return lib


def add_to_builtins(module_name): def add_to_builtins(module_name):
Expand Down
9 changes: 4 additions & 5 deletions docs/templates_python.txt
Expand Up @@ -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 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. 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 To be a valid tag library, the module contain a module-level variable named
``template.Library`` instance, in which all the tags and filters are ``register`` that is a ``template.Library`` instance, in which all the tags and
registered. So, near the top of your module, put the following:: filters are registered. So, near the top of your module, put the following::


from django.core import template from django.core import template
register = template.Library()


Convention is to call this instance ``register``. register = template.Library()


.. admonition:: Behind the scenes .. admonition:: Behind the scenes


Expand Down

0 comments on commit 2fb95f1

Please sign in to comment.