views:

877

answers:

1

I have a small typography related templatetag library that I use on almost every page. Right now I need to load it for each template using

{% load nbsp %}

Is there a way to load it "globally" for all views and templates at once? Putting the load tag into a base template doesn't work.

+12  A: 

There is an add_to_builtins method in django.template.loader. Just pass it the name of your templatetags module (as a string).

from django.template.loader import add_to_builtins

add_to_builtins('myapp.templatetags.mytagslib')

Now mytagslib is available automatically in any template.

Daniel Roseman
Excellent, thank you.
TomA
Note that while you can do this, it's quite likely that you'll regret it at some point (I've done it, and regretted it). It makes your templates non-portable to any other project which doesn't add_to_builtins, and it can break tests which render those templates (unless you make sure the test runner also runs add_to_builtins). All in all, it makes things more brittle for a very small gain in convenience.
Carl Meyer
Not to mention that any new developer is going to be confuzzled by your use of a tag that doesn't exist in the standard libraries, until they ask you (if you're still around) or stumble upon it. :) Remember, explicit is better than implicit.
Xiong Chiamiov