(cross-posted from
http://stackoverflow.com/questions/5972458/help-with-custom-jinja2-extension)
--------
I've struggled to get this Jinja2 custom extension to work -- the docs
weren't kidding when they said writing one was not for "civilians" --
and finally managed to arrive at this working code:
class WrapperExtension(Extension):
tags = set(['wrap'])
def parse(self, parser):
lineno = parser.stream.next().lineno
args = [parser.parse_expression()]
args.append(nodes.Const(args[0].name))
return nodes.CallBlock(
self.call_method('_render', args),
[], [], []).set_lineno(lineno)
def _render(self, value, name, *args, **kwargs):
if some_condition():
return '<wrapper id="%s">%s</wrapper>' % (name, value)
return value
As I said, this is now working. What I'm unsure about is why I need to
return nodes.CallBlock in parse(), rather than self.call_method()
(which returns a nodes.Call object). If anyone has any insight -- or
can point me to a tutorial on writing extensions -- please do let me
know.
--
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.