Django Template Tag Namespaces Now Possible
Backwards compatibility
Django's policy is to remain backwards compatible, and the template language is certainly no exception. In order to give the "{% load %}" tag namespacing features, it needed to be extended in a way that allows current assumptions about its behavior to remain the same. In particular, the assumption that all tags will be loaded into the global namespace by default had to stay. This means that, given a template library named "lib1" containing "tag1" and "tag2", the following code must work:
{% load lib1 %}
{% tag1 %}
{% tag2 %}
Current proposals have suggested the backwards-incompatible syntax that assumes namespaces are on by default:
{% load lib1 %}
{% lib1.tag1 %}
{% lib1.tag2 %}
In my implementation, "load" works the same (as in the top example), but has a few keywords that control its behavior. For example, to load a library into a namespace, use "into":
{% load lib1 into lib1 %}
{% lib1.tag1 %}
{% lib1.tag2 %}
Other features
To load a specific tag (optionally renaming it with the "as" keyword):
{% load lib1.tag1 as my_tag %}
{% my_tag %}
Loading from a specific app can be done using the "from" keyword:
{% load lib1 from app1 into app1 %}
{% load lib1 from app2 into app2 %}
{% app1.tag1 %}
{% app2.tag1 %}
To make everybody happy
It has been suggested to write a separate "{% import %}" tag in order to enable namespaces by default while retaining backwards-compatibility with existing Django applications. I've also experimented with the following import syntax, and it's also included in django-smart-load-tag:
{% import lib1 %}
{% lib1.tag1 %}
Its namespace-on-by-default design can be subverted using "* from":
{% import * from lib1 %}
{% tag1 %}
The "as" and "from" keywords are also implemented:
{% import lib1 as my_lib %}
{% my_lib.tag1 %}
{% import lib1 from app1 %}
{% lib1.tag1 %}
Where to go from here
If template tag namespaces are to be accepted as a core part of Django, some discussion will need to take place on what is the most correct solution for moving forward. Your comments here or on the mailing list can make a difference, and with enough contribution from the community, perhaps all my ponies will one day run free.

blog comments powered by Disqus