尝试制作一个简单的 Jinja2 扩展来处理 NDB 期货
好的,有一点背景知识:我正在使用 NDB,也许更相关,其实行“期货”。基本上,它们代表异步运行的工作,并且可以通过调用 get_result()
来获取结果。
目前,在创建上下文以提供模板时,我必须在将所有 future 传递给 Jinja2 之前对它们进行 de-future,这效率较低,会导致重复代码,也许最重要的是:很混乱。
我想要一个可以为我完成此操作的扩展程序。
我想要这样:
Logged in as {{ user.name }}
有效地是:
Logged in as {{ user.get_result().name }}
仅当 user
是 Future
类的实例时。
Okay, a little background: I'm using NDB, and perhaps more relevantly, its implementation of "futures". Basically, they represent asynchronously running work, and can have their result fetched by calling get_result()
on them.
Currently, when creating the context to provide the template, I have to de-future all the futures before passing them to Jinja2, this is less efficient, causes duplicate code and perhaps most importantly: is messy.
I want an extension that does this for me.
I want this:
Logged in as {{ user.name }}
To effectively be:
Logged in as {{ user.get_result().name }}
Only when user
is an instance of the Future
class.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论