Le 17/03/2012 02:20, AKP a écrit :
As you can probably tell, I pass the 'notes' list to my extension,
parse the body of the tag, and return the result as a CallBlock, with
the following callback function:

     def _callback(self, args, caller):
         rv = caller()
         args.append(rv)
         return '[%s]' % str(len(args))

Instead of my last two questions, I ask these instead:

1. Is this a safe thing to do in Jinja2? Will I improperly change
anything in the global state or cause some other sort of problem? And,
would this break down when serving multiple requests? (I don't think
so, but I can't be sure.)

2. Is there any way to avoid explicitly passing 'notes' to the
'footnote' tag?

Thanks to Simon for helping me create this first solution, and thanks
again to you all for taking the time to read this.


As you found out, the only way to add new tags is to write a Jinja extension, but my (unjustified) instinct is that an extension is probably more work than is really needed. The {% filter %} tag can help.

Would something like this work?

def append_to(text, list):
    list.append(text)
    return ''
jinja_env.filters['append_to'] = append_to

{% set footnotes = [] %}
{% filter append_to(footnotes) %}
This is footnote 1!
{% endfilter %}


About your extension being safe, can you copy the full code?


Remains the need to explicitly make a list and pass it every time. I guess this could be avoided but I’m not sure how since filters are "global" to an environment.

Maybe making now environments every time could work:
http://jinja.pocoo.org/docs/api/#jinja2.Environment.overlay

Regards,
--
Simon Sapin

--
You received this message because you are subscribed to the Google Groups 
"pocoo-libs" 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/pocoo-libs?hl=en.

Reply via email to