views:

168

answers:

1

I try to make extension for jinja2. I has written such code:

http://dumpz.org/12996/

But I receive exception: "'NoneType' object is not iterable" Where is a bug? That should return parse. Also what should accept and return _media?

A: 

You're using a CallBlock which indicates that you want your extension to act as a block. E.g. {% test arg1 arg2 %} stuff in here {% endtest %}

nodes.Callblock expects that you pass it a list of nodes representing the body (the inner statements) for your extension. Currently this is where you're passing None - hence your error.

Once you've parsed your arguments, your need to proceed to parse the body of the block. Fortunately it's easy. You can simply do:

body = parser.parse_statements(['name:endwith'], drop_needle=True)
return nodes.CallBlock(self.call_method('_media', args),[], [], body).set_lineno(lineno)

Alternatively, if you don't want your extension to be a block tag. e.g. {% test arg1 arg2 %} you shouldn't use nodes.CallBlock, you should just use nodes.Call instead, which doesn't take a body parameter. So just do:
return self.call_method('_media', args)
self.callmethod is simply a handy wrapper function that creates a Call node for you.

I've spent a few days writing Jinja2 extensions and it's tricky. There's not much documentation (other than the code). The coffin github project has a few examples of extensions here.

Brian McDonnell