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.
The reason is that parse()
is expected to return a statement node, such as CallBlock
or Assign
. call_method()
returns an expression node, which you must wrap in CallBlock
to have a statement.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With