On Tue, Mar 18, 2008 at 12:29 AM, Malcolm Tredinnick
<[EMAIL PROTECTED]> wrote:

> If you know how many levels of nesting you want to keep the value for,
> you could traverse the context['parent'] links to store things in the
> right parent / grandparent / what have you.

I don't see any context['parent'] links to traverse.  After reading
your email, I wrote:

    def render(self, context):
        for i in range(self.parent_contexts):
            context = context['parent']
        context[self.variable_name] = self.value
        return ''

...but that didn't work.  After some digging around, I came up with:

    def render(self, context):
        context = context.dicts[self.parent_contexts]
        context[self.variable_name] = self.value
        return ''

...which works.  Is this code liable to break at some point?  Is there
a better way to write this?

--
Daryl


On Tue, Mar 18, 2008 at 12:29 AM, Malcolm Tredinnick
<[EMAIL PROTECTED]> wrote:
>
>
> On Mon, 2008-03-17 at 15:16 -0700, Peter wrote:
>> I have a template tag that performs an expensive database call (it
>> returns a dict of ints), and I run this template tag over a nested
>> loop of items and values.
>>
>> I decided to be sneaky by setting the result of this call into the
>> context.  I do the following:
>> * see if this variable exists in the context: if
>> context.has_key('counts')
>> * if the variable is not found, i set it: context['counts'] =
>> expensive_method()
>> * otherwise, I just grab it from the context
>>
>> So it looked like this in my template:
>>
>> {% for value in values %}
>>     {% for item in items %}
>>         {% custom_tag value item %}
>>     {% endfor %}
>> {% endfor %}
>>
>> This seemed like a great solution to my issue, but it was still very
>> slow.  I modified my code to do some printouts and...
>>
>> It turns out that this variable only remains in the context for the
>> length of the inner loop (items) and it disappears whenever I iterate
>> to the next item in the outer loop (values).
>>
>> I figured out a workaround to this -- either with doc example* using
>> {% with ... as ... %} for another tag, or just setting it in my
>> view... But, does anyone know why the context functions like this?
>
> Because block-based tags like this (and like {%block %}) push a new
> context dictionary onto the stack upon entry and pop it off again at
> exit so that things don't hang around cluttering up the outer namespace.
>
> If you know how many levels of nesting you want to keep the value for,
> you could traverse the context['parent'] links to store things in the
> right parent / grandparent / what have you.
>
> Malcolm
>
> --
> If you think nobody cares, try missing a couple of payments.
> http://www.pointy-stick.com/blog/
>
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
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?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to