When you reference a value, the layers are searched, most recently added
first, until one is found that has the variable you want (or not). When you
set a variable, it always uses the top layer, even if the same variable exists
in a lower layer.
Various things add layers, for example, the "with" and "for" tags. When you
pass the corresponding end tag, the layer is discarded. So if some of your
domready invocations are within one of these constructs, and your
domready_render is outside, it's not going to see the stuff that was defined
inside.
A way of handling this is to make your renderer a tag that requires an end
tag. It can create the variable (probably pushing it's own layer onto the
context stack) in its opening tag, making its value a mutable, such as a
python list. The domready tags would then append to the list. This avoids
making a new version of the same variable in some inner layer because
you only read the variable, you don't set it: it still has the
original list, it is
the list that is modified. The render renders its template contents, then,
as a final act, emits the javascript in its variable. This is like it being
rendered at the end tag.
You might want to make both tags accept a string argument to use as the
variable name, just in case you discover a need to nest them, say to
accumulate stuff that must occur in an order different from that in which it
appears in the template. You could, of course, default the variable to a
standard name.
Bill
On Fri, Feb 25, 2011 at 10:26 AM, kost BebiX <kost88@gmail.com> wrote:
> Hi! I would like to write two simple tags, one would be called {% domready
> %}, and another one is {% domready_render %}, first will add some js to some
> buffer and second will just join it alltogather and print it (at base
> template in some $(document).ready(...)). So my question is: where/how do I
> need to store some variables between those tags? (maybe some current request
> context or what?)
> I mean, I wrote something like:
> @register.tag
> def domready(parser, token):
> nodelist = parser.parse(('enddomready',))
> parser.delete_first_token()
> return DomreadyNode(nodelist)
> class DomreadyNode(template.Node):
> def __init__(self, nodelist):
> self.nodelist = nodelist
>
> def render(self, context):
> if 'dom_ready' not in context:
> context['dom_ready'] = []
>
> context['dom_ready'].append(self.nodelist.render(context))
> return ''
> @register.tag
> def domready_render(parser, token):
> return DomreadyRenderNode()
> class DomreadyRenderNode(template.Node):
> def render(self, context):
> if 'dom_ready' in context:
> return u"\n".join(context['dom_ready'])
> return ''
> But it doesn't work in different templates (contexts are different?).
> Thank you.
>
> --
> 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+unsubscribe@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>
--
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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
No comments:
Post a Comment