如何创建 jinja2 扩展?
我尝试为 jinja2 进行扩展。我写了这样的代码:
但我收到异常: “NoneType”对象不可迭代
。哪里有bug? 这应该返回parse
。另外什么应该接受并返回_media
?
I try to make extension for jinja2. I has written such code:
But I receive exception: 'NoneType' object is not iterable
. Where is a bug?
That should return parse
. Also what should accept and return _media
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在使用
CallBlock
,这表明您希望您的扩展程序充当块。例如,nodes.CallBlock 期望您向其传递代表扩展主体(内部语句)的节点列表。目前,这是您传递
None
的地方 - 因此您的错误。解析完参数后,您需要继续解析块的主体。幸运的是,这很容易。您可以简单地执行:
然后返回一个新节点。
CallBlock
接收一个要调用的方法(在本例中为_mytestfunc
),该方法为您的扩展提供逻辑。或者,如果您不希望您的扩展成为块标记,例如
您不应该使用
nodes.CallBlock
,您应该只使用nodes.Call
,这不接受主体参数。所以只需这样做:self.call_method
只是一个方便的包装函数,可以为您创建一个 Call 节点。我花了几天时间编写 Jinja2 扩展,这很棘手。没有太多文档(除了代码)。 coffin GitHub 项目此处提供了一些扩展示例。
You're using a
CallBlock
, which indicates that you want your extension to act as a block. E.g.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 passingNone
- hence your error.Once you've parsed your arguments, you need to proceed to parse the body of the block. Fortunately, it's easy. You can simply do:
and then return a new node. The
CallBlock
receives a method to be called (in this case_mytestfunc
) that provides the logic for your extension.Alternatively, if you don't want your extension to be a block tag, e.g.
you shouldn't use
nodes.CallBlock
, you should just usenodes.Call
instead, which doesn't take a body parameter. So just do:self.call_method
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.