On May 13, 2006, at 4:30 PM, nevroe wrote:
> The Django documentation here [1] says something about it being very
> useful that Context objects are stacks.
>
> [1]: http://www.djangoproject.com/documentation/templates_python/
>
> """
> A Context object is a stack. That is, you can push() and pop() it. If
> you pop() too much, it'll raise django.template.ContextPopException:
> ...
> Using a Context as a stack comes in handy in some custom template  
> tags,
> as you'll see below.
> """
>
> I'm trying to figure out the case scenario where this becomes a useful
> thing, and I couldn't find the mentioned documentation.  Did I miss  
> it,
> or does anyone have a good example where it is useful to have template
> context as a stack?

Well, the canonical example is ``{% for %}`` loops::

        {% for var in list %}
                {{ var }}
        {% endfor %}

It wouldn't make any sense if ``{{ var }}`` became available outside  
of the ``{% for %}`` loop or (even worse) overwrote an existing  
variable.

So if you ever want to write a block tag that binds variables, then  
you'll want to be pushing/popping contexts.

Jacob

--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Reply via email to