Deeper into the use case.
I have a tag, that generates IDs for tags, if needed, stores them and
finally at the end of the page it prints them into a JS array, which
is used to process the IDs (in my case IDs of dojo widgets).
Without the template tag my code would look like this:
<input dojoType="combobox" id="inventedId" />
<script>djConfig.searchIds.push("inventedId");</script>
{% for u in users %}
<a href="{{ u.get_absolute_url }}" dojoType="my:userlinker"
id="user{{ u.id }}" />
<script>djConfig.searchIds.push("user{{ u.id }}");</script>
{% endfor %}
_With_ the template tag, I can shorten that to:
<input {% dojo_widget "combobox" %} />
{% for u in users %}
<a href="{{ u.get_absolute_url }}" {% dojo_widget "my:userlinker" %} />
{% endfor %}
<script> djConfig.searchIds = djConfig.searchIds.concat({%
get_dojo_widget_ids %}); </script>
And just imagine, when you have multiple IDs spread all over the page,
It get unreadable, you have to create generic IDs that never colide
.... a perfect task for a programming language!
And spitting them all out at the end, best even in the base.html, that
is a task that exploits the current scope rules.
My current solution, which solves
* the problem I opened this thread with
* and allows me to use {% get_dojo_widget_ids %} inside base.html
(would normally be out of scope)
is to simply use the "most global scope" directly, by doing the
following inside my render() method:
# FIXXXME this is a bad hack, fixing the below problem not nice i guess
context = context.dicts[len(context.dicts)-1]
but it works like a charm!!!!
And might just need to be "officialiced" by passing another parameter
to the render mehtod which is called "global_scope" or something
alike.
regards
Wolfram
On 4/11/07, Gulopine <[EMAIL PROTECTED]> wrote:
>
> On Apr 11, 8:10 am, "Wolfram Kriesing" <[EMAIL PROTECTED]>
> wrote:
> > The "add_data" adds the data to the context inside the render()
> > function of the tag. Here lies the problem, the scope of the for loop
> > gets destroyed when the for loop ends
>
> Well, in a template context, that does make more sense to me, since
> you don't really have advanced control structures to break the flow at
> different points. In pure Python, it would be reasonable to set
> something within a loop, then break out and use that value elsewhere.
> In a template, however, the entire loop is iterated regardless, and
> you'd be setting the value on every iteration, so the last value is
> the only one that would be available outside the loop anyway. In that
> case, I think it would make more sense to just set that somewhere
> outside the loop and avoid the mess.
>
> That said, there might be other use cases for what you're describing,
> but the only real way to go about it would be add some kind of
> "parent" attribute to each context on the stack, but then there are
> questions of what it points to. Is it the immediately previous context
> or the top-most? Should there be a way of specifying how far up the
> stack to go? Should each context on the stack be able to report what
> position it's in? If this would be implemented, would there be a
> safeguard to prevent template tags from swapping out one layer of the
> stack with another?
>
> All in all, it seems like the current behavior would be acceptable,
> but like I said, there may be other use cases that haven't yet been
> raised.
>
> -Gul
>
>
> >
>
--
cu
Wolfram
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-developers?hl=en
-~----------~----~----~----~------~----~------~--~---