On Tue, Aug 25, 2009 at 9:26 PM, stevedegrace<degr...@gmail.com> wrote:
> I suppose this is easy enough to fix by doing the binding in the view
> when the context is first constructed which would probably have been
> easier in the first place, but now I'm curious and want to know what's
> happening.

If you want something available in the template, then yes, making it
available through the context provided in the view is the way to do
it. The various built-in template tags which can add variables to the
context all have limited scope, and in many cases (e.g., the "with"
tag) this is explicit.

> I never suspected this behaviour and got no inkling to expect it from
> the Django docs. Can anyone with more knowledge of the guts of the
> template rendering system suggest why this happens and if there is any
> way to force variables bound by custom tags into the "global scope"?

The answer is that the template context is not a dictionary. It's a
stack of dictionaries, with fall-through lookup semantics.

Various operations going on in the template push a new dictionary onto
the context stack and populate it, then pop that dictionary off the
context stack when they're done. In general, this is a good thing; for
example, it means it's much easier to have tags do whatever they want
without trampling all over each other's context variables. Many
built-in tags take good advantage of this.

If you truly need a way to make a variable available everywhere in a
template, but it's simply not possible to provide that directly
through the initial context, you want to look at the "dicts" attribute
of the Context instance, which is a list of dictionaries. The last one
in the list (e.g., index -1) is the one that originally came from
instantiating the Context with its initial set of variables, and
during normal template use it will always persist for the life of the
rendering process (someone who wanted to shoot themselves in the foot
could write a tag which manually removes it, but the normal context
push/pop operations will not do this and will raise an exception if
you try to pop the last dictionary out of the stack).


-- 
"Bureaucrat Conrad, you are technically correct -- the best kind of correct."

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to